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

Allow configuring MDC key names for trace_id, span_id, trace_flags #11329

Merged
merged 22 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

package io.opentelemetry.javaagent.instrumentation.jbosslogmanager.mdc.v1_1;

import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.SPAN_ID;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_FLAGS;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_ID;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand All @@ -18,6 +15,7 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.Map;
Expand Down Expand Up @@ -57,7 +55,9 @@ public static void onExit(
@Advice.This ExtLogRecord record,
@Advice.Argument(0) String key,
@Advice.Return(readOnly = false) String value) {
if (TRACE_ID.equals(key) || SPAN_ID.equals(key) || TRACE_FLAGS.equals(key)) {
if (CommonConfig.get().getLoggingKeysTraceId().equals(key)
|| CommonConfig.get().getLoggingKeysSpanId().equals(key)
|| CommonConfig.get().getLoggingKeysTraceFlags().equals(key)) {
if (value != null) {
// Assume already instrumented event if traceId/spanId/sampled is present.
return;
Expand All @@ -72,18 +72,14 @@ public static void onExit(
return;
}

switch (key) {
case TRACE_ID:
value = spanContext.getTraceId();
break;
case SPAN_ID:
value = spanContext.getSpanId();
break;
case TRACE_FLAGS:
value = spanContext.getTraceFlags().asHex();
break;
default:
// do nothing
if (CommonConfig.get().getLoggingKeysTraceId().equals(key)) {
value = spanContext.getTraceId();
}
if (CommonConfig.get().getLoggingKeysSpanId().equals(key)) {
value = spanContext.getSpanId();
}
if (CommonConfig.get().getLoggingKeysTraceFlags().equals(key)) {
value = spanContext.getTraceFlags().asHex();
}
}
}
Expand All @@ -97,9 +93,9 @@ public static void onExit(
@Advice.This ExtLogRecord record,
@Advice.Return(readOnly = false) Map<String, String> value) {

if (value.containsKey(TRACE_ID)
&& value.containsKey(SPAN_ID)
&& value.containsKey(TRACE_FLAGS)) {
if (value.containsKey(CommonConfig.get().getLoggingKeysTraceId())
&& value.containsKey(CommonConfig.get().getLoggingKeysSpanId())
&& value.containsKey(CommonConfig.get().getLoggingKeysTraceFlags())) {
return;
}

Expand All @@ -113,16 +109,17 @@ public static void onExit(
return;
}

if (!value.containsKey(TRACE_ID)) {
value.put(TRACE_ID, spanContext.getTraceId());
if (!value.containsKey(CommonConfig.get().getLoggingKeysTraceId())) {
value.put(CommonConfig.get().getLoggingKeysTraceId(), spanContext.getTraceId());
}

if (!value.containsKey(SPAN_ID)) {
value.put(SPAN_ID, spanContext.getSpanId());
if (!value.containsKey(CommonConfig.get().getLoggingKeysSpanId())) {
value.put(CommonConfig.get().getLoggingKeysSpanId(), spanContext.getSpanId());
}

if (!value.containsKey(TRACE_FLAGS)) {
value.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
if (!value.containsKey(CommonConfig.get().getLoggingKeysTraceFlags())) {
value.put(
CommonConfig.get().getLoggingKeysTraceFlags(), spanContext.getTraceFlags().asHex());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ tasks {
filter {
excludeTestsMatching("LibraryLog4j2BaggageTest")
}
systemProperty("otel.instrumentation.common.logging.keys.trace_id", "trace_id")
systemProperty("otel.instrumentation.common.logging.keys.span_id", "span_id")
systemProperty("otel.instrumentation.common.logging.keys.trace_flags", "trace_flags")
laurit marked this conversation as resolved.
Show resolved Hide resolved
}

val testAddBaggage by registering(Test::class) {
filter {
includeTestsMatching("LibraryLog4j2BaggageTest")
}
jvmArgs("-Dotel.instrumentation.log4j-context-data.add-baggage=true")
systemProperty("otel.instrumentation.common.logging.keys.trace_id", "trace_id")
systemProperty("otel.instrumentation.common.logging.keys.span_id", "span_id")
systemProperty("otel.instrumentation.common.logging.keys.trace_flags", "trace_flags")
}

named("check") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

package io.opentelemetry.instrumentation.log4j.contextdata.v2_17;

import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.SPAN_ID;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_FLAGS;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_ID;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.baggage.BaggageEntry;
import io.opentelemetry.api.trace.Span;
Expand All @@ -30,6 +26,15 @@ public class OpenTelemetryContextDataProvider implements ContextDataProvider {
private static final boolean BAGGAGE_ENABLED =
ConfigPropertiesUtil.getBoolean("otel.instrumentation.log4j-context-data.add-baggage", false);

private static final String LOGGING_KEYS_TRACE_ID =
ConfigPropertiesUtil.getString("otel.instrumentation.common.logging.keys.trace_id");

private static final String LOGGING_KEYS_SPAN_ID =
ConfigPropertiesUtil.getString("otel.instrumentation.common.logging.keys.span_id");

private static final String LOGGING_KEYS_TRACE_FLAGS =
ConfigPropertiesUtil.getString("otel.instrumentation.common.logging.keys.trace_flags");
laurit marked this conversation as resolved.
Show resolved Hide resolved

private static final boolean configuredResourceAttributeAccessible =
isConfiguredResourceAttributeAccessible();

Expand Down Expand Up @@ -77,9 +82,9 @@ public Map<String, String> supplyContextData() {
contextData.putAll(staticContextData);

SpanContext spanContext = currentSpan.getSpanContext();
contextData.put(TRACE_ID, spanContext.getTraceId());
contextData.put(SPAN_ID, spanContext.getSpanId());
contextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
contextData.put(LOGGING_KEYS_TRACE_ID, spanContext.getTraceId());
contextData.put(LOGGING_KEYS_SPAN_ID, spanContext.getSpanId());
contextData.put(LOGGING_KEYS_TRACE_FLAGS, spanContext.getTraceFlags().asHex());

if (BAGGAGE_ENABLED) {
Baggage baggage = Baggage.fromContext(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@

package io.opentelemetry.javaagent.instrumentation.log4j.contextdata.v2_7;

import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.SPAN_ID;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_FLAGS;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_ID;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.baggage.BaggageEntry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.ConfiguredResourceAttributesHolder;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import java.util.List;
Expand Down Expand Up @@ -41,7 +38,7 @@ public SpanDecoratingContextDataInjector(ContextDataInjector delegate) {
public StringMap injectContextData(List<Property> list, StringMap stringMap) {
StringMap contextData = delegate.injectContextData(list, stringMap);

if (contextData.containsKey(TRACE_ID)) {
if (contextData.containsKey(CommonConfig.get().getLoggingKeysTraceId())) {
// Assume already instrumented event if traceId is present.
return staticContextData.isEmpty() ? contextData : newContextData(contextData);
}
Expand All @@ -54,9 +51,11 @@ public StringMap injectContextData(List<Property> list, StringMap stringMap) {
}

StringMap newContextData = newContextData(contextData);
newContextData.putValue(TRACE_ID, currentContext.getTraceId());
newContextData.putValue(SPAN_ID, currentContext.getSpanId());
newContextData.putValue(TRACE_FLAGS, currentContext.getTraceFlags().asHex());
newContextData.putValue(
CommonConfig.get().getLoggingKeysTraceId(), currentContext.getTraceId());
newContextData.putValue(CommonConfig.get().getLoggingKeysSpanId(), currentContext.getSpanId());
newContextData.putValue(
CommonConfig.get().getLoggingKeysTraceFlags(), currentContext.getTraceFlags().asHex());

if (BAGGAGE_ENABLED) {
Baggage baggage = Baggage.fromContext(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

package io.opentelemetry.javaagent.instrumentation.log4j.mdc.v1_2;

import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.SPAN_ID;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_FLAGS;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_ID;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand All @@ -18,6 +15,7 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.ConfiguredResourceAttributesHolder;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -51,7 +49,9 @@ public static void onExit(
@Advice.This LoggingEvent event,
@Advice.Argument(0) String key,
@Advice.Return(readOnly = false) Object value) {
if (TRACE_ID.equals(key) || SPAN_ID.equals(key) || TRACE_FLAGS.equals(key)) {
if (CommonConfig.get().getLoggingKeysTraceId().equals(key)
|| CommonConfig.get().getLoggingKeysSpanId().equals(key)
|| CommonConfig.get().getLoggingKeysTraceFlags().equals(key)) {
if (value != null) {
// Assume already instrumented event if traceId/spanId/sampled is present.
return;
Expand All @@ -67,18 +67,14 @@ public static void onExit(
return;
}

switch (key) {
case TRACE_ID:
value = spanContext.getTraceId();
break;
case SPAN_ID:
value = spanContext.getSpanId();
break;
case TRACE_FLAGS:
value = spanContext.getTraceFlags().asHex();
break;
default:
// do nothing
if (CommonConfig.get().getLoggingKeysTraceId().equals(key)) {
value = spanContext.getTraceId();
}
if (CommonConfig.get().getLoggingKeysSpanId().equals(key)) {
value = spanContext.getSpanId();
}
if (CommonConfig.get().getLoggingKeysTraceFlags().equals(key)) {
value = spanContext.getTraceFlags().asHex();
}
} else if (value == null) {
value = ConfiguredResourceAttributesHolder.getAttributeValue(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

package io.opentelemetry.javaagent.instrumentation.logback.mdc.v1_0;

import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.SPAN_ID;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_FLAGS;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_ID;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand All @@ -24,6 +21,7 @@
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.instrumentation.logback.mdc.v1_0.internal.UnionMap;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.ConfiguredResourceAttributesHolder;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -62,7 +60,8 @@ public static void onExit(
@Advice.This ILoggingEvent event,
@Advice.Return(typing = Typing.DYNAMIC, readOnly = false) Map<String, String> contextData) {

if (contextData != null && contextData.containsKey(TRACE_ID)) {
if (contextData != null
&& contextData.containsKey(CommonConfig.get().getLoggingKeysTraceId())) {
// Assume already instrumented event if traceId is present.
return;
}
Expand All @@ -77,9 +76,10 @@ public static void onExit(
SpanContext spanContext = Java8BytecodeBridge.spanFromContext(context).getSpanContext();

if (spanContext.isValid()) {
spanContextData.put(TRACE_ID, spanContext.getTraceId());
spanContextData.put(SPAN_ID, spanContext.getSpanId());
spanContextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
spanContextData.put(CommonConfig.get().getLoggingKeysTraceId(), spanContext.getTraceId());
spanContextData.put(CommonConfig.get().getLoggingKeysSpanId(), spanContext.getSpanId());
spanContextData.put(
CommonConfig.get().getLoggingKeysTraceFlags(), spanContext.getTraceFlags().asHex());
}
spanContextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttributes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ testing {
implementation(project(":instrumentation:logback:logback-mdc-1.0:testing"))
implementation(project(":instrumentation:logback:logback-mdc-1.0:library"))
}
targets {
all {
testTask.configure {
systemProperty("otel.instrumentation.common.logging.keys.trace_id", "trace_id")
systemProperty("otel.instrumentation.common.logging.keys.span_id", "span_id")
systemProperty("otel.instrumentation.common.logging.keys.trace_flags", "trace_flags")
laurit marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

package io.opentelemetry.instrumentation.logback.mdc.v1_0;

import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.SPAN_ID;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_FLAGS;
import static io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants.TRACE_ID;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggerContextVO;
import ch.qos.logback.classic.spi.LoggingEventVO;
Expand All @@ -20,6 +16,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import io.opentelemetry.instrumentation.logback.mdc.v1_0.internal.UnionMap;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;
Expand All @@ -33,6 +30,15 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEv

private final AppenderAttachableImpl<ILoggingEvent> aai = new AppenderAttachableImpl<>();

private static final String LOGGING_KEYS_TRACE_ID =
ConfigPropertiesUtil.getString("otel.instrumentation.common.logging.keys.trace_id");

private static final String LOGGING_KEYS_SPAN_ID =
ConfigPropertiesUtil.getString("otel.instrumentation.common.logging.keys.span_id");

private static final String LOGGING_KEYS_TRACE_FLAGS =
ConfigPropertiesUtil.getString("otel.instrumentation.common.logging.keys.trace_flags");
laurit marked this conversation as resolved.
Show resolved Hide resolved

/**
* When set to true this will enable addition of all baggage entries to MDC. This can be done by
* adding the following to the logback.xml config for this appender. {@code
Expand All @@ -46,7 +52,7 @@ public void setAddBaggage(boolean addBaggage) {

public ILoggingEvent wrapEvent(ILoggingEvent event) {
Map<String, String> eventContext = event.getMDCPropertyMap();
if (eventContext != null && eventContext.containsKey(TRACE_ID)) {
if (eventContext != null && eventContext.containsKey(LOGGING_KEYS_TRACE_ID)) {
// Assume already instrumented event if traceId is present.
return event;
}
Expand All @@ -57,9 +63,9 @@ public ILoggingEvent wrapEvent(ILoggingEvent event) {

if (currentSpan.getSpanContext().isValid()) {
SpanContext spanContext = currentSpan.getSpanContext();
contextData.put(TRACE_ID, spanContext.getTraceId());
contextData.put(SPAN_ID, spanContext.getSpanId());
contextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
contextData.put(LOGGING_KEYS_TRACE_ID, spanContext.getTraceId());
contextData.put(LOGGING_KEYS_SPAN_ID, spanContext.getSpanId());
contextData.put(LOGGING_KEYS_TRACE_FLAGS, spanContext.getTraceFlags().asHex());
}

if (addBaggage) {
Expand Down
Loading
Loading