Open Fralleee opened 9 months ago
A working but hacky solution:
import { createLogger, format, transports } from "winston";
import { trace } from "@opentelemetry/api";
const { combine, timestamp, json, printf, splat } = format;
const jsonFormatter = combine(
splat(),
timestamp(),
json(),
);
const configuration = {
exitOnError: false,
transports: new transports.Console({
format: combine(
jsonFormatter,
printf((info) => {
const activeSpan = trace.getActiveSpan();
const metaData: { span_id?: string; trace_id?: string } = {};
if (activeSpan) {
metaData.span_id = activeSpan.spanContext().spanId;
metaData.trace_id = activeSpan.spanContext().traceId;
}
return `[${info.level.toUpperCase()}] ${info["timestamp"]}: ${info.message}\n${JSON.stringify({ ...info, ...metaData, })}`;
}),
),
})
};
export const logger = createLogger(configuration);
Not sure if there's any issues by doing it this way.
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.
Description
I'm having issues getting Winston Instrumentation to inject the span context correctly in a NextJS application. Some parts work as expected, but I'm facing issues in certain scenarios, particularly when importing the logger in different parts of the application.
Working Setup
I have set up OpenTelemetry and instrumentation as follows:
Calling the test() function after setting up OpenTelemetry works as expected:
Issue
When logging from the default logger within the app later on, it doesn't work as expected.
Here's how the logger is configured:
And here's how the logger is used in the application:
Question
How can I structure the code to have a single instance of the logger that retains the OpenTelemetry context, allowing span contexts to be correctly injected in a NextJS application?
Any guidance or best practices for setting this up correctly would be immensely appreciated!