Skip to content

Commit cf86974

Browse files
committed
(#9297) add new instrumentation config otel.instrumentation.mdc.resource-attributes that will be used to configure Resource attributes to be added to logging map diagnostic context.
1 parent 4deb652 commit cf86974

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

instrumentation/log4j/log4j-mdc-1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/mdc/v1_2/LoggingEventInstrumentation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2121
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2222
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
23+
import io.opentelemetry.javaagent.tooling.ConfiguredResourceAttributesHolder;
2324
import net.bytebuddy.asm.Advice;
2425
import net.bytebuddy.description.type.TypeDescription;
2526
import net.bytebuddy.matcher.ElementMatcher;
@@ -79,6 +80,10 @@ public static void onExit(
7980
default:
8081
// do nothing
8182
}
83+
} else if (ConfiguredResourceAttributesHolder.getAttributeValue(key) != null) {
84+
if (value == null) {
85+
value = ConfiguredResourceAttributesHolder.getAttributeValue(key);
86+
}
8287
}
8388
}
8489
}

instrumentation/logback/logback-mdc-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/logback/mdc/v1_0/LoggingEventInstrumentation.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2727
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2828
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
29+
import io.opentelemetry.javaagent.tooling.ConfiguredResourceAttributesHolder;
2930
import java.util.HashMap;
3031
import java.util.Map;
3132
import net.bytebuddy.asm.Advice;
@@ -79,6 +80,8 @@ public static void onExit(
7980
spanContextData.put(TRACE_ID, spanContext.getTraceId());
8081
spanContextData.put(SPAN_ID, spanContext.getSpanId());
8182
spanContextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
83+
84+
spanContextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttribute());
8285
}
8386

8487
if (LogbackSingletons.addBaggage()) {

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import io.opentelemetry.javaagent.tooling.muzzle.AgentTooling;
4242
import io.opentelemetry.javaagent.tooling.util.Trie;
4343
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
44+
import io.opentelemetry.sdk.autoconfigure.SdkAutoconfigureAccess;
4445
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
4546
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
4647
import java.lang.instrument.Instrumentation;
@@ -125,6 +126,9 @@ private static void installBytebuddyAgent(
125126
copyNecessaryConfigToSystemProperties(sdkConfig);
126127

127128
setBootstrapPackages(sdkConfig, extensionClassLoader);
129+
ConfiguredResourceAttributesHolder.initialize(
130+
SdkAutoconfigureAccess.getResourceAttribute(autoConfiguredSdk),
131+
InstrumentationConfig.get());
128132

129133
for (BeforeAgentListener agentListener :
130134
loadOrdered(BeforeAgentListener.class, extensionClassLoader)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.tooling;
7+
8+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
9+
import static java.util.Collections.emptyList;
10+
11+
import io.opentelemetry.api.common.Attributes;
12+
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
public final class ConfiguredResourceAttributesHolder {
18+
19+
private static final Map<String, String> resourceAttribute = new HashMap<>();
20+
21+
public static Map<String, String> getResourceAttribute() {
22+
return resourceAttribute;
23+
}
24+
25+
public static void initialize(Attributes resourceAttribute, InstrumentationConfig config) {
26+
List<String> mdcResourceAttributes =
27+
config.getList("otel.instrumentation.mdc.resource-attributes", emptyList());
28+
29+
for (String key : mdcResourceAttributes) {
30+
String value = resourceAttribute.get(stringKey(key));
31+
if (value != null) {
32+
ConfiguredResourceAttributesHolder.resourceAttribute.put(
33+
String.format("otel.resource.%s", key), value);
34+
}
35+
}
36+
}
37+
38+
public static String getAttributeValue(String key) {
39+
return resourceAttribute.get(String.format("otel.resource.%s", key));
40+
}
41+
42+
private ConfiguredResourceAttributesHolder() {}
43+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.autoconfigure;
7+
8+
import io.opentelemetry.api.common.Attributes;
9+
10+
public final class SdkAutoconfigureAccess {
11+
public static Attributes getResourceAttribute(AutoConfiguredOpenTelemetrySdk sdk) {
12+
return sdk.getResource().getAttributes();
13+
}
14+
15+
private SdkAutoconfigureAccess() {}
16+
}

0 commit comments

Comments
 (0)