-
Notifications
You must be signed in to change notification settings - Fork 457
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 Context API #98
Add Context API #98
Conversation
This provides an implementation of the [Context API Spec](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/context/context.md). A context is a propagation mechanism which carries execution-scoped values across API boundaries and between logically associated execution units. Cross-cutting concerns access their data in-process using the same shared `Context` object. It is implemented as an immutable thread-local collection of heterogeneous values that can be inserted and retrieved by their type. A thread's current context can be assigned via the `attach` method. The previous context is restored as the current context when the resulting context guard is dropped, allowing precise control and nesting of active context scopes.
Any thoughts about this context as nested thread local storage? cc @MikeGoldsmith @hawkw |
Can see examples of how it would change how active spans are tracked in sync / async code in #99. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good, thanks @jtescher.
Only query is regarding context::default
and capacity of 0.
What do you think about exposing |
Does something like https://docs.rs/opentelemetry/latest/opentelemetry/struct.Context.html#method.current work for your use case 😃 ? |
That's what we're using currently, but it clones on every access. I was referring to this comment by @jtescher
I'm happy to open a PR if that makes sense. |
@joroKr21 may be worth benchmarking your use case first to see the actual overhead of the |
Oh thanks for clarifying - I think in that case it will be fine. Sorry for not realising that on my own 🙈 |
This provides an implementation of the Context API Spec:
It is implemented as an immutable thread-local collection of heterogeneous values that can be inserted and retrieved by their type. A thread's current context can be assigned via the
attach
method. The previous context is restored as the current context when the resulting context guard is dropped, allowing precise control and nesting of active context scopes.This is the groundwork for correlation context propagation, and could also be used to replace constructs like the current span stack.
Alternative design choices
Rc
s to track values so contexts are notSend
/Sync
, could beArc
instead if contexts need to be shared.get_current
(which takes a closure with a context ref parameter) additionally or instead of the current method, would be more performant but is a bit more dangerous as attaching within that closure panics at runtime. CurrentlyContext::current_with_value(new_value).attach()
instead ofget_current(|cx| cx.with_value(new_value)).attach()
.get
andwith_value
currently take a type parameter instead of the context spec'sKey
API as this seems like the more idiomatic rust way of accomplishing the same concept.