Logger MDC auto-instrumentation with opentelemetry-spring-boot-starter #7653
-
Hello, I have a problem with a correlation between logs and traces, log statements don't contain any tracing information when Any suggestion on how tracing information can be appended to each log statement is appreciated. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hey @birkoni5 , Unfortunately the spring boot starter is missing this feature right now; it can be added very easily though. You'll have to include one more dependency, and then configure your logging pattern to include the |
Beta Was this translation helpful? Give feedback.
-
It appears to be working with Spring Boot 3.1.x with the current instrumentation and works with the Logstash JSON encoder as well
|
Beta Was this translation helpful? Give feedback.
-
I dunno whether it is appropriate but I instanciated the initializer as follow public class OtelLogbackMdcAppenderApplicationInitializer implements GenericApplicationListener {
@Override
public boolean supportsEventType(ResolvableType eventType) {
return eventType.isAssignableFrom(ApplicationEnvironmentPreparedEvent.class);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof ApplicationEnvironmentPreparedEvent) {
Logger logger = (Logger) LoggerFactory.getILoggerFactory().getLogger("ROOT");
OpenTelemetryAppender openTelemetryAppender = new OpenTelemetryAppender();
List<Appender<ILoggingEvent>> existingAppelders = new ArrayList<>();
logger.iteratorForAppenders().forEachRemaining(existingAppelders::add);
existingAppelders.forEach(logger::detachAppender);
AtomicBoolean appenderAddedToOpenTelemetry = new AtomicBoolean();
existingAppelders.forEach(existingAppender -> {
if(existingAppender instanceof OpenTelemetryAppender
|| existingAppender instanceof io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender) {
logger.addAppender(existingAppender);
} else {
openTelemetryAppender.addAppender(existingAppender);
appenderAddedToOpenTelemetry.set(true);
}
});
if(appenderAddedToOpenTelemetry.get()) {
openTelemetryAppender.start();
logger.addAppender(openTelemetryAppender);
}
}
}
} and add it in a |
Beta Was this translation helpful? Give feedback.
Hey @birkoni5 ,
Unfortunately the spring boot starter is missing this feature right now; it can be added very easily though. You'll have to include one more dependency, and then configure your logging pattern to include the
trace_id
/span_id
information.