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

SDK: set sampled flag on sampling trace #407

Merged
merged 4 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ class TraceOptions(int):
def get_default(cls) -> "TraceOptions":
return cls(cls.DEFAULT)

@classmethod
def get_sampled(cls, other: "TraceOptions" = None) -> "TraceOptions":
if other is None:
return cls(cls.SAMPLED)
return cls(other | cls.SAMPLED)

@property
def sampled(self) -> bool:
return bool(self & TraceOptions.SAMPLED)
Expand Down
3 changes: 3 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,9 @@ def start_span( # pylint: disable=too-many-locals
)

if sampling_decision.sampled:
context.trace_options = trace_api.TraceOptions.get_sampled(
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
context.trace_options
)
if attributes is None:
span_attributes = sampling_decision.attributes
else:
Expand Down
2 changes: 2 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,7 @@ def test_start_span_explicit(self):
other_parent = trace_api.SpanContext(
trace_id=0x000000000000000000000000DEADBEEF,
span_id=0x00000000DEADBEF0,
trace_options=trace_api.TraceOptions.get_sampled(),
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
)

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