Skip to content

Commit 530a858

Browse files
fix(opentelemetry): Handle None span context
1 parent a60f8fa commit 530a858

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

sentry_sdk/integrations/opentelemetry/span_processor.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def on_start(
124124
if client.options["instrumenter"] != INSTRUMENTER.OTEL:
125125
return
126126

127-
if not otel_span.get_span_context().is_valid:
127+
span_context = otel_span.get_span_context()
128+
if span_context is None or not span_context.is_valid:
128129
return
129130

130131
if self._is_sentry_span(otel_span):
@@ -183,7 +184,7 @@ def on_end(self, otel_span: "OTelSpan") -> None:
183184
return
184185

185186
span_context = otel_span.get_span_context()
186-
if not span_context.is_valid:
187+
if span_context is None or not span_context.is_valid:
187188
return
188189

189190
span_id = format_span_id(span_context.span_id)
@@ -263,16 +264,17 @@ def _get_trace_data(
263264
trace_data: "dict[str, Any]" = {}
264265
span_context = otel_span.get_span_context()
265266

266-
span_id = format_span_id(span_context.span_id)
267-
trace_data["span_id"] = span_id
267+
if span_context is not None:
268+
span_id = format_span_id(span_context.span_id)
269+
trace_data["span_id"] = span_id
268270

269-
trace_id = format_trace_id(span_context.trace_id)
270-
trace_data["trace_id"] = trace_id
271+
trace_id = format_trace_id(span_context.trace_id)
272+
trace_data["trace_id"] = trace_id
271273

272-
parent_span_id = (
273-
format_span_id(otel_span.parent.span_id) if otel_span.parent else None
274-
)
275-
trace_data["parent_span_id"] = parent_span_id
274+
parent_span_id = (
275+
format_span_id(otel_span.parent.span_id) if otel_span.parent else None
276+
)
277+
trace_data["parent_span_id"] = parent_span_id
276278

277279
sentry_trace_data = get_value(SENTRY_TRACE_KEY, parent_context)
278280
sentry_trace_data = cast("dict[str, Union[str, bool, None]]", sentry_trace_data)

0 commit comments

Comments
 (0)