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.
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:
- Install package dependency.
npm install --save @opentelemetry/instrumentation-winston
- Add winston instrumentation registration to the file where OpenTelemetry JS instrumentation is configured. See Sumo Logic OpenTelemetry JS auto-instrumentation for details.
- Import dependency:
const { WinstonInstrumentation } = require('@opentelemetry/instrumentation-winston');
- Register
WinstonInstrumentation
:registerInstrumentations({
instrumentations: [
new WinstonInstrumentation(),
// other instrumentations
],
});
- Import dependency:
Bunyan Logger OpenTelemetry instrumentationβ
The following information walks through bunyan logger OpenTelemetry instrumentation:
- Install package dependency:
npm install --save @opentelemetry/instrumentation-bunyan
- 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
],
});
- Import dependency.
Pino Logger OpenTelemetry instrumentationβ
The following information walks through pino logger OpenTelemetry instrumentation.
- Install package dependency.
npm install --save @opentelemetry/instrumentation-pino
- 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
],
});
- Import dependency.
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:
- Install package dependency.
npm install --save @opentelemetry/api
- 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
andtrace
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"
- Import dependency.