diff --git a/instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/build.gradle.kts b/instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/build.gradle.kts index 9b8ceb49f4c7..26b587e868e3 100644 --- a/instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/build.gradle.kts +++ b/instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/build.gradle.kts @@ -5,7 +5,7 @@ plugins { base.archivesName.set("${base.archivesName.get()}-autoconfigure") dependencies { - implementation(project(":javaagent-bootstrap")) + compileOnly(project(":javaagent-bootstrap")) library("org.apache.logging.log4j:log4j-core:2.17.0") testImplementation(project(":instrumentation:log4j:log4j-context-data:log4j-context-data-common:testing")) diff --git a/instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/log4j/contextdata/v2_17/OpenTelemetryContextDataProvider.java b/instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/log4j/contextdata/v2_17/OpenTelemetryContextDataProvider.java index 2ce6e3126ac5..0ffe9258d749 100644 --- a/instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/log4j/contextdata/v2_17/OpenTelemetryContextDataProvider.java +++ b/instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/log4j/contextdata/v2_17/OpenTelemetryContextDataProvider.java @@ -26,6 +26,19 @@ * #supplyContextData()} is called when a log entry is created. */ public class OpenTelemetryContextDataProvider implements ContextDataProvider { + + private boolean configuredResourceAttributeAccessible; + + public OpenTelemetryContextDataProvider() { + try { + Class.forName("io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder"); + this.configuredResourceAttributeAccessible = true; + + } catch (ClassNotFoundException ok) { + this.configuredResourceAttributeAccessible = false; + } + } + private static final boolean BAGGAGE_ENABLED = ConfigPropertiesUtil.getBoolean("otel.instrumentation.log4j-context-data.add-baggage", false); @@ -48,7 +61,10 @@ public Map supplyContextData() { contextData.put(TRACE_ID, spanContext.getTraceId()); contextData.put(SPAN_ID, spanContext.getSpanId()); contextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex()); - contextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttributes()); + + if (configuredResourceAttributeAccessible) { + contextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttributes()); + } if (BAGGAGE_ENABLED) { Baggage baggage = Baggage.fromContext(context);