Skip to content

Commit

Permalink
Implement MDC auto-instrumentation for log4j2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Sep 16, 2020
1 parent 452d5bc commit cbb2d17
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +81,7 @@ public static class GetMdcAdvice {
public static void onExit(
@Advice.This ILoggingEvent event,
@Advice.Return(typing = Typing.DYNAMIC, readOnly = false) Map<String, String> contextData) {
if (contextData != null && contextData.containsKey("traceId")) {
if (contextData != null && contextData.containsKey(TRACE_ID)) {
// Assume already instrumented event if traceId is present.
return;
}
Expand All @@ -90,10 +93,10 @@ public static void onExit(

Map<String, String> 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) {
Expand Down

0 comments on commit cbb2d17

Please sign in to comment.