-
Notifications
You must be signed in to change notification settings - Fork 985
Samplers never receive the parent's tracestate, and the composite sampler erases it #5579
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
Open
dwin-gharibi
wants to merge
4
commits into
open-telemetry:main
Choose a base branch
from
dwin-gharibi:fix/sampler-tracestate-propagation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f8fc808
test(sdk): assert samplers receive the parent's tracestate
dwin-gharibi 82aeba1
fix(sdk): pass the parent's tracestate through to the sampler
dwin-gharibi cc03ce8
Merge branch 'main' into fix/sampler-tracestate-propagation
dwin-gharibi 0554c09
fix(ci): satisfy changelog and lint checks
dwin-gharibi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-sdk`: pass the parent span's `trace_state` to `Sampler.should_sample`, forward it through `ParentBased`, and stop the composite sampler discarding vendor tracestate entries | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
opentelemetry-sdk/tests/trace/test_sampler_tracestate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these tests should go inside |
||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Samplers must actually receive the parent's tracestate. | ||
|
|
||
| `Sampler.should_sample` declares a `trace_state` parameter and the consistent | ||
| probability sampling design in `_sampling_experimental` reads the parent | ||
| threshold out of it, so dropping it on the way in makes that machinery inert | ||
| and lets the composite sampler discard vendor tracestate entries. | ||
| """ | ||
|
|
||
| import unittest | ||
|
|
||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace._sampling_experimental import ( | ||
| ComposableSampler, | ||
| SamplingIntent, | ||
| composable_always_on, | ||
| composable_parent_threshold, | ||
| composite_sampler, | ||
| ) | ||
| from opentelemetry.sdk.trace._sampling_experimental._util import MIN_THRESHOLD | ||
| from opentelemetry.sdk.trace.sampling import ( | ||
| ALWAYS_ON, | ||
| Decision, | ||
| ParentBased, | ||
| Sampler, | ||
| SamplingResult, | ||
| ) | ||
| from opentelemetry.trace import TraceState, set_span_in_context | ||
| from opentelemetry.trace.propagation.tracecontext import ( | ||
| TraceContextTextMapPropagator, | ||
| ) | ||
|
|
||
| _CARRIER = { | ||
| "traceparent": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01", | ||
| "tracestate": "vendora=alpha,ot=th:8", | ||
| } | ||
|
|
||
|
|
||
| class _SpySampler(Sampler): | ||
| """Records the trace_state it was handed.""" | ||
|
|
||
| def __init__(self): | ||
| self.seen = [] | ||
|
|
||
| def should_sample( | ||
| self, | ||
| parent_context, | ||
| trace_id, | ||
| name, | ||
| kind=None, | ||
| attributes=None, | ||
| links=None, | ||
| trace_state=None, | ||
| ): | ||
| self.seen.append(trace_state) | ||
| return SamplingResult(Decision.RECORD_AND_SAMPLE, attributes, trace_state) | ||
|
|
||
| def get_description(self): | ||
| return "Spy" | ||
|
|
||
|
|
||
| def _remote_context(): | ||
| return TraceContextTextMapPropagator().extract(dict(_CARRIER)) | ||
|
|
||
|
|
||
| class TestSamplerReceivesTraceState(unittest.TestCase): | ||
| @staticmethod | ||
| def _run(sampler): | ||
| provider = TracerProvider(sampler=sampler, shutdown_on_exit=False) | ||
| provider.get_tracer(__name__).start_span("child", context=_remote_context()) | ||
| provider.shutdown() | ||
|
|
||
| def test_sampler_receives_parent_tracestate(self): | ||
| spy = _SpySampler() | ||
| self._run(spy) | ||
| self.assertIsNotNone(spy.seen[0]) | ||
| self.assertEqual(spy.seen[0].get("vendora"), "alpha") | ||
| self.assertEqual(spy.seen[0].get("ot"), "th:8") | ||
|
|
||
| def test_parent_based_forwards_tracestate_to_its_delegate(self): | ||
| spy = _SpySampler() | ||
| self._run(ParentBased(root=ALWAYS_ON, remote_parent_sampled=spy)) | ||
| self.assertIsNotNone(spy.seen[0]) | ||
| self.assertEqual(spy.seen[0].get("vendora"), "alpha") | ||
|
|
||
| def test_root_span_receives_no_tracestate(self): | ||
| """A root span has no parent, so None is correct here.""" | ||
| spy = _SpySampler() | ||
| provider = TracerProvider(sampler=spy, shutdown_on_exit=False) | ||
| provider.get_tracer(__name__).start_span("root") | ||
| provider.shutdown() | ||
| self.assertIsNone(spy.seen[0]) | ||
|
|
||
|
|
||
| class TestCompositeSamplerPreservesTraceState(unittest.TestCase): | ||
| @staticmethod | ||
| def _outgoing_tracestate(sampler): | ||
| provider = TracerProvider(sampler=sampler, shutdown_on_exit=False) | ||
| span = provider.get_tracer(__name__).start_span("child", context=_remote_context()) | ||
| carrier = {} | ||
| TraceContextTextMapPropagator().inject(carrier, context=set_span_in_context(span)) | ||
| provider.shutdown() | ||
| return carrier.get("tracestate") | ||
|
|
||
| def test_vendor_entries_survive_the_composite_sampler(self): | ||
| outgoing = self._outgoing_tracestate( | ||
| composite_sampler(composable_parent_threshold(composable_always_on())) | ||
| ) | ||
| self.assertIsNotNone(outgoing) | ||
| self.assertIn("vendora=alpha", outgoing) | ||
|
|
||
| def test_default_sampler_still_preserves_tracestate(self): | ||
| """Control: the default sampler already got this right.""" | ||
| outgoing = self._outgoing_tracestate(None) | ||
| self.assertIn("vendora=alpha", outgoing) | ||
|
|
||
|
|
||
| class TestSamplingIntentTraceStateUpdate(unittest.TestCase): | ||
| """`SamplingIntent.update_trace_state` must run for root spans too.""" | ||
|
|
||
| class _TaggingSampler(ComposableSampler): | ||
| def sampling_intent(self, parent_ctx, name, span_kind, attributes, links, trace_state): | ||
| return SamplingIntent( | ||
| threshold=MIN_THRESHOLD, | ||
| update_trace_state=lambda ts: ts.add("vendorb", "beta"), | ||
| ) | ||
|
|
||
| def get_description(self): | ||
| return "Tagging" | ||
|
|
||
| def setUp(self): | ||
| self.sampler = composite_sampler(self._TaggingSampler()) | ||
| self.trace_id = 0x0AF7651916CD43DD8448EB211C80319C | ||
|
|
||
| def test_applied_when_incoming_tracestate_is_absent(self): | ||
| result = self.sampler.should_sample(None, self.trace_id, "op", trace_state=None) | ||
| self.assertIsNotNone(result.trace_state) | ||
| self.assertEqual(result.trace_state.get("vendorb"), "beta") | ||
|
|
||
| def test_applied_when_incoming_tracestate_is_present(self): | ||
| result = self.sampler.should_sample( | ||
| None, self.trace_id, "op", trace_state=TraceState([("other", "1")]) | ||
| ) | ||
| self.assertEqual(result.trace_state.get("vendorb"), "beta") | ||
| self.assertEqual(result.trace_state.get("other"), "1") | ||
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.
Uh oh!
There was an error while loading. Please reload this page.