opentelemetry-js/packages/opentelemetry-context-zone
Trent Mick 7332f3a863
chore!: bump min-supported node to ^18.19.0 || >=20.6.0 (#5397)
Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
2025-01-31 09:18:27 +00:00
..
src refactor: remove "export *" in favor of explicit named exports (#4880) 2024-08-09 10:42:38 +00:00
.eslintignore fix(lint): move tslint to eslint (#892) 2020-05-20 20:42:17 -07:00
.eslintrc.js chore(deps): update dependency eslint to v8.43.0 (#3929) 2023-07-06 15:14:56 +02:00
LICENSE Rename scope to context (#853) 2020-03-13 15:01:07 -04:00
README.md feat(sdk-trace-base): add `spanProcessors` property in `TracerConfig` interface (#5138) 2024-11-12 15:48:00 +00:00
package.json chore!: bump min-supported node to ^18.19.0 || >=20.6.0 (#5397) 2025-01-31 09:18:27 +00:00
tsconfig.esm.json Update `next` branch (#4294) 2023-11-15 11:15:51 -05:00
tsconfig.esnext.json Update `next` branch (#4294) 2023-11-15 11:15:51 -05:00
tsconfig.json Update `next` branch (#4294) 2023-11-15 11:15:51 -05:00

README.md

OpenTelemetry Context Zone

NPM Published Version Apache License

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);
});

License

Apache 2.0 - See LICENSE for more information.