Skip to content

Commit 9726fec

Browse files
committed
Handle invalid span context more gracefully
1 parent 6bf38a7 commit 9726fec

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,12 @@ def start_span(
478478
if parent_span_context is not None and not isinstance(
479479
parent_span_context, SpanContext
480480
):
481-
raise TypeError(
482-
"parent_span_context must be a SpanContext or None."
481+
logger.warning(
482+
"Invalid Span Context for %s: %s",
483+
current_span,
484+
parent_span_context,
483485
)
486+
parent_span_context = None
484487

485488
return NonRecordingSpan(context=parent_span_context)
486489

opentelemetry-api/tests/test_implementation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ def test_default_tracer_context_propagation_with_invalid_context(self):
128128
ctx = trace.set_span_in_context(
129129
RecordingSpan(context="invalid_context") # type: ignore[reportArgumentType]
130130
)
131-
with self.assertRaises(TypeError):
132-
tracer.start_span("test", context=ctx)
131+
with tracer.start_span("test", context=ctx) as span:
132+
self.assertIsNone(span.get_span_context())
133133

134134
def test_span(self):
135135
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)