Skip to content

Add native tracing fields to LogEvent to eliminate async MDC overhead#4171

Merged
ramanathan1504 merged 9 commits into
2.xfrom
fix-trace-metadata-1976
Jul 6, 2026
Merged

Add native tracing fields to LogEvent to eliminate async MDC overhead#4171
ramanathan1504 merged 9 commits into
2.xfrom
fix-trace-metadata-1976

Conversation

@ramanathan1504

Copy link
Copy Markdown
Contributor

Fixes #1976 by introducing a TraceContextProvider SPI and adding
native W3C tracing fields (traceId, spanId, traceFlags) directly to LogEvent.

This allows frameworks like OpenTelemetry to bypass the ThreadContext map
entirely, eliminating the map-cloning memory overhead previously
required during asynchronous logging.

@rgoers rgoers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see no reason not to do this. The implemenation looks pretty simple and clean. However, I don't see an integration with JsonTemplateLayout. Is one needed? Without that how would the data get injected into ElasticSearch?

@ramanathan1504

ramanathan1504 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Hi @rgoers, that is a great point. I hadn't included the JsonTemplateLayout integration yet, but it is definitely needed to support structured logging properly without falling back to intermediate pattern allocations.

I will open a new, follow-up PR with the native JTL resolvers (TraceIdResolver, SpanIdResolver, TraceFlagsResolver) and link it back to this one.

@ramanathan1504 ramanathan1504 merged commit b6ba7d0 into 2.x Jul 6, 2026
10 checks passed
@ramanathan1504 ramanathan1504 deleted the fix-trace-metadata-1976 branch July 6, 2026 18:28
@github-project-automation github-project-automation Bot moved this from Approved to Merged in Log4j pull request tracker Jul 6, 2026
@ramanathan1504

Copy link
Copy Markdown
Contributor Author

Hi @rgoers, the follow-up PR for the native JsonTemplateLayout resolvers is now open here: #4175 Thanks again for the guidance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

Log events should have trace/span-related metadata

2 participants