The @opentelemetry/instrumentation-bunyan module provides two primary capabilities:
- Log sending: Automatically adds a Bunyan stream to your logger that sends log records to the OpenTelemetry Logs SDK. If no Logger provider is configured in the OpenTelemetry SDK, this stream becomes a no-op.
- Log correlation: Automatically injects trace-context into Bunyan log records when they are emitted within an active tracing span. This allows you to link logs to specific traces.
Supported Bunyan versions: >=1.0.0 <2
const { trace } = require('@opentelemetry/api');
const { SimpleSpanProcessor, ConsoleSpanExporter } = require('@opentelemetry/sdk-trace');
const { SimpleLogRecordProcessor, ConsoleLogRecordExporter } = require('@opentelemetry/sdk-logs');
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { BunyanInstrumentation } = require('@opentelemetry/instrumentation-bunyan');
const sdk = new NodeSDK({
spanProcessors: [
new SimpleSpanProcessor(new ConsoleSpanExporter()),
],
logRecordProcessors: [
new SimpleLogRecordProcessor({ exporter: new ConsoleLogRecordExporter() }),
],
instrumentations: [
new BunyanInstrumentation({}),
]
});
sdk.start();
const bunyan = require('bunyan');
const logger = bunyan.createLogger({name: 'example'});
// Log sending in action
logger.info('hi');
// Log correlation in action
const tracer = trace.getTracer('example');
tracer.startActiveSpan('manual-span', span => {
logger.info('in a span');
// Log record will include trace_id, span_id, and trace_flags
});