Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service.name to MDC #9647

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
(#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
cleverchuk committed Nov 9, 2023
commit 32e4a297195f8bed86b7dec82cdc59c09bd03bc6
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -79,6 +80,10 @@ public static void onExit(
default:
// do nothing
}
} else if (ConfiguredResourceAttributesHolder.getAttributeValue(key) != null) {
if (value == null) {
value = ConfiguredResourceAttributesHolder.getAttributeValue(key);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.instrumentation.logback.mdc.v1_0.internal.UnionMap;
import io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -79,6 +80,8 @@ public static void onExit(
spanContextData.put(TRACE_ID, spanContext.getTraceId());
spanContextData.put(SPAN_ID, spanContext.getSpanId());
spanContextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());

spanContextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttribute());
}

if (LogbackSingletons.addBaggage()) {
Expand Down
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<>();
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved

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);
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

private static String[] getConfiguredAttributes() {
String resourceAttributes =
ConfigPropertiesUtil.getString("otel.instrumentation.mdc.resource-attributes");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use InstrumentationConfig.get().getList("otel.instrumentation.mdc.resource-attributes", emptyList()) but InstrumentationConfig isn't probably accessible from here. InstrumentationConfig gets the properties from the sdk and could support more source than what ConfigPropertiesUtil uses. ConfigPropertiesUtil is usually used for library instrumentations and InstrumentationConfig in javaagent. Could you try moving this class to https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal so you could use InstrumentationConfig. Sorry for not noticing this before, should be the last thing from me.

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() {}
}
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(
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
"test-service", ConfiguredResourceAttributesHolder.getAttributeValue("service.name"));
}
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved

@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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader;
import io.opentelemetry.javaagent.bootstrap.BootstrapPackagePrefixesHolder;
import io.opentelemetry.javaagent.bootstrap.ClassFileTransformerHolder;
import io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder;
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper;
import io.opentelemetry.javaagent.bootstrap.InstrumentedTaskClasses;
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizer;
Expand All @@ -41,6 +42,7 @@
import io.opentelemetry.javaagent.tooling.muzzle.AgentTooling;
import io.opentelemetry.javaagent.tooling.util.Trie;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.SdkAutoconfigureAccess;
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import java.lang.instrument.Instrumentation;
Expand Down Expand Up @@ -125,6 +127,8 @@ private static void installBytebuddyAgent(
copyNecessaryConfigToSystemProperties(sdkConfig);

setBootstrapPackages(sdkConfig, extensionClassLoader);
ConfiguredResourceAttributesHolder.initialize(
SdkAutoconfigureAccess.getResourceAttributes(autoConfiguredSdk));

for (BeforeAgentListener agentListener :
loadOrdered(BeforeAgentListener.class, extensionClassLoader)) {
Expand Down
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() {}
}