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

fix: Allow overlapping context scopes #2378

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

bantonsson
Copy link
Contributor

@bantonsson bantonsson commented Dec 3, 2024

Fixes #1887

This PR is fully functional, but not complete with edge case checks, tests and comments. It's ment as a basis for discussions.

Changes

This PR changes the single current Context to a more robust stack that is resilient to out of order and overlapping Context scopes. This robustness is needed to be able to support better interop with tokio-rs/tracing.

There are some context_attach benchmarks added, and this implementation performs the same or between 3-17% faster than the old one (while maintaining a working context state).

As for the context benchmarks in opentelemetry-sdk, this implementation performs the same except for the context/is_sampled/?/alt variants where it is 6-7% slower. I haven't dug into this too much at this point.

Update 2025-02-04

Two things:

  1. I've moved out the actual code that we're benchmarking in the context benchmark into separate functions that are not inlined into the iterator loop, and there are now no differences in the context benchmark between the old way, and the new context stack.
  2. I've added an optimization of the cloning of Context, that increases the throughput of context_attach 60-80% for empty_cx, 230-330% for single_value_cx and 50-80% for span_cx. The optimization also increases the throughput of the spec versions of the context benchmark between 20-50%.

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@bantonsson bantonsson requested a review from a team as a code owner December 3, 2024 12:59
Copy link

codecov bot commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 97.77778% with 2 lines in your changes missing coverage. Please review.

Project coverage is 79.0%. Comparing base (b358202) to head (94ddd7e).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
opentelemetry/src/context.rs 97.7% 2 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main   #2378   +/-   ##
=====================================
  Coverage   79.0%   79.0%           
=====================================
  Files        120     120           
  Lines      22512   22580   +68     
=====================================
+ Hits       17791   17857   +66     
- Misses      4721    4723    +2     

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

@bantonsson bantonsson force-pushed the ban/explore-context-stack branch from b64d904 to dd46520 Compare December 3, 2024 14:55
@bantonsson bantonsson force-pushed the ban/explore-context-stack branch 2 times, most recently from ab2f259 to 4502611 Compare January 27, 2025 11:03
@bantonsson bantonsson force-pushed the ban/explore-context-stack branch 5 times, most recently from 828ba76 to ea537b8 Compare February 5, 2025 12:06
@cijothomas cijothomas added this to the 0.29 milestone Feb 8, 2025
opentelemetry/src/context.rs Outdated Show resolved Hide resolved
opentelemetry/benches/context_attach.rs Outdated Show resolved Hide resolved
opentelemetry/src/context.rs Outdated Show resolved Hide resolved
@cijothomas
Copy link
Member

@bantonsson Can you add some details on how this fixes the issue of contexts being dropped out-of-order? Perhaps some code comments will help future readers.

@bantonsson
Copy link
Contributor Author

@cijothomas I'll clean this up a bit more and add comments describing the ContextStack and how it solves the out of order closing of contexts.

@bantonsson bantonsson force-pushed the ban/explore-context-stack branch from ea537b8 to ecb0279 Compare February 12, 2025 13:30
@bantonsson bantonsson force-pushed the ban/explore-context-stack branch from ecb0279 to 94ddd7e Compare February 12, 2025 13:40
@bantonsson
Copy link
Contributor Author

@cijothomas Do you know if the integration tests are flaky? The previous run failed but now it succeeded without any functional changes to the code.

@cijothomas
Copy link
Member

@cijothomas Do you know if the integration tests are flaky? The previous run failed but now it succeeded without any functional changes to the code.

You can ignore it for now. Integration tests are not marked required for merging PRs. It does has some instability due to spinning up OTel Collector in a docker and multiple tests using the same instance etc. That will be addressed separately.

@shaun-cox
Copy link
Contributor

Interesting. I too am curious to know a bit more about tokio-rs/tracing use cases that cause the context guards to be dropped out of order.

What is the expected behavior of the context stack if new contexts are pushed during out-of-order pops?

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.

[Bug]: Overlapping contexts
5 participants