What is @opentelemetry/instrumentation-winston?
@opentelemetry/instrumentation-winston is an npm package that provides automatic instrumentation for the Winston logging library, enabling the collection of telemetry data such as traces and metrics. This helps in monitoring and debugging applications by providing insights into the logging behavior.
What are @opentelemetry/instrumentation-winston's main functionalities?
Automatic Trace Context Injection
This feature automatically injects trace context into Winston log messages, allowing you to correlate logs with traces. The code sample demonstrates how to enable the instrumentation and create a Winston logger that includes trace context in its log messages.
const { WinstonInstrumentation } = require('@opentelemetry/instrumentation-winston');
const winston = require('winston');
const instrumentation = new WinstonInstrumentation();
instrumentation.enable();
const logger = winston.createLogger({
transports: [
new winston.transports.Console()
]
});
logger.info('This is an info message');
Custom Log Format with Trace Context
This feature allows you to customize the log format to include trace context information. The code sample shows how to create a custom log format that includes a timestamp and other metadata, which can be useful for detailed log analysis.
const { WinstonInstrumentation } = require('@opentelemetry/instrumentation-winston');
const winston = require('winston');
const instrumentation = new WinstonInstrumentation();
instrumentation.enable();
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(({ level, message, timestamp, ...meta }) => {
return `${timestamp} [${level}]: ${message} ${JSON.stringify(meta)}`;
})
),
transports: [
new winston.transports.Console()
]
});
logger.info('This is an info message');
Other packages similar to @opentelemetry/instrumentation-winston
winston
Winston is a popular logging library for Node.js that provides a simple and universal logging interface. While it does not provide automatic instrumentation for OpenTelemetry, it can be used in conjunction with other OpenTelemetry packages to achieve similar functionality.
@opentelemetry/instrumentation-pino
@opentelemetry/instrumentation-pino is an OpenTelemetry instrumentation package for the Pino logging library. It provides similar functionality to @opentelemetry/instrumentation-winston by automatically injecting trace context into log messages, but it is designed specifically for Pino instead of Winston.
bunyan
Bunyan is another logging library for Node.js that focuses on JSON log output. Like Winston, it does not provide built-in OpenTelemetry instrumentation, but it can be integrated with OpenTelemetry manually to achieve similar results.
OpenTelemetry instrumentation for winston
This module provides automatic instrumentation for injection of trace context to winston
.
Compatible with OpenTelemetry JS API and SDK 1.0+
.
Installation
npm install --save @opentelemetry/instrumentation-winston
Usage
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { WinstonInstrumentation } = require('@opentelemetry/instrumentation-winston');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
instrumentations: [
new WinstonInstrumentation({
logHook: (record, span) => {
record['resource.service.name'] = provider.resource.attributes['service.name'];
},
}),
],
});
const winston = require('winston');
const logger = winston.createLogger({
transports: [new winston.transports.Console()],
})
logger.info('foobar');
Fields added to Winston metadata
For the current active span, the following fields are injected:
trace_id
span_id
trace_flags
When no span context is active or the span context is invalid, injection is skipped.
Supported versions
1.x
, 2.x
, 3.x
Useful links
License
Apache 2.0 - See LICENSE for more information.