diff --git a/instrumentation/log4j/log4j-2.13.2/auto/src/main/java/io/opentelemetry/instrumentation/auto/log4j/v2_13_2/Log4j2MdcInstrumentation.java b/instrumentation/log4j/log4j-2.13.2/auto/src/main/java/io/opentelemetry/instrumentation/auto/log4j/v2_13_2/Log4j2MdcInstrumentation.java index ca5656f419f7..896b663e0753 100644 --- a/instrumentation/log4j/log4j-2.13.2/auto/src/main/java/io/opentelemetry/instrumentation/auto/log4j/v2_13_2/Log4j2MdcInstrumentation.java +++ b/instrumentation/log4j/log4j-2.13.2/auto/src/main/java/io/opentelemetry/instrumentation/auto/log4j/v2_13_2/Log4j2MdcInstrumentation.java @@ -40,6 +40,8 @@ public ElementMatcher classLoaderMatcher() { @Override public ElementMatcher 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"); } diff --git a/instrumentation/log4j/log4j-2.13.2/library/README.md b/instrumentation/log4j/log4j-2.13.2/library/README.md index 875e1626181b..cef9f06382f6 100644 --- a/instrumentation/log4j/log4j-2.13.2/library/README.md +++ b/instrumentation/log4j/log4j-2.13.2/library/README.md @@ -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 diff --git a/instrumentation/log4j/log4j-2.7/src/main/java/io/opentelemetry/instrumentation/auto/log4j/v2_7/Log4j27MdcInstrumentation.java b/instrumentation/log4j/log4j-2.7/src/main/java/io/opentelemetry/instrumentation/auto/log4j/v2_7/Log4j27MdcInstrumentation.java index eda1bccdc59b..e630fde23235 100644 --- a/instrumentation/log4j/log4j-2.7/src/main/java/io/opentelemetry/instrumentation/auto/log4j/v2_7/Log4j27MdcInstrumentation.java +++ b/instrumentation/log4j/log4j-2.7/src/main/java/io/opentelemetry/instrumentation/auto/log4j/v2_7/Log4j27MdcInstrumentation.java @@ -50,7 +50,8 @@ public String[] helperClassNames() { @Override public ElementMatcher 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 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) {