Skip to content

Commit

Permalink
SDK Tracer treats invalid span parent like null
Browse files Browse the repository at this point in the history
Fixes open-telemetry#233. The SDK tracer will now create spans with invalid parents
as brand new spans, similar to not having a parent at all.

Adding this behavior to the Tracer ensures that integrations do not have
to handle invalid span contexts in their own code, and ensures that behavior
is consistent with w3c tracecontext (which specifies invalid results should
be handled by creating new spans).
  • Loading branch information
toumorokoshi committed Oct 24, 2019
1 parent 1fd8659 commit 5441efc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def create_span(
span_id = generate_span_id()
if parent is Tracer.CURRENT_SPAN:
parent = self.get_current_span()
if parent is None:
if parent in {None, trace_api.INVALID_SPAN_CONTEXT}:
context = trace_api.SpanContext(generate_trace_id(), span_id)
else:
if isinstance(parent, trace_api.Span):
Expand Down
12 changes: 12 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ def test_extends_api(self):


class TestSpanCreation(unittest.TestCase):
def test_create_span_invalid_spancontext(self):
"""If an invalid span context is passed as the parent, the created
span should use a new span id.
"""
tracer = trace.Tracer("test_create_span_invalid_spancontext")
new_span = tracer.create_span(
"root", parent=trace_api.INVALID_SPAN_CONTEXT
)
self.assertNotEqual(
new_span.context.span_id, trace_api.INVALID_SPAN_ID
)

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

Expand Down

0 comments on commit 5441efc

Please sign in to comment.