Skip to content

Commit e8abddb

Browse files
authored
examples(tracing): add multi exporter example (open-telemetry#537)
* docs: add multi exporter example * chore: add script and update readme
1 parent fa583fd commit e8abddb

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

examples/basic-tracer-node/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ Click on the trace to view its details.
5757

5858
<p align="center"><img src="./images/jaeger-ui-detail.png?raw=true"/></p>
5959

60+
### Export to multiple exporters
61+
62+
- Run the sample
63+
64+
```sh
65+
$ # from this directory
66+
$ npm run multi_exporter
67+
```
68+
69+
This will export the spans data simultaneously on `Zipkin` and `Jaeger` backend. This is handy if transitioning from one vendor/OSS project to another for the tracing backend. You might want to export to both during the transitional phase.
70+
6071
## Useful links
6172
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
6273
- For more information on tracing, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-tracing>
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+
}

examples/basic-tracer-node/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"main": "index.js",
77
"scripts": {
88
"zipkin:basic": "cross-env EXPORTER=zipkin node ./index.js",
9-
"jaeger:basic": "cross-env EXPORTER=jaeger node ./index.js"
9+
"jaeger:basic": "cross-env EXPORTER=jaeger node ./index.js",
10+
"multi_exporter": "node ./multi_exporter.js"
1011
},
1112
"repository": {
1213
"type": "git",

0 commit comments

Comments
 (0)