|
34 | 34 | if TYPE_CHECKING: |
35 | 35 | from typing import Any, Optional, Union |
36 | 36 | from opentelemetry import context as context_api |
| 37 | + from opentelemetry.trace import SpanContext |
37 | 38 | from sentry_sdk._types import Event, Hint |
38 | 39 |
|
39 | 40 | OPEN_TELEMETRY_CONTEXT = "otel" |
@@ -131,7 +132,7 @@ def on_start( |
131 | 132 | if self._is_sentry_span(otel_span): |
132 | 133 | return |
133 | 134 |
|
134 | | - trace_data = self._get_trace_data(otel_span, parent_context) |
| 135 | + trace_data = self._get_trace_data(otel_span, span_context, parent_context) |
135 | 136 |
|
136 | 137 | parent_span_id = trace_data["parent_span_id"] |
137 | 138 | sentry_parent_span = ( |
@@ -256,25 +257,26 @@ def _get_otel_context(self, otel_span: "OTelSpan") -> "dict[str, Any]": |
256 | 257 | return ctx |
257 | 258 |
|
258 | 259 | def _get_trace_data( |
259 | | - self, otel_span: "OTelSpan", parent_context: "Optional[context_api.Context]" |
| 260 | + self, |
| 261 | + otel_span: "OTelSpan", |
| 262 | + span_context: "SpanContext", |
| 263 | + parent_context: "Optional[context_api.Context]", |
260 | 264 | ) -> "dict[str, Any]": |
261 | 265 | """ |
262 | 266 | Extracts tracing information from one OTel span and its parent OTel context. |
263 | 267 | """ |
264 | 268 | trace_data: "dict[str, Any]" = {} |
265 | | - span_context = otel_span.get_span_context() |
266 | 269 |
|
267 | | - if span_context is not None: |
268 | | - span_id = format_span_id(span_context.span_id) |
269 | | - trace_data["span_id"] = span_id |
| 270 | + span_id = format_span_id(span_context.span_id) |
| 271 | + trace_data["span_id"] = span_id |
270 | 272 |
|
271 | | - trace_id = format_trace_id(span_context.trace_id) |
272 | | - trace_data["trace_id"] = trace_id |
| 273 | + trace_id = format_trace_id(span_context.trace_id) |
| 274 | + trace_data["trace_id"] = trace_id |
273 | 275 |
|
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 |
| 276 | + parent_span_id = ( |
| 277 | + format_span_id(otel_span.parent.span_id) if otel_span.parent else None |
| 278 | + ) |
| 279 | + trace_data["parent_span_id"] = parent_span_id |
278 | 280 |
|
279 | 281 | sentry_trace_data = get_value(SENTRY_TRACE_KEY, parent_context) |
280 | 282 | sentry_trace_data = cast("dict[str, Union[str, bool, None]]", sentry_trace_data) |
|
0 commit comments