feat: allow retrieval of Context from ContextGuard #2894
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
When attaching a context, store the context in the ContextGuard so that it can be retrieved.
Why?
I'm a maintainer of opentelemetry-php, and am prototyping using opentelemetry-rust to do all of the heavy lifting for a PHP API/SDK. One of the features from PHP's context API that I want to implement is being able to store a guard (we call it a scope), then later fetching it and operating on the context's active span.
Usage
In rust, what I want to do would look like this:
I think this change works (see added test), but running the benchmarks locally, the
context_attachtests have a regression which probably makes this PR a non-starter. I'm submitting just in case there's an optimization that I can't see, or the regression isn't as bad as it looks.Benchmarks
Running locally, compared to main:
Merge requirement checklist
CHANGELOG.mdfiles updated for non-trivial, user-facing changes