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

Add otel4s instrumentation #13549

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iRevive
Copy link

@iRevive iRevive commented Mar 19, 2025

Note

The implementation relies on this unreleased PR in otel4s.
Hence, the CI will fail.

Hey folks.

otel4s is an OpenTelemetry library for Typelevel ecosystem. It offers Scala-friendly API and uses opentelemetry-java as a backend.

otel4s uses IOLocal under the hood to propagate context within fibers.
An instance of IOLocal can be represented as a ThreadLocal.

Unfortunately, due to various complications, we cannot use Context.current and Context.attach as a complete replacement for IOLocal.


How the instrumentation works

  • Agent instruments the IOLocalContextStorage.registerFiberThreadContext and captures the provided ThreadLocal representation of IOLocal
  • The captured ThreadLocal is stored in the global variable that exists in the bootstrap classloader (see FiberLocalContextHelper)
  • Instrumentation installs a custom ContextStorage wrapper (for the agent context storage). This wrapper uses FiberLocalContextHelper to retrieve the fiber's current context (if available)

How the context bridge works

The bridge works in the following way:

  1. current - returns fiber context if available, return agent context otherwise
  2. attach - sets the given context to both fiber's and agent contexts

While things look chaotic, the demo proves the instrumentation works.

@iRevive
Copy link
Author

iRevive commented Mar 19, 2025

Do you know if the current implementation is viable? If not, could you give me hints on how it can be improved?

@max-peroch
Copy link

Just wanted to link #10599 for reference. Can't wait to try this out @iRevive, thanks for your work!

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.

2 participants