0.12.2 • Published 3 years ago

kelvin-context-zone-peer-dep v0.12.2

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
3 years ago

OpenTelemetry Context Zone Peer Dependency

Gitter chat NPM Published Version dependencies devDependencies Apache License

This module provides Zone Context Manager with a peer dependency for zone-js for Web applications. If you use Angular you already have the zone-js and you should use this package. If you don't have your own zone-js please use @opentelemetry/context-zone

Installation

Please note that due to an issue with zone.js, the ZoneContextManager does not work with JS code targeting ES2017+. In order to use the ZoneContextManager, please transpile back to ES2015.

npm install --save @opentelemetry/context-zone-peer-dep

Usage

import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing';
import { WebTracer } from '@opentelemetry/web';
import { ZoneContextManager } from '@opentelemetry/context-zone-peer-dep';

const webTracerWithZone = new WebTracer({
  contextManager: new ZoneContextManager()
});
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

// Example how the ZoneContextManager keeps the reference to the correct context during async operations
const span1 = webTracerWithZone.startSpan('foo1');
webTracerWithZone.withSpan(span1, () => {
  console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1);
  setTimeout(() => {
    const span2 = webTracerWithZone.startSpan('foo2');
    console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1);
    webTracerWithZone.withSpan(span2, () => {
      console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2);
      setTimeout(() => {
        console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2);
      }, 500);
    });
    // there is a timeout which still keeps span2 active
    console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2);
  }, 500);
  console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1);
});

Useful links

License

Apache 2.0 - See LICENSE for more information.