Skip to content

Commit 1c02310

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 1c02310

6 files changed

Lines changed: 132 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
@@ -17,6 +17,7 @@
1717
import io.opentelemetry.api.trace.SpanContext;
1818
import io.opentelemetry.context.Context;
1919
import io.opentelemetry.instrumentation.api.util.VirtualField;
20+
import io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder;
2021
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2122
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2223
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
@@ -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
@@ -23,6 +23,7 @@
2323
import io.opentelemetry.context.Context;
2424
import io.opentelemetry.instrumentation.api.util.VirtualField;
2525
import io.opentelemetry.instrumentation.logback.mdc.v1_0.internal.UnionMap;
26+
import io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder;
2627
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
2728
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2829
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
@@ -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()) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.bootstrap;
7+
8+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
9+
10+
import io.opentelemetry.api.common.Attributes;
11+
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import javax.annotation.Nullable;
15+
16+
public final class ConfiguredResourceAttributesHolder {
17+
18+
private static final Map<String, String> resourceAttribute = new HashMap<>();
19+
20+
public static Map<String, String> getResourceAttribute() {
21+
return resourceAttribute;
22+
}
23+
24+
public static void initialize(Attributes resourceAttribute) {
25+
String[] mdcResourceAttributes = getConfiguredAttributes();
26+
27+
for (String key : mdcResourceAttributes) {
28+
String value = resourceAttribute.get(stringKey(key));
29+
if (value != null) {
30+
ConfiguredResourceAttributesHolder.resourceAttribute.put(
31+
String.format("otel.resource.%s", key), value);
32+
}
33+
}
34+
}
35+
36+
private static String[] getConfiguredAttributes() {
37+
String resourceAttributes =
38+
ConfigPropertiesUtil.getString("otel.instrumentation.mdc.resource-attributes");
39+
if (resourceAttributes == null) {
40+
return new String[] {};
41+
}
42+
return resourceAttributes.split(",");
43+
}
44+
45+
@Nullable
46+
public static String getAttributeValue(String key) {
47+
return resourceAttribute.get(String.format("otel.resource.%s", key));
48+
}
49+
50+
private ConfiguredResourceAttributesHolder() {}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.bootstrap;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertNull;
10+
11+
import io.opentelemetry.api.common.AttributeKey;
12+
import io.opentelemetry.api.common.Attributes;
13+
import java.util.Collections;
14+
import org.junit.jupiter.api.Test;
15+
import org.junitpioneer.jupiter.ClearSystemProperty;
16+
import org.junitpioneer.jupiter.SetSystemProperty;
17+
18+
class ConfiguredResourceAttributesHolderTest {
19+
20+
@Test
21+
@SetSystemProperty(
22+
key = "otel.instrumentation.mdc.resource-attributes",
23+
value = "service.name,runtime")
24+
void testGetAttributeValue() {
25+
Attributes attributes = Attributes.builder().put("service.name", "test-service").build();
26+
27+
ConfiguredResourceAttributesHolder.initialize(attributes);
28+
assertEquals(
29+
"test-service", ConfiguredResourceAttributesHolder.getAttributeValue("service.name"));
30+
}
31+
32+
@Test
33+
@SetSystemProperty(key = "otel.instrumentation.mdc.resource-attributes", value = "items")
34+
void testGetAttributeValueWhenKeyIsNotString() {
35+
Attributes attributes =
36+
Attributes.builder()
37+
.put(AttributeKey.stringArrayKey("items"), Collections.singletonList("test-item"))
38+
.build();
39+
40+
ConfiguredResourceAttributesHolder.initialize(attributes);
41+
assertNull(ConfiguredResourceAttributesHolder.getAttributeValue("items"));
42+
}
43+
44+
@Test
45+
@ClearSystemProperty(key = "otel.instrumentation.mdc.resource-attributes")
46+
void testGetAttributeValueWhenConfigIsNotSet() {
47+
Attributes attributes =
48+
Attributes.builder().put(AttributeKey.stringArrayKey("don't care"), "won't care").build();
49+
50+
ConfiguredResourceAttributesHolder.initialize(attributes);
51+
assertNull(ConfiguredResourceAttributesHolder.getAttributeValue("dc-wc"));
52+
}
53+
}

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
@@ -21,6 +21,7 @@
2121
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader;
2222
import io.opentelemetry.javaagent.bootstrap.BootstrapPackagePrefixesHolder;
2323
import io.opentelemetry.javaagent.bootstrap.ClassFileTransformerHolder;
24+
import io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder;
2425
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper;
2526
import io.opentelemetry.javaagent.bootstrap.InstrumentedTaskClasses;
2627
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizer;
@@ -41,6 +42,7 @@
4142
import io.opentelemetry.javaagent.tooling.muzzle.AgentTooling;
4243
import io.opentelemetry.javaagent.tooling.util.Trie;
4344
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
45+
import io.opentelemetry.sdk.autoconfigure.SdkAutoconfigureAccess;
4446
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
4547
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
4648
import java.lang.instrument.Instrumentation;
@@ -125,6 +127,8 @@ private static void installBytebuddyAgent(
125127
copyNecessaryConfigToSystemProperties(sdkConfig);
126128

127129
setBootstrapPackages(sdkConfig, extensionClassLoader);
130+
ConfiguredResourceAttributesHolder.initialize(
131+
SdkAutoconfigureAccess.getResourceAttributes(autoConfiguredSdk));
128132

129133
for (BeforeAgentListener agentListener :
130134
loadOrdered(BeforeAgentListener.class, extensionClassLoader)) {
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 getResourceAttributes(AutoConfiguredOpenTelemetrySdk sdk) {
12+
return sdk.getResource().getAttributes();
13+
}
14+
15+
private SdkAutoconfigureAccess() {}
16+
}

0 commit comments

Comments
 (0)