-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopentelemetry-example.ts
More file actions
131 lines (108 loc) · 4.29 KB
/
opentelemetry-example.ts
File metadata and controls
131 lines (108 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Example: Using OpenTelemetry tracing with the adblock-compiler
*
* This example demonstrates how to integrate OpenTelemetry distributed tracing
* into the compilation process for observability across services.
*
* Usage with Deno:
* ```bash
* # Enable OpenTelemetry in Deno
* OTEL_DENO=true deno run --unstable-otel --allow-net examples/opentelemetry-example.ts
* ```
*
* This will export traces to a local OpenTelemetry collector at localhost:4318
* or to any configured OTLP endpoint.
*/
import { context, trace } from '@opentelemetry/api';
import {
createOpenTelemetryExporter,
SourceCompiler,
TransformationPipeline,
TransformationType,
} from '../src/index.ts';
/**
* Example: Compile a filter list with OpenTelemetry tracing
*/
async function compileWithTracing() {
// Create an OpenTelemetry exporter as the diagnostics collector
const diagnostics = createOpenTelemetryExporter({
serviceName: 'adblock-compiler',
enableConsoleLogging: true, // Enable console output for debugging
});
// Create a source compiler with OpenTelemetry diagnostics
const compiler = new SourceCompiler({
diagnostics,
pipeline: new TransformationPipeline([
TransformationType.RemoveComments,
TransformationType.Deduplicate,
TransformationType.Validate,
]),
});
// Get the global tracer for manual instrumentation
const tracer = trace.getTracer('adblock-compiler-example', '1.0.0');
// Create a parent span for the entire compilation
return tracer.startActiveSpan('compile-filter-list', async (span) => {
try {
span.setAttribute('example', 'opentelemetry-integration');
span.setAttribute('source.count', 1);
console.log('Starting compilation with OpenTelemetry tracing...');
// Compile a test source
const source = {
name: 'Example Filter List',
source: 'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts',
transformations: [
TransformationType.RemoveComments,
TransformationType.Deduplicate,
TransformationType.Validate,
],
};
const rules = await compiler.compile(source, 0, 1);
span.setAttribute('output.rules.count', rules.length);
console.log(`✅ Compilation completed: ${rules.length} rules`);
// Log a sample of rules
console.log('\nSample rules:');
rules.slice(0, 5).forEach((rule) => console.log(` ${rule}`));
return rules;
} catch (error) {
span.recordException(error as Error);
console.error('❌ Compilation failed:', error);
throw error;
} finally {
span.end();
}
});
}
/**
* Example: Using manual span creation for fine-grained tracing
*/
async function manualSpanExample() {
const tracer = trace.getTracer('adblock-compiler-example', '1.0.0');
return tracer.startActiveSpan('manual-span-example', async (parentSpan) => {
parentSpan.setAttribute('example.type', 'manual-instrumentation');
// Create a nested span
const childSpan = tracer.startSpan('child-operation', {}, trace.setSpan(context.active(), parentSpan));
childSpan.setAttribute('operation', 'data-processing');
// Simulate work
await new Promise((resolve) => setTimeout(resolve, 100));
childSpan.end();
parentSpan.end();
console.log('✅ Manual span example completed');
});
}
// Run the examples
if (import.meta.main) {
console.log('=== OpenTelemetry Integration Example ===\n');
try {
// Example 1: Compile with automatic tracing via diagnostics collector
await compileWithTracing();
console.log('\n=== Manual Span Example ===\n');
// Example 2: Manual span creation
await manualSpanExample();
console.log('\n✅ All examples completed successfully!');
console.log('\nTraces have been exported to your OpenTelemetry collector.');
console.log('If running locally, check your Jaeger/Grafana/Datadog dashboard.');
} catch (error) {
console.error('Error running examples:', error);
Deno.exit(1);
}
}