@opentelemetry/context-zone

esm
OpenTelemetry Context Zone
Version 2.0.1 License Apache-2.0
Keywords
opentelemetrywebtracingprofilingmetricsstats
INSTALL
Type:
Version:
- Static
- Latest Patch
- Latest Minor
- Latest Major
- 2.0.1
- 2.0.0
- 1.30.1
- 1.30.0
- 1.29.0
- 1.28.0
- 1.27.0
- 1.26.0
- 1.25.1
- 1.25.0
- 1.24.1
- 1.24.0
- 1.23.0
- 1.22.0
- 1.21.0
- 1.20.0
- 1.19.0
- 1.18.1
- 1.18.0
- 1.17.1
- 1.17.0
- 1.16.0
- 1.15.2
- 1.15.1
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.0
- 1.11.0
- 1.10.1
- 1.10.0
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- 0.26.0
- 0.25.0
- 0.24.0
- 0.23.0
- 0.22.0
- 0.21.0
- 0.20.0
- 0.19.0
- 0.18.2
- 0.18.1
- 0.18.0
- 0.17.0
- 0.16.0
- 0.15.0
- 0.14.0
- 0.13.0
- 0.12.0
- 0.11.0
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.0
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.2
- 0.5.1
- 0.5.0
- 2.0.0-rc.1
- 2.0.0-dev.1
- 2.0.0-dev.0
- 0.25.1-alpha.21
- 0.25.1-alpha.13
- 0.25.1-alpha.4
- 0.24.1-alpha.31
- 0.24.1-alpha.19
- 0.24.1-alpha.6
- 0.24.1-alpha.5
- 0.24.1-alpha.4
- 0.24.1-alpha.1
- 0.23.1-alpha.28
- 0.23.1-alpha.23
- 0.23.1-alpha.18
- 0.23.1-alpha.17
- 0.23.1-alpha.15
- 0.23.1-alpha.3
- 0.22.1-alpha.16
- 0.22.1-alpha.15
- 0.22.1-alpha.14
- 0.22.1-alpha.13
- 0.22.1-alpha.5
- 0.21.1-alpha.7
- 0.21.1-alpha.1
- 0.20.1-alpha.49
- 0.20.1-alpha.4
- 0.20.1-alpha.3
- 0.19.1-alpha.43
- 0.19.1-alpha.39
- 0.19.1-alpha.37
- 0.19.1-alpha.36
- 0.19.1-alpha.31
- 0.19.1-alpha.11
- 0.18.3-alpha.25
- 0.18.3-alpha.20
- 0.18.3-alpha.16
- 0.18.3-alpha.7
- 0.18.3-alpha.5
- 0.18.3-alpha.4
- 0.18.3-alpha.2
- 0.18.3-alpha.1
- 0.18.2-alpha.81
- 0.18.2-alpha.1
- 0.18.1-alpha.24
- 0.18.1-alpha.21
- 0.18.1-alpha.20
- 0.18.1-alpha.12
- 0.17.1-alpha.21
- 0.17.1-alpha.8
- 0.17.1-alpha.5
- 0.16.1-alpha.11
- 0.12.1-alpha.54
- 0.12.1-alpha.3
- 0.11.1-alpha.48
- 0.11.1-alpha.36
- 0.10.3-canary.0
<script src=" https://cdn.jsdelivr.net/npm/@opentelemetry/context-zone@2.0.1/build/src/index.min.js "></script>
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, trace } from '@opentelemetry/api';
import {
ConsoleSpanExporter,
SimpleSpanProcessor,
WebTracerProvider,
} from '@opentelemetry/sdk-trace-web';
import { ZoneContextManager } from '@opentelemetry/context-zone';
const providerWithZone = new WebTracerProvider({
spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())]
});
providerWithZone.register({
contextManager: new ZoneContextManager()
});
// Example how the ZoneContextManager keeps the reference to the correct context during async operations
const webTracerWithZone = providerWithZone.getTracer('default');
const span1 = webTracerWithZone.startSpan('foo1');
context.with(trace.setSpan(context.active(), span1), () => {
console.log('Current span is span1', trace.getSpan(context.active()) === span1);
setTimeout(() => {
const span2 = webTracerWithZone.startSpan('foo2');
console.log('Current span is span1', trace.getSpan(context.active()) === span1);
context.with(trace.setSpan(context.active(), span2), () => {
console.log('Current span is span2', trace.getSpan(context.active()) === span2);
setTimeout(() => {
console.log('Current span is span2', trace.getSpan(context.active()) === span2);
}, 500);
});
// there is a timeout which still keeps span2 active
console.log('Current span is span2', trace.getSpan(context.active()) === span2);
}, 500);
console.log('Current span is span1', trace.getSpan(context.active()) === span1);
});
Useful links
- For more information on OpenTelemetry, visit: https://opentelemetry.io/
- For more about OpenTelemetry JavaScript: https://github.com/open-telemetry/opentelemetry-js
- For help or feedback on this project, join us in GitHub Discussions
License
Apache 2.0 - See LICENSE for more information.