-
Notifications
You must be signed in to change notification settings - Fork 532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Winston logging not working with node js #2280
Comments
You need to add the winston transport as well for this to work: https://www.npmjs.com/package/@opentelemetry/winston-transport |
Adding this giving more output but still spanId and traceId are not coming . const { OpenTelemetryTransportV3 } = require('@opentelemetry/winston-transport'); output : - {"level":"info","message":"foobar"} |
can you try removing the hook use, I haven't used hooks then also its working fine for me |
yeah removed it and posted then only registerInstrumentations({ |
even mine isn't working wherein I have a library which uses opentelemetry and exposes a few things of it to the using application. If I run tracing otherwise works fine for me. In my tracing isn't working at all not sure why even though verbose logging shows express etc. all instrumented correctly. Also I am doing this in the library test file. If anyone can help getting some console.error at resource/detector.ts not sure why as all resources are showing correctly in the logs etc as well |
Try to install the the winston transport It looks like an problem with dependecies for |
I have got same problem, traceId and spanId are undefined. Is there anything to configure ? here are the transports I set in winston.:
and the logs (everything is undefined)
|
It seems that the traceId and spanId fields are undefined because there’s no active trace context available at the time the logs are being created. OpenTelemetry requires an active span for trace context to propagate to the logs, but looks like Winston doesn’t automatically generate or inherit this context. I just make a little repo to help you clarify this situation. https://github.com/jennysson-junior/demo-instrumenting-winston |
thks but I can't see where you are injecting the context. It is destructered from the library but never used in your file |
I just forget to remove, It isn't necessary. It's all inject by auto instrumentation. |
got it, but this means that auto instrumentation in necessary and that the example in the readme file is wring, which is a problem. |
Not Really, the point is that traceId and spanId fields are undefined because there’s no active trace. In my example I've use auto instrumentation to simplify the process. In case that I've present as example, when have log on
|
looking further and the issue is in this line : When commenting this line out, spanId and traceId are set and no longer undefined. But I guess then, the winston logs are not sent to opentelemetry ? |
There is some confusion here:
One complicating factor is that there is a known issue where the "log However, the known issue doesn't seem to be a problem for @jennysson-junior's demo code (https://github.com/jennysson-junior/demo-instrumenting-winston). |
This is package.json
"dependencies": {
"@opentelemetry/api": "1.4.1",
"@opentelemetry/api-logs": "^0.52",
"@opentelemetry/sdk-trace-node": "^1.25.0",
"@opentelemetry/sdk-logs": "^0.52",
"@opentelemetry/instrumentation-http": "^0.52.0",
"@opentelemetry/instrumentation-express": "^0.40",
"@opentelemetry/instrumentation-winston": "0.38",
"@opentelemetry/core": "^1.25",
"express": "^4.19",
"axios": "^1.7",
"winston": "^3.13"
}
This is the real code in tracer.js
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const logsAPI = require('@opentelemetry/api-logs');
const {
LoggerProvider,
SimpleLogRecordProcessor,
ConsoleLogRecordExporter,
} = require('@opentelemetry/sdk-logs');
const { WinstonInstrumentation } = require('@opentelemetry/instrumentation-winston');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const tracerProvider = new NodeTracerProvider();
tracerProvider.register();
// To start a logger, you first need to initialize the Logger provider.
const loggerProvider = new LoggerProvider();
// Add a processor to export log record
loggerProvider.addLogRecordProcessor(
new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())
);
logsAPI.logs.setGlobalLoggerProvider(loggerProvider);
registerInstrumentations({
instrumentations: [
new WinstonInstrumentation({
level:'info',
logHook: (record, span) => {
// Add custom attributes or context to log records
record.attributes['trace_id'] = span.spanContext().traceId;
record.attributes['span_id'] = span.spanContext().spanId;
}
// See below for Winston instrumentation options.
}),
],
});
const winston = require('winston');
const logger = winston.createLogger({
level: "info",
format: winston.format.json(),
})
logger.info('foobar'); // prints {"level":"info","message":"foobar"} no traceid keys
No opentelemetry trace ids are getting printed, I required this in some other file in server.js there also its not printing trace id.
The text was updated successfully, but these errors were encountered: