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

support for adding baggage to log4j 2 ContextData #8810

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -42,6 +42,12 @@ will be added to the context when a log statement is made when a span is active:
- `span_id`
- `trace_flags`

If the `otel.instrumentation.log4j-context-data.add-baggage` system property (or the
`OTEL_INSTRUMENTATION_LOG4J_CONTEXT_DATA_ADD_BAGGAGE` environment variable) is set to `true`,
key/value pairs in [baggage](https://opentelemetry.io/docs/concepts/signals/baggage/) will be added to the context too.

- `baggage.<entry_name>`

You can use these keys when defining an appender in your `log4j.xml` configuration, for example:

```xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
import static io.opentelemetry.instrumentation.api.log.LoggingContextConstants.TRACE_FLAGS;
import static io.opentelemetry.instrumentation.api.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.instrumentation.api.internal.ConfigPropertiesUtil;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -21,6 +25,8 @@
* #supplyContextData()} is called when a log entry is created.
*/
public class OpenTelemetryContextDataProvider implements ContextDataProvider {
private static final boolean BAGGAGE_ENABLED =
ConfigPropertiesUtil.getBoolean("otel.instrumentation.log4j-context-data.add-baggage", false);

/**
* Returns context from the current span when available.
Expand All @@ -30,7 +36,8 @@ public class OpenTelemetryContextDataProvider implements ContextDataProvider {
*/
@Override
public Map<String, String> supplyContextData() {
Span currentSpan = Span.current();
Context context = Context.current();
Span currentSpan = Span.fromContext(context);
if (!currentSpan.getSpanContext().isValid()) {
return Collections.emptyMap();
}
Expand All @@ -40,6 +47,15 @@ public Map<String, String> supplyContextData() {
contextData.put(TRACE_ID, spanContext.getTraceId());
contextData.put(SPAN_ID, spanContext.getSpanId());
contextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());

if (BAGGAGE_ENABLED) {
Baggage baggage = Baggage.fromContext(context);
for (Map.Entry<String, BaggageEntry> entry : baggage.asMap().entrySet()) {
// prefix all baggage values to avoid clashes with existing context
contextData.put("baggage." + entry.getKey(), entry.getValue().getValue());
}
}

return contextData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@
import static io.opentelemetry.instrumentation.api.log.LoggingContextConstants.TRACE_FLAGS;
import static io.opentelemetry.instrumentation.api.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.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.core.ContextDataInjector;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.util.ReadOnlyStringMap;
import org.apache.logging.log4j.util.SortedArrayStringMap;
import org.apache.logging.log4j.util.StringMap;

public final class SpanDecoratingContextDataInjector implements ContextDataInjector {
private static final boolean BAGGAGE_ENABLED =
InstrumentationConfig.get()
.getBoolean("otel.instrumentation.log4j-context-data.add-baggage", false);

private final ContextDataInjector delegate;

public SpanDecoratingContextDataInjector(ContextDataInjector delegate) {
Expand All @@ -34,7 +43,9 @@ public StringMap injectContextData(List<Property> list, StringMap stringMap) {
return contextData;
}

SpanContext currentContext = Java8BytecodeBridge.currentSpan().getSpanContext();
Context context = Context.current();
Span span = Span.fromContext(context);
SpanContext currentContext = span.getSpanContext();
if (!currentContext.isValid()) {
return contextData;
}
Expand All @@ -43,6 +54,14 @@ public StringMap injectContextData(List<Property> list, StringMap stringMap) {
newContextData.putValue(TRACE_ID, currentContext.getTraceId());
newContextData.putValue(SPAN_ID, currentContext.getSpanId());
newContextData.putValue(TRACE_FLAGS, currentContext.getTraceFlags().asHex());

if (BAGGAGE_ENABLED) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add tests for the new feature? You can take a look at the logback implementation and do it in a similar way

val addBaggageTest by registering(JvmTestSuite::class) {
targets {
all {
testTask.configure {
jvmArgs("-Dotel.instrumentation.logback-mdc.add-baggage=true")
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry. I am not familiar with the unit testing modules of this project. It would be great if some volunteers could provide testing cases in future. By the way, my private built agent with this feature works well for me.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I'll create a follow-up task for that instead.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure ~

Baggage baggage = Baggage.fromContext(context);
for (Map.Entry<String, BaggageEntry> entry : baggage.asMap().entrySet()) {
// prefix all baggage values to avoid clashes with existing context
newContextData.putValue("baggage." + entry.getKey(), entry.getValue().getValue());
}
}
return newContextData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

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

import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;

public final class LogbackSingletons {
private static final boolean ADD_BAGGAGE =
ConfigPropertiesUtil.getBoolean("otel.instrumentation.logback-mdc.add-baggage", false);
InstrumentationConfig.get().getBoolean("otel.instrumentation.logback-mdc.add-baggage", false);

public static boolean addBaggage() {
return ADD_BAGGAGE;
Expand Down