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 65d866f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public ElementMatcher<ClassLoader> classLoaderMatcher() {

@Override
public ElementMatcher<? super TypeDescription> typeMatcher() {
// need to instrument a class, not an interface (ContextDataProvider) - otherwise helper classes
// and resources are not injected
return named("org.apache.logging.log4j.core.impl.ThreadContextDataInjector");
}

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/log4j/log4j-2.13.2/library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ a log statement is made when a span is active.

- `traceId`
- `spanId`
- `traceFlags`
- `sampled`

You can use these keys when defining an appender in your `log4j.xml` configuration, for example

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public String[] helperClassNames() {

@Override
public ElementMatcher<ClassLoader> classLoaderMatcher() {
return not(hasClassesNamed("org.apache.logging.log4j.core.util.ContextDataProvider"));
return hasClassesNamed("org.apache.logging.log4j.core.impl.ContextDataInjectorFactory")
.and(not(hasClassesNamed("org.apache.logging.log4j.core.util.ContextDataProvider")));
}

@Override
Expand Down
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 65d866f

Please sign in to comment.