Skip to content

Commit

Permalink
(#9297) add new instrumentation config `otel.instrumentation.mdc.reso…
Browse files Browse the repository at this point in the history
…urce-attributes` that will be used to configure `Resource` attributes to be added to logging map diagnostic context.
  • Loading branch information
cleverchuk committed Oct 10, 2023
1 parent 4deb652 commit cf86974
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.tooling.ConfiguredResourceAttributesHolder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
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 @@ -26,6 +26,7 @@
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.tooling.ConfiguredResourceAttributesHolder;
import java.util.HashMap;
import java.util.Map;
import net.bytebuddy.asm.Advice;
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
Expand Up @@ -41,6 +41,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 +126,9 @@ private static void installBytebuddyAgent(
copyNecessaryConfigToSystemProperties(sdkConfig);

setBootstrapPackages(sdkConfig, extensionClassLoader);
ConfiguredResourceAttributesHolder.initialize(
SdkAutoconfigureAccess.getResourceAttribute(autoConfiguredSdk),
InstrumentationConfig.get());

for (BeforeAgentListener agentListener :
loadOrdered(BeforeAgentListener.class, extensionClassLoader)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static java.util.Collections.emptyList;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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, InstrumentationConfig config) {
List<String> mdcResourceAttributes =
config.getList("otel.instrumentation.mdc.resource-attributes", emptyList());

for (String key : mdcResourceAttributes) {
String value = resourceAttribute.get(stringKey(key));
if (value != null) {
ConfiguredResourceAttributesHolder.resourceAttribute.put(
String.format("otel.resource.%s", key), value);
}
}
}

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,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 getResourceAttribute(AutoConfiguredOpenTelemetrySdk sdk) {
return sdk.getResource().getAttributes();
}

private SdkAutoconfigureAccess() {}
}

0 comments on commit cf86974

Please sign in to comment.