diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 1a1efa96bf7..fdb2816b3a1 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -788,6 +788,8 @@ def start_span( # pylint: disable=too-many-locals ) span.start(start_time=start_time) else: + options = context.trace_flags & ~trace_api.TraceFlags.SAMPLED + context.trace_flags = trace_api.TraceFlags(options) span = trace_api.DefaultSpan(context=context) return span diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index acc2005826e..0d63ab541e2 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -139,18 +139,20 @@ 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_flags.sampled) + self.assertTrue(root_span.get_context().trace_flags.sampled) def test_sampler_no_sampling(self): tracer_provider = trace.TracerProvider(sampling.ALWAYS_OFF) tracer = tracer_provider.get_tracer(__name__) # Check that the default tracer creates no-op spans if the sampler - # decides not to sampler + # decides not to sample root_span = tracer.start_span(name="root span", parent=None) self.assertIsInstance(root_span, trace_api.DefaultSpan) + self.assertFalse(root_span.get_context().trace_flags.sampled) child_span = tracer.start_span(name="child span", parent=root_span) self.assertIsInstance(child_span, trace_api.DefaultSpan) + self.assertFalse(child_span.get_context().trace_flags.sampled) class TestSpanCreation(unittest.TestCase):