ocaml-opentelemetry icon indicating copy to clipboard operation
ocaml-opentelemetry copied to clipboard

``Opentelemetry_ambient_context_eio` is not able to persist data in child fibers accross domains

Open shonfeder opened this issue 3 months ago • 3 comments

Opentelemetry_ambient_context_eio uses Eio's fiber-local storage. Contrary to the documented behavior, which says

Each fiber maintains a map of additional variables associated with it, which can be used to store fiber-related state or context. This map is propagated to any forked fibers.

Fiber-local storage is not shared with forked fibers within a spawned domain. This is discussed in This is discussed in https://github.com/ocaml-multicore/eio/issues/807 .

This is not a bug with the ambient context module, per se, merely a limitation worth noting, inherited from Eio.

However, as reported by @ajbt200128, it does produce arguably buggy (at minimum, surprising) behavior when tracing, since any time a domain is spawned, computations that otherwise appear to be in a child fiber will end up with new span ID.

AFAIK, the only way for us to address without upstream changes to Eio would be reimplement the ambient context to rely on some sort of global storage backend.

I'm creating this issue to track the known limitation and as a place for further discussion.

shonfeder avatar Aug 25 '25 20:08 shonfeder

Interesting. I've had to carry around some context recently as well between async spans, so it's not just Eio.

I think there needs to be clear ways to:

  • grab the current local state (at least the current OTEL state, ideally it'd fit in a single TLS/DLS/.. local storage slot)
  • set the current state in a child fiber, from the one grabbed from the parent using the function above.

c-cube avatar Sep 05 '25 20:09 c-cube

this is essentially what we've done as a workaround in Semgrep

ajbt200128 avatar Sep 05 '25 21:09 ajbt200128

Yes it's annoying. I'm not sure how to improve on it (perhaps write a wrapper to Eio.spawn that captures the current trace context, spawns a fiber, and sets the current trace context before calling its function arguments?)

c-cube avatar Sep 08 '25 13:09 c-cube