Skip to content

Commit f8efe85

Browse files
committed
- use compile dependency on `:javaagent-bootstrap` and do runtime check before use.
1 parent 7683547 commit f8efe85

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
base.archivesName.set("${base.archivesName.get()}-autoconfigure")
66

77
dependencies {
8-
implementation(project(":javaagent-bootstrap"))
8+
compileOnly(project(":javaagent-bootstrap"))
99
library("org.apache.logging.log4j:log4j-core:2.17.0")
1010

1111
testImplementation(project(":instrumentation:log4j:log4j-context-data:log4j-context-data-common:testing"))

instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/log4j/contextdata/v2_17/OpenTelemetryContextDataProvider.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626
* #supplyContextData()} is called when a log entry is created.
2727
*/
2828
public class OpenTelemetryContextDataProvider implements ContextDataProvider {
29+
30+
private boolean configuredResourceAttributeAccessible;
31+
32+
public OpenTelemetryContextDataProvider() {
33+
try {
34+
Class.forName("io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder");
35+
this.configuredResourceAttributeAccessible = true;
36+
37+
} catch (ClassNotFoundException ok) {
38+
this.configuredResourceAttributeAccessible = false;
39+
}
40+
}
41+
2942
private static final boolean BAGGAGE_ENABLED =
3043
ConfigPropertiesUtil.getBoolean("otel.instrumentation.log4j-context-data.add-baggage", false);
3144

@@ -48,7 +61,10 @@ public Map<String, String> supplyContextData() {
4861
contextData.put(TRACE_ID, spanContext.getTraceId());
4962
contextData.put(SPAN_ID, spanContext.getSpanId());
5063
contextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
51-
contextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttributes());
64+
65+
if (configuredResourceAttributeAccessible) {
66+
contextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttributes());
67+
}
5268

5369
if (BAGGAGE_ENABLED) {
5470
Baggage baggage = Baggage.fromContext(context);

0 commit comments

Comments
 (0)