Skip to content

Commit

Permalink
examples(tracing): add multi exporter example (#537)
Browse files Browse the repository at this point in the history
* docs: add multi exporter example

* chore: add script and update readme
  • Loading branch information
mayurkale22 authored Nov 19, 2019
1 parent fa583fd commit e8abddb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
11 changes: 11 additions & 0 deletions examples/basic-tracer-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ Click on the trace to view its details.

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

### Export to multiple exporters

- Run the sample

```sh
$ # from this directory
$ npm run multi_exporter
```

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.

## Useful links
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more information on tracing, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-tracing>
Expand Down
55 changes: 55 additions & 0 deletions examples/basic-tracer-node/multi_exporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const opentelemetry = require('@opentelemetry/core');
const { BasicTracer, BatchSpanProcessor, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');

const tracer = new BasicTracer();

const zipkinExporter = new ZipkinExporter({serviceName: 'basic-service'});
const jaegerExporter = new JaegerExporter({
serviceName: 'basic-service',
// The default flush interval is 5 seconds.
flushInterval: 2000
})

// It is recommended to use this BatchSpanProcessor for better performance
// and optimization, especially in production.
tracer.addSpanProcessor(new BatchSpanProcessor(zipkinExporter, {
bufferSize: 10 // This is added for example, default size is 100.
}));

// It is recommended to use SimpleSpanProcessor in case of Jaeger exporter as
// it's internal client already handles the spans with batching logic.
tracer.addSpanProcessor(new SimpleSpanProcessor(jaegerExporter));

// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
opentelemetry.initGlobalTracer(tracer);

// Create a span. A span must be closed.
const span = opentelemetry.getTracer().startSpan('main');
for (let i = 0; i < 10; i++) {
doWork(span);
}
// Be sure to end the span.
span.end();

// flush and close the connection.
zipkinExporter.shutdown();
jaegerExporter.shutdown();

function doWork(parent) {
// Start another span. In this example, the main method already started a
// span, so that'll be the parent span, and this will be a child span.
const span = opentelemetry.getTracer().startSpan('doWork', {
parent: parent
});

// simulate some random work.
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i++) { }

// Set attributes to the span.
span.setAttribute('key', 'value');

// Annotate our span to capture metadata about our operation
span.addEvent('invoking doWork').end();
}
3 changes: 2 additions & 1 deletion examples/basic-tracer-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"main": "index.js",
"scripts": {
"zipkin:basic": "cross-env EXPORTER=zipkin node ./index.js",
"jaeger:basic": "cross-env EXPORTER=jaeger node ./index.js"
"jaeger:basic": "cross-env EXPORTER=jaeger node ./index.js",
"multi_exporter": "node ./multi_exporter.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit e8abddb

Please sign in to comment.