-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest-trace.js
More file actions
82 lines (65 loc) · 2.91 KB
/
test-trace.js
File metadata and controls
82 lines (65 loc) · 2.91 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
const { trace } = require("@opentelemetry/api");
const { InMemorySpanExporter, SimpleSpanProcessor, BasicTracerProvider } = require("@opentelemetry/sdk-trace-base");
// Mocking some OTel 2.x behavior
async function testTrace() {
console.log("Starting OTel 2.x Trace Test...");
const exporter = new InMemorySpanExporter();
const processor = new SimpleSpanProcessor(exporter);
// Create provider
const provider = new BasicTracerProvider();
console.log("Provider class:", provider.constructor.name);
// Check for addSpanProcessor
if (typeof provider.addSpanProcessor === 'function') {
console.log("Found addSpanProcessor");
provider.addSpanProcessor(processor);
} else {
console.log("addSpanProcessor NOT found. Checking _activeSpanProcessor...");
if (provider._activeSpanProcessor) {
console.log("Found _activeSpanProcessor");
if (provider._activeSpanProcessor._spanProcessors) {
provider._activeSpanProcessor._spanProcessors.push(processor);
console.log("Attached to _spanProcessors array");
} else {
console.log("_spanProcessors array NOT found on _activeSpanProcessor");
}
}
}
// Register provider (OTel 2.x way if register exists, or just set global)
if (typeof provider.register === 'function') {
console.log("Calling provider.register()");
provider.register();
} else {
console.log("provider.register NOT found. Setting global tracer provider manually...");
trace.setGlobalTracerProvider(provider);
}
const tracer = trace.getTracer("test-tracer");
const span = tracer.startSpan("test-span");
console.log("Span started:", span.spanContext().spanId);
// Add some attributes
span.setAttribute("test.attribute", "value");
// Simulate some work
await new Promise(resolve => setTimeout(resolve, 100));
span.end();
console.log("Span ended");
// Wait for processor to flush
await new Promise(resolve => setTimeout(resolve, 200));
const spans = exporter.getFinishedSpans();
console.log(`Captured spans: ${spans.length}`);
if (spans.length > 0) {
const capturedSpan = spans[0];
console.log("Captured span name:", capturedSpan.name);
console.log("Captured span attributes:", JSON.stringify(capturedSpan.attributes));
// Test sanitization (mimic base.ts)
const sanitized = {
name: capturedSpan.name,
context: capturedSpan.spanContext(),
startTime: capturedSpan.startTime,
endTime: capturedSpan.endTime,
attributes: capturedSpan.attributes,
};
console.log("Sanitized span (serializable):", JSON.stringify(sanitized, null, 2));
} else {
console.error("FAILED: No spans captured!");
}
}
testTrace().catch(console.error);