JSPM

  • Created
  • Published
  • Downloads 282797
  • Score
    100M100P100Q281857F
  • License Apache-2.0

OpenTelemetry Context Zone

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@opentelemetry/context-zone) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    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.