Skip to content

Commit f48de81

Browse files
committed
docs: add multi exporter example
1 parent fa583fd commit f48de81

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const opentelemetry = require('@opentelemetry/core');
2+
const { BasicTracer, BatchSpanProcessor, SimpleSpanProcessor } = require('@opentelemetry/tracing');
3+
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
4+
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');
5+
6+
const tracer = new BasicTracer();
7+
8+
const zipkinExporter = new ZipkinExporter({serviceName: 'basic-service'});
9+
const jaegerExporter = new JaegerExporter({
10+
serviceName: 'basic-service',
11+
// The default flush interval is 5 seconds.
12+
flushInterval: 2000
13+
})
14+
15+
// It is recommended to use this BatchSpanProcessor for better performance
16+
// and optimization, especially in production.
17+
tracer.addSpanProcessor(new BatchSpanProcessor(zipkinExporter, {
18+
bufferSize: 10 // This is added for example, default size is 100.
19+
}));
20+
21+
// It is recommended to use SimpleSpanProcessor in case of Jaeger exporter as
22+
// it's internal client already handles the spans with batching logic.
23+
tracer.addSpanProcessor(new SimpleSpanProcessor(jaegerExporter));
24+
25+
// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
26+
opentelemetry.initGlobalTracer(tracer);
27+
28+
// Create a span. A span must be closed.
29+
const span = opentelemetry.getTracer().startSpan('main');
30+
for (let i = 0; i < 10; i++) {
31+
doWork(span);
32+
}
33+
// Be sure to end the span.
34+
span.end();
35+
36+
// flush and close the connection.
37+
zipkinExporter.shutdown();
38+
jaegerExporter.shutdown();
39+
40+
function doWork(parent) {
41+
// Start another span. In this example, the main method already started a
42+
// span, so that'll be the parent span, and this will be a child span.
43+
const span = opentelemetry.getTracer().startSpan('doWork', {
44+
parent: parent
45+
});
46+
47+
// simulate some random work.
48+
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i++) { }
49+
50+
// Set attributes to the span.
51+
span.setAttribute('key', 'value');
52+
53+
// Annotate our span to capture metadata about our operation
54+
span.addEvent('invoking doWork').end();
55+
}

0 commit comments

Comments
 (0)