Skip to content

Commit 4e0102e

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 4e0102e

6 files changed

Lines changed: 123 additions & 0 deletions

File tree

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)) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
import javax.annotation.Nullable;
17+
18+
public final class ConfiguredResourceAttributesHolder {
19+
20+
private static final Map<String, String> resourceAttribute = new HashMap<>();
21+
22+
public static Map<String, String> getResourceAttribute() {
23+
return resourceAttribute;
24+
}
25+
26+
public static void initialize(Attributes resourceAttribute, InstrumentationConfig config) {
27+
List<String> mdcResourceAttributes =
28+
config.getList("otel.instrumentation.mdc.resource-attributes", emptyList());
29+
30+
for (String key : mdcResourceAttributes) {
31+
String value = resourceAttribute.get(stringKey(key));
32+
if (value != null) {
33+
ConfiguredResourceAttributesHolder.resourceAttribute.put(
34+
String.format("otel.resource.%s", key), value);
35+
}
36+
}
37+
}
38+
39+
@Nullable
40+
public static String getAttributeValue(String key) {
41+
return resourceAttribute.get(String.format("otel.resource.%s", key));
42+
}
43+
44+
private ConfiguredResourceAttributesHolder() {}
45+
}
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+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertNull;
10+
import static org.mockito.ArgumentMatchers.anyList;
11+
import static org.mockito.ArgumentMatchers.eq;
12+
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.when;
14+
15+
import io.opentelemetry.api.common.AttributeKey;
16+
import io.opentelemetry.api.common.Attributes;
17+
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
18+
import java.util.Collections;
19+
import org.junit.jupiter.api.Test;
20+
21+
class ConfiguredResourceAttributesHolderTest {
22+
23+
@Test
24+
void testGetAttributeValue() {
25+
Attributes attributes = Attributes.builder().put("service.name", "test-service").build();
26+
27+
InstrumentationConfig config = mock(InstrumentationConfig.class);
28+
when(config.getList(eq("otel.instrumentation.mdc.resource-attributes"), anyList()))
29+
.thenReturn(Collections.singletonList("service.name"));
30+
31+
ConfiguredResourceAttributesHolder.initialize(attributes, config);
32+
assertEquals(
33+
"test-service", ConfiguredResourceAttributesHolder.getAttributeValue("service.name"));
34+
}
35+
36+
@Test
37+
void testGetAttributeValueWhenKeyIsNotString() {
38+
Attributes attributes =
39+
Attributes.builder()
40+
.put(AttributeKey.stringArrayKey("items"), Collections.singletonList("test-item"))
41+
.build();
42+
43+
InstrumentationConfig config = mock(InstrumentationConfig.class);
44+
when(config.getList(eq("otel.instrumentation.mdc.resource-attributes"), anyList()))
45+
.thenReturn(Collections.singletonList("items"));
46+
47+
ConfiguredResourceAttributesHolder.initialize(attributes, config);
48+
assertNull(ConfiguredResourceAttributesHolder.getAttributeValue("items"));
49+
}
50+
}

0 commit comments

Comments
 (0)