From 029fde5d65f4d96de1cc36ad1eb09b450fceabd0 Mon Sep 17 00:00:00 2001 From: David Grochowski Date: Wed, 26 Aug 2020 20:27:10 -0400 Subject: [PATCH] Disable the sampled trace flag when a span is not sampled --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 2 ++ opentelemetry-sdk/tests/trace/test_trace.py | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 034acd0f440..63cdb1e5a55 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -831,6 +831,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 9915860cf05..bb7fed126ae 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):