Skip to content

Latest commit

 

History

History
82 lines (62 loc) · 3.55 KB

File metadata and controls

82 lines (62 loc) · 3.55 KB

OpenTelemetry Long Task Instrumentation for web

NPM Published Version dependencies devDependencies Apache License

This instrumentation creates spans from tasks that take more than 50 milliseconds using the Long Task API. All of the data reported via PerformanceLongTaskTiming is included as span attributes.

Compatible with OpenTelemetry JS API and SDK 1.0+.

Installation

npm install --save @opentelemetry/instrumentation-long-task

Usage

import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
import { LongTaskInstrumentation } from '@opentelemetry/instrumentation-long-task';
import { registerInstrumentations } from '@opentelemetry/instrumentation';

const provider = new WebTracerProvider();

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

registerInstrumentations({
  tracerProvider: provider,
  instrumentations: [
    new LongTaskInstrumentation({
      // see under for available configuration
    }),
  ],
});

longtask Instrumentation Options

Options Type Description
observerCallback ObserverCallback Callback executed on observed longtask, allowing additional attributes to be attached to the span.

The observerCallback function is passed the created span and the longtask PerformanceEntry, allowing the user to add custom attributes to the span with any logic. For example, a webapp with client-side routing can add contextual information on the current page, even if the tracer was instantiated before navigation.

Usage Example:

longtaskInstrumentationConfig = {
  observerCallback: (span, longtaskEvent) => {
    span.setAttribute('location.pathname', window.location.pathname)
  }
}

Useful links

License

Apache 2.0 - See LICENSE for more information.