From cbb2d17e6e1dd40a8a466a4bde9ff8b925b87d86 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Wed, 16 Sep 2020 13:35:08 +0200 Subject: [PATCH] Implement MDC auto-instrumentation for log4j2 --- .../logback/v1_0_0/LoggingEventInstrumentation.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/instrumentation/logback/logback-1.0.0/auto/src/main/java/io/opentelemetry/instrumentation/auto/logback/v1_0_0/LoggingEventInstrumentation.java b/instrumentation/logback/logback-1.0.0/auto/src/main/java/io/opentelemetry/instrumentation/auto/logback/v1_0_0/LoggingEventInstrumentation.java index 2a1d982b4874..a9bfd905abca 100644 --- a/instrumentation/logback/logback-1.0.0/auto/src/main/java/io/opentelemetry/instrumentation/auto/logback/v1_0_0/LoggingEventInstrumentation.java +++ b/instrumentation/logback/logback-1.0.0/auto/src/main/java/io/opentelemetry/instrumentation/auto/logback/v1_0_0/LoggingEventInstrumentation.java @@ -16,6 +16,9 @@ package io.opentelemetry.instrumentation.auto.logback.v1_0_0; +import static io.opentelemetry.instrumentation.api.log.LoggingContextConstants.SAMPLED; +import static io.opentelemetry.instrumentation.api.log.LoggingContextConstants.SPAN_ID; +import static io.opentelemetry.instrumentation.api.log.LoggingContextConstants.TRACE_ID; import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.isMethod; @@ -78,7 +81,7 @@ public static class GetMdcAdvice { public static void onExit( @Advice.This ILoggingEvent event, @Advice.Return(typing = Typing.DYNAMIC, readOnly = false) Map contextData) { - if (contextData != null && contextData.containsKey("traceId")) { + if (contextData != null && contextData.containsKey(TRACE_ID)) { // Assume already instrumented event if traceId is present. return; } @@ -90,10 +93,10 @@ public static void onExit( Map spanContextData = new HashMap<>(); SpanContext spanContext = currentSpan.getContext(); - spanContextData.put("traceId", spanContext.getTraceIdAsHexString()); - spanContextData.put("spanId", spanContext.getSpanIdAsHexString()); + spanContextData.put(TRACE_ID, spanContext.getTraceIdAsHexString()); + spanContextData.put(SPAN_ID, spanContext.getSpanIdAsHexString()); if (spanContext.isSampled()) { - spanContextData.put("sampled", "true"); + spanContextData.put(SAMPLED, "true"); } if (contextData == null) {