OpenTelemetry Context Zone
This module provides Zone Context Manager with bundled zone-js for Web applications.
If you have your own zone-js please use @opentelemetry/context-zone-peer-dep
If you use Angular it means you already have the zone-js and you should use @opentelemetry/context-zone-peer-dep
Installation
npm install --save @opentelemetry/context-zone
Usage
import { context, getSpan, setSpan } from '@opentelemetry/api';
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing';
import { WebTracerProvider } from '@opentelemetry/web';
import { ZoneContextManager } from '@opentelemetry/context-zone';
const providerWithZone = new WebTracerProvider();
providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register({
contextManager: new ZoneContextManager()
});
const webTracerWithZone = providerWithZone.getTracer('default');
const span1 = webTracerWithZone.startSpan('foo1');
context.with(setSpan(context.active(), span1), () => {
console.log('Current span is span1', getSpan(context.active()) === span1);
setTimeout(() => {
const span2 = webTracerWithZone.startSpan('foo2');
console.log('Current span is span1', getSpan(context.active()) === span1);
context.with(setSpan(context.active(), span2), () => {
console.log('Current span is span2', getSpan(context.active()) === span2);
setTimeout(() => {
console.log('Current span is span2', getSpan(context.active()) === span2);
}, 500);
});
console.log('Current span is span2', getSpan(context.active()) === span2);
}, 500);
console.log('Current span is span1', getSpan(context.active()) === span1);
});
Useful links
License
Apache 2.0 - See LICENSE for more information.