-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#9297) add new instrumentation config `otel.instrumentation.mdc.reso…
…urce-attributes` that will be used to configure `Resource` attributes to be added to logging map diagnostic context.
- Loading branch information
1 parent
4deb652
commit 1c02310
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...rc/main/java/io/opentelemetry/javaagent/bootstrap/ConfiguredResourceAttributesHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.bootstrap; | ||
|
||
import static io.opentelemetry.api.common.AttributeKey.stringKey; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
|
||
public final class ConfiguredResourceAttributesHolder { | ||
|
||
private static final Map<String, String> resourceAttribute = new HashMap<>(); | ||
|
||
public static Map<String, String> getResourceAttribute() { | ||
return resourceAttribute; | ||
} | ||
|
||
public static void initialize(Attributes resourceAttribute) { | ||
String[] mdcResourceAttributes = getConfiguredAttributes(); | ||
|
||
for (String key : mdcResourceAttributes) { | ||
String value = resourceAttribute.get(stringKey(key)); | ||
if (value != null) { | ||
ConfiguredResourceAttributesHolder.resourceAttribute.put( | ||
String.format("otel.resource.%s", key), value); | ||
} | ||
} | ||
} | ||
|
||
private static String[] getConfiguredAttributes() { | ||
String resourceAttributes = | ||
ConfigPropertiesUtil.getString("otel.instrumentation.mdc.resource-attributes"); | ||
if (resourceAttributes == null) { | ||
return new String[] {}; | ||
} | ||
return resourceAttributes.split(","); | ||
} | ||
|
||
@Nullable | ||
public static String getAttributeValue(String key) { | ||
return resourceAttribute.get(String.format("otel.resource.%s", key)); | ||
} | ||
|
||
private ConfiguredResourceAttributesHolder() {} | ||
} |
53 changes: 53 additions & 0 deletions
53
...est/java/io/opentelemetry/javaagent/bootstrap/ConfiguredResourceAttributesHolderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.bootstrap; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import java.util.Collections; | ||
import org.junit.jupiter.api.Test; | ||
import org.junitpioneer.jupiter.ClearSystemProperty; | ||
import org.junitpioneer.jupiter.SetSystemProperty; | ||
|
||
class ConfiguredResourceAttributesHolderTest { | ||
|
||
@Test | ||
@SetSystemProperty( | ||
key = "otel.instrumentation.mdc.resource-attributes", | ||
value = "service.name,runtime") | ||
void testGetAttributeValue() { | ||
Attributes attributes = Attributes.builder().put("service.name", "test-service").build(); | ||
|
||
ConfiguredResourceAttributesHolder.initialize(attributes); | ||
assertEquals( | ||
"test-service", ConfiguredResourceAttributesHolder.getAttributeValue("service.name")); | ||
} | ||
|
||
@Test | ||
@SetSystemProperty(key = "otel.instrumentation.mdc.resource-attributes", value = "items") | ||
void testGetAttributeValueWhenKeyIsNotString() { | ||
Attributes attributes = | ||
Attributes.builder() | ||
.put(AttributeKey.stringArrayKey("items"), Collections.singletonList("test-item")) | ||
.build(); | ||
|
||
ConfiguredResourceAttributesHolder.initialize(attributes); | ||
assertNull(ConfiguredResourceAttributesHolder.getAttributeValue("items")); | ||
} | ||
|
||
@Test | ||
@ClearSystemProperty(key = "otel.instrumentation.mdc.resource-attributes") | ||
void testGetAttributeValueWhenConfigIsNotSet() { | ||
Attributes attributes = | ||
Attributes.builder().put(AttributeKey.stringArrayKey("don't care"), "won't care").build(); | ||
|
||
ConfiguredResourceAttributesHolder.initialize(attributes); | ||
assertNull(ConfiguredResourceAttributesHolder.getAttributeValue("dc-wc")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...gent-tooling/src/main/java/io/opentelemetry/sdk/autoconfigure/SdkAutoconfigureAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.autoconfigure; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
|
||
public final class SdkAutoconfigureAccess { | ||
public static Attributes getResourceAttributes(AutoConfiguredOpenTelemetrySdk sdk) { | ||
return sdk.getResource().getAttributes(); | ||
} | ||
|
||
private SdkAutoconfigureAccess() {} | ||
} |