Skip to content

Implement thread context sharing OTEP-4947 - #9088

Open
jhalliday wants to merge 1 commit into
open-telemetry:mainfrom
jhalliday:jh-profiling-w
Open

jhalliday wants to merge 1 commit into
open-telemetry:mainfrom
jhalliday:jh-profiling-w

Conversation

@jhalliday

Copy link
Copy Markdown
Contributor

No description provided.

@jhalliday
jhalliday requested a review from a team as a code owner December 2, 2026 12:36
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-12-02 13:08 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with the author? Comment /dashboard route:author to route it back.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.53%. Comparing base (e91fe6d) to head (0c47ede).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9088      +/-   ##
============================================
- Coverage     90.54%   90.53%   -0.01%     
+ Complexity    11254    11253       -1     
============================================
  Files          1040     1040              
  Lines         30703    30703              
  Branches       4015     4015              
============================================
- Hits          27799    27796       -3     
  Misses         1928     1928              
- Partials        976      979       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhalliday

jhalliday commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

This is the first stage of work for supporting thread context sharing in the Java SDK.

Read OTEP-4947 for background if you haven't yet.

This PR's code is deliberately incomplete, in order to keep the discussion focussed. Later steps will open the jumbo size can of worms that is building and packaging native code for multiple architectures, as well as the equally large one that is wiring and configuration, which the OTEP doesn't specify and probably should. Meanwhile there is still plenty to talk about...

This code is intended for use in environments where the SDK's ThreadLocalContextStorage is currently used, and is a drop in replacement for it. EXCEPT: it won't work with virtual threads, unless the JDK's vthread mount/unmount is patched to support it. That's out of scope at present, but achievable by the bytecode rewriting agent. It's mentioned here to address the question of if this code belongs in the SDK repo, or in the instrumentation repo. Support for reactive (thread per core) context is also out of scope, but that's true for the SDK in general too. This code also shamelessly duplicates much of ThreadLocalContextStorage, largely to avoid having to patch the existing code in a way that would change its dependencies or performance. However, it's written with an eye on the possibility of eventually merging thread context support into ThreadLocalContextStorage as it matures and use becomes more widespread.

This code uses panama FFM to access C TLS ('thread_local'), which isn't possible through a pure Java API. yes, it could use JNI instead. However, thread context relies on process context, which is also panama based. yes, this means the whole thing is Java 25+ only. It doesn't have to be and the main reason it is boils down to: we just don't want to maintain JNI code.

That said, it also uses Unsafe. What's up with that? Panama has a safe (well, less unsafe) API for working with native memory. Process Context uses it. yet here we don't. This is purely a performance choice: unlike process context, this code is on the critical path and Unsafe is a lot quicker than panama's Arena/Segment model for this use case.

There are remaining nits in the build system:
Using panama and Unsafe needs some settings that are non-default and which neither javadoc nor animal-sniffer play nice with at present.

There are remaining nits in the APIs used for integration:
Span was never intended to be read whilst in flight, so we make the simplifying assumption that all Spans are ReadableSpans. This implementation will break if that's violated, but it's sufficiently true for most use cases and my current thinking does not favor API change here.
Span and Trace ids are normally represented as Strings and most context propagation internally and externally passes them around in that form or the UTF-8 equivalent. Thread context doesn't, it uses the binary form for space saving reasons. Conversion is a large part of the code cost on the hot path and altering SpanContext to allow for storing both representations is desirable but currently out of scope.

From the perspective of providing a fully functional implementation of the OTEP, this PR would have to be considered draft - it frankly doesn't do anything useful by itself. However, I'm offering it as a self-contained step on the path, on the basis that the review will touch on distinct aspects, and involve different stake holders to, that of other steps such as the CI and security steps for building and distributing native binaries.

This branch has not been deployed

No deployments
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.

1 participant