Expose baggage in log4j context data when there is no current span - #19378
Conversation
The log4j ContextDataProvider (2.17) and ContextDataInjector (2.7) returned early when there was no valid current span, so baggage entries were dropped even though add-baggage was enabled. The logback MDC instrumentation adds baggage regardless of span validity; this aligns log4j with that behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f4f3199b-be7c-4518-a334-803ebeabaeea
Pull request dashboard statusMerged · refreshed 2026-08-04 11:49 UTC Status above doesn't look right?
|
There was a problem hiding this comment.
Pull request overview
Aligns Log4j context-data instrumentation with Logback by retaining baggage when no valid span exists.
Changes:
- Restricts span validity checks to trace identifiers.
- Preserves the empty-context fast path.
- Tests baggage propagation outside spans.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
Log4j2Test.java |
Tests baggage without an active span. |
SpanDecoratingContextDataInjector.java |
Propagates baggage independently of span validity. |
OpenTelemetryContextDataProvider.java |
Updates library-mode context data propagation. |
… name Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f4f3199b-be7c-4518-a334-803ebeabaeea
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f4f3199b-be7c-4518-a334-803ebeabaeea
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (2)
instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/log4j/contextdata/v2_17/OpenTelemetryContextDataProvider.java:70
- When baggage is present but baggage export is disabled (the default), this guard now falls through, initializes
Configuration, and allocates aHashMapfor every no-span log event although nothing will be added. Keep the empty-baggage check first to retain the restored lazy-initialization behavior, but also return early when the feature is disabled.
if (!spanContext.isValid() && baggage.isEmpty()) {
instrumentation/log4j/log4j-context-data/log4j-context-data-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/contextdata/v2_7/SpanDecoratingContextDataInjector.java:54
- When baggage is present but
add-baggageremains at its defaultfalse, this guard now falls through and copies theStringMapfor every no-span log event even though no dynamic value can be added. Since this injector is on the logging hot path, includeBAGGAGE_ENABLEDin the fast-path condition so disabled baggage preserves the prior allocation behavior.
if (!currentContext.isValid() && baggage.isEmpty()) {
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6e9dc9eb-82b7-447f-9dc2-a79570783341
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (1)
instrumentation/log4j/log4j-context-data/log4j-context-data-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/contextdata/v2_7/SpanDecoratingContextDataInjector.java:54
- [Performance]
Baggage.fromContext(context)now runs for every Log4j event even when baggage injection is disabled (the default), adding an avoidable context lookup on the logging hot path. SinceBAGGAGE_ENABLEDis already a static value in this implementation, check it before reading baggage.
Baggage baggage = Baggage.fromContext(context);
boolean addBaggage = BAGGAGE_ENABLED && !baggage.isEmpty();
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0cb73e3c-b821-4f64-9ad8-c879bbc58b56
Found while reviewing #19375.
When
otel.instrumentation.log4j-context-data.add-baggage=true, the log4j instrumentation silently drops baggage if there is no valid current span:OpenTelemetryContextDataProvider(2.17) returns early on!spanContext.isValid(), before the baggage blockSpanDecoratingContextDataInjector(2.7) does the sameThe logback MDC instrumentation does not — in both
OpenTelemetryAppender.processEvent()andLoggingEventInstrumentation,isValid()only guards the trace/span/flags entries, and baggage is added unconditionally.AbstractLogbackTest.testNoIdsWhenNoSpanalready asserts baggage is present outside a span.This aligns log4j with logback: the span validity check now guards only the trace id / span id / trace flags entries, and the early return happens only when there is neither a valid span nor any baggage (preserving the existing fast path).
Pre-existing since #8810 (2023), which added baggage support inside the region guarded by an early return introduced back in #4957.
Testing:
Log4j2Test.testNoIdsWhenNoSpannow runs inside a baggage scope and asserts thebaggage.entry, mirroring the logback test. Verified it fails on both modules without the fix.