Skip to content

Commit

Permalink
(#9297)
Browse files Browse the repository at this point in the history
- use compile dependency on `:javaagent-bootstrap` and do runtime check before use.
  • Loading branch information
cleverchuk committed Oct 17, 2023
1 parent 90177b9 commit 6ec28c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -48,7 +61,10 @@ public Map<String, String> 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);
Expand Down

0 comments on commit 6ec28c2

Please sign in to comment.