This repository was archived by the owner on Jun 28, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest-otel.js
More file actions
58 lines (52 loc) · 1.83 KB
/
Copy pathtest-otel.js
File metadata and controls
58 lines (52 loc) · 1.83 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
const { trace } = require("@opentelemetry/api");
const {
BasicTracerProvider,
InMemorySpanExporter,
SimpleSpanProcessor
} = require("@opentelemetry/sdk-trace-base");
// 1. Setup Provider
const provider = new BasicTracerProvider();
const exporter = new InMemorySpanExporter();
const processor = new SimpleSpanProcessor(exporter);
// Use the 2.x style if available, or fallback to addSpanProcessor
if (typeof provider.addSpanProcessor === 'function') {
console.log("Using addSpanProcessor");
provider.addSpanProcessor(processor);
} else {
console.log("Using OTel 2.x _activeSpanProcessor fallback");
const activeProcessor = provider._activeSpanProcessor || provider.activeSpanProcessor;
if (activeProcessor && Array.isArray(activeProcessor._spanProcessors)) {
activeProcessor._spanProcessors.push(processor);
} else {
console.error("Failed to find a way to attach span processor!");
}
}
trace.setGlobalTracerProvider(provider);
// 2. Create a span
const tracer = trace.getTracer("test-tracer");
const span = tracer.startSpan("test-span");
span.setAttribute("test-attr", "value");
span.end();
// 3. Check results
setTimeout(() => {
const spans = exporter.getFinishedSpans();
console.log(`Captured ${spans.length} spans`);
if (spans.length > 0) {
console.log("Span name:", spans[0].name);
console.log("Span attributes:", JSON.stringify(spans[0].attributes));
// Test serialization (what we do in base.ts)
try {
const sanitized = spans.map(s => ({
name: s.name,
context: s.spanContext(),
attributes: s.attributes,
startTime: s.startTime,
endTime: s.endTime
}));
console.log("Serialization successful!");
console.log(JSON.stringify(sanitized, null, 2));
} catch (e) {
console.error("Serialization failed:", e.message);
}
}
}, 100);