Skip to content

SDK: set sampled flag on sampling trace #407

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

Merged
merged 4 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ def start_span( # pylint: disable=too-many-locals
)

if sampling_decision.sampled:
options = context.trace_options | trace_api.TraceOptions.SAMPLED
context.trace_options = trace_api.TraceOptions(options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, we are modifying a context here which is supposed to be immutable. Since we are the unique owner of the context here (we haven't let anyone else see it yet) that's fine, but I wonder if we should make use of __slots__ and @propertys in SpanContext.

if attributes is None:
span_attributes = sampling_decision.attributes
else:
Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def test_default_sampler(self):
self.assertIsInstance(root_span, trace.Span)
child_span = tracer.start_span(name="child span", parent=root_span)
self.assertIsInstance(child_span, trace.Span)
self.assertTrue(root_span.context.trace_options.sampled)

def test_sampler_no_sampling(self):
tracer_source = trace.TracerSource(sampling.ALWAYS_OFF)
Expand Down Expand Up @@ -251,6 +252,9 @@ def test_start_span_explicit(self):
other_parent = trace_api.SpanContext(
trace_id=0x000000000000000000000000DEADBEEF,
span_id=0x00000000DEADBEF0,
trace_options=trace_api.TraceOptions(
trace_api.TraceOptions.SAMPLED
),
)

self.assertIsNone(tracer.get_current_span())
Expand Down