Skip to content

Commit 2c87e44

Browse files
committed
Addressing review feedback
Tracer.create_span now checks is_valid for validity of the SpanContext. This is more comprehensive than a simple identity check for INVALID_SPAN_CONTEXT. Setting the parent to none if the parent context is invalid, reducing logic to handle that situation in downstream processing like exporters.
1 parent 5441efc commit 2c87e44

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -364,21 +364,29 @@ def create_span(
364364
span_id = generate_span_id()
365365
if parent is Tracer.CURRENT_SPAN:
366366
parent = self.get_current_span()
367-
if parent in {None, trace_api.INVALID_SPAN_CONTEXT}:
368-
context = trace_api.SpanContext(generate_trace_id(), span_id)
367+
368+
parent_context = parent
369+
if isinstance(parent_context, trace_api.Span):
370+
parent_context = parent.get_context()
371+
372+
if parent_context is not None and not isinstance(
373+
parent_context, trace_api.SpanContext
374+
):
375+
raise TypeError
376+
377+
if parent_context is None or not parent_context.is_valid():
378+
parent = None
379+
trace_id = generate_trace_id()
380+
trace_options = None
381+
trace_state = None
369382
else:
370-
if isinstance(parent, trace_api.Span):
371-
parent_context = parent.get_context()
372-
elif isinstance(parent, trace_api.SpanContext):
373-
parent_context = parent
374-
else:
375-
raise TypeError
376-
context = trace_api.SpanContext(
377-
parent_context.trace_id,
378-
span_id,
379-
parent_context.trace_options,
380-
parent_context.trace_state,
381-
)
383+
trace_id = parent_context.trace_id
384+
trace_options = parent_context.trace_options
385+
trace_state = parent_context.trace_state
386+
387+
context = trace_api.SpanContext(
388+
trace_id, span_id, trace_options, trace_state
389+
)
382390
return Span(
383391
name=name,
384392
context=context,

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ class TestSpanCreation(unittest.TestCase):
3030
def test_create_span_invalid_spancontext(self):
3131
"""If an invalid span context is passed as the parent, the created
3232
span should use a new span id.
33+
34+
Invalid span contexts should also not be added as a parent. This
35+
eliminates redundant error handling logic in exporters.
3336
"""
3437
tracer = trace.Tracer("test_create_span_invalid_spancontext")
3538
new_span = tracer.create_span(
3639
"root", parent=trace_api.INVALID_SPAN_CONTEXT
3740
)
38-
self.assertNotEqual(
39-
new_span.context.span_id, trace_api.INVALID_SPAN_ID
40-
)
41+
self.assertTrue(new_span.context.is_valid())
42+
self.assertIsNone(new_span.parent)
4143

4244
def test_start_span_implicit(self):
4345
tracer = trace.Tracer("test_start_span_implicit")

0 commit comments

Comments
 (0)