Skip to content

Expose baggage in log4j context data when there is no current span - #19378

Merged
laurit merged 5 commits into
open-telemetry:mainfrom
trask:trask-log4j-baggage-without-span
Aug 4, 2026
Merged

Expose baggage in log4j context data when there is no current span#19378
laurit merged 5 commits into
open-telemetry:mainfrom
trask:trask-log4j-baggage-without-span

Conversation

@trask

@trask trask commented Aug 3, 2026

Copy link
Copy Markdown
Member

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 block
  • SpanDecoratingContextDataInjector (2.7) does the same

The logback MDC instrumentation does not — in both OpenTelemetryAppender.processEvent() and LoggingEventInstrumentation, isValid() only guards the trace/span/flags entries, and baggage is added unconditionally. AbstractLogbackTest.testNoIdsWhenNoSpan already 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.testNoIdsWhenNoSpan now runs inside a baggage scope and asserts the baggage. entry, mirroring the logback test. Verified it fails on both modules without the fix.

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
Copilot AI review requested due to automatic review settings August 3, 2026 20:37
@trask
trask requested a review from a team as a code owner August 3, 2026 20:37
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 3, 2026

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-08-04 11:49 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

trask and others added 2 commits August 3, 2026 13:47
… 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 a HashMap for 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-baggage remains at its default false, this guard now falls through and copies the StringMap for every no-span log event even though no dynamic value can be added. Since this injector is on the logging hot path, include BAGGAGE_ENABLED in 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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. Since BAGGAGE_ENABLED is 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@laurit
laurit merged commit b9d751e into open-telemetry:main Aug 4, 2026
98 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants