Skip to main content

JavaScript TraceId and SpanId injection into logs

Configuring the injection of traceId, spanId, and trace flags data into user logs in JavaScript applications is straightforward. Typically, you only need to add instrumented versions of the logging packages to your project dependencies and register them as new instrumentation.

note

The examples below are compatible with OpenTelemetry JS API and SDK 1.0+.

Winston Logger OpenTelemetry instrumentation​

The following information walks through winston logger OpenTelemetry instrumentation:

  1. Install package dependency.
    npm install --save @opentelemetry/instrumentation-winston
  2. Add winston instrumentation registration to the file where OpenTelemetry JS instrumentation is configured. See Sumo Logic OpenTelemetry JS auto-instrumentation for details.
    1. Import dependency:
      const { WinstonInstrumentation } = require('@opentelemetry/instrumentation-winston');
    2. Register WinstonInstrumentation:
      registerInstrumentations({
      instrumentations: [
      new WinstonInstrumentation(),
      // other instrumentations
      ],
      });

Bunyan Logger OpenTelemetry instrumentation​

The following information walks through bunyan logger OpenTelemetry instrumentation:

  1. Install package dependency:
    npm install --save @opentelemetry/instrumentation-bunyan
  2. Add bunyan instrumentation registration to the file where OpenTelemetry JS instrumentation is configured. See Sumo Logic OpenTelemetry JS auto-instrumentationΒ for details.
    • Import dependency.
      const { BunyanInstrumentation } = require('@opentelemetry/instrumentation-bunyan');
    • Register BunyanInstrumentation.
      registerInstrumentations({
      instrumentations: [
      new BunyanInstrumentation(),
      // other instrumentations
      ],
      });

Pino Logger OpenTelemetry instrumentation​

The following information walks through pino logger OpenTelemetry instrumentation.

  1. Install package dependency.
    npm install --save @opentelemetry/instrumentation-pino
  2. Add pino instrumentation registration to the file where OpenTelemetry JS instrumentation is configured. See Sumo Logic OpenTelemetry JS auto-instrumentationΒ for details.
    • Import dependency.
      const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino');
    • Register PinoInstrumentation.
      registerInstrumentations({
      instrumentations: [
      new PinoInstrumentation(),
      // other instrumentations
      ],
      });

Custom logger - span Context extraction​

In the case of custom loggers, the most important thing is to know how to obtain the current traceId, spanId, and trace flag. Follow the steps below:

  1. Install package dependency.
    npm install --save @opentelemetry/api
  2. Current span Context extraction is the way to obtain required data.
    • Import dependency.
      const api = require('@opentelemetry/api');
    • Get a current span.
      let current_span = api.trace.getSpan(api.context.active());
    • Obtain trace_id, span_id and trace flag.
      let trace_id = current_span.spanContext().traceId;
      let span_id = current_span.spanContext().spanId;
      let trace_flags = current_span.spanContext().traceFlags;
    • Example usage.
      console.log(`Example log trace_id:”${trace_id}” span_id:”${span_id}” trace_flags:”${trace_flags}”`);
    • Example output.
      Example log trace_id:"b2fa3d72711c1adad9ec88348c46f449" span_id:"85733005b2678b28" trace_flags:"1"
Status
Legal
Privacy Statement
Terms of Use

Copyright Β© 2024 by Sumo Logic, Inc.