|
29 | 29 | from sentry_sdk.tracing_utils import ( |
30 | 30 | Baggage, |
31 | 31 | has_tracing_enabled, |
| 32 | + has_span_streaming_enabled, |
32 | 33 | normalize_incoming_data, |
33 | 34 | PropagationContext, |
34 | 35 | ) |
| 36 | +from sentry_sdk.traces import StreamedSpan |
35 | 37 | from sentry_sdk.tracing import ( |
36 | 38 | BAGGAGE_HEADER_NAME, |
37 | 39 | SENTRY_TRACE_HEADER_NAME, |
@@ -1278,6 +1280,17 @@ def _capture_metric(self, metric: "Optional[Metric]") -> None: |
1278 | 1280 |
|
1279 | 1281 | client._capture_metric(metric, scope=merged_scope) |
1280 | 1282 |
|
| 1283 | + def _capture_span(self, span: "Optional[StreamedSpan]") -> None: |
| 1284 | + if span is None: |
| 1285 | + return |
| 1286 | + |
| 1287 | + client = self.get_client() |
| 1288 | + if not has_span_streaming_enabled(client.options): |
| 1289 | + return |
| 1290 | + |
| 1291 | + merged_scope = self._merge_scopes() |
| 1292 | + client._capture_span(span, scope=merged_scope) |
| 1293 | + |
1281 | 1294 | def capture_message( |
1282 | 1295 | self, |
1283 | 1296 | message: str, |
@@ -1522,16 +1535,25 @@ def _apply_flags_to_event( |
1522 | 1535 | ) |
1523 | 1536 |
|
1524 | 1537 | def _apply_scope_attributes_to_telemetry( |
1525 | | - self, telemetry: "Union[Log, Metric]" |
| 1538 | + self, telemetry: "Union[Log, Metric, StreamedSpan]" |
1526 | 1539 | ) -> None: |
| 1540 | + # TODO: turn Logs, Metrics into actual classes |
| 1541 | + if isinstance(telemetry, dict): |
| 1542 | + attributes = telemetry["attributes"] |
| 1543 | + else: |
| 1544 | + attributes = telemetry._attributes |
| 1545 | + |
1527 | 1546 | for attribute, value in self._attributes.items(): |
1528 | | - if attribute not in telemetry["attributes"]: |
1529 | | - telemetry["attributes"][attribute] = value |
| 1547 | + if attribute not in attributes: |
| 1548 | + attributes[attribute] = value |
1530 | 1549 |
|
1531 | 1550 | def _apply_user_attributes_to_telemetry( |
1532 | | - self, telemetry: "Union[Log, Metric]" |
| 1551 | + self, telemetry: "Union[Log, Metric, StreamedSpan]" |
1533 | 1552 | ) -> None: |
1534 | | - attributes = telemetry["attributes"] |
| 1553 | + if isinstance(telemetry, dict): |
| 1554 | + attributes = telemetry["attributes"] |
| 1555 | + else: |
| 1556 | + attributes = telemetry._attributes |
1535 | 1557 |
|
1536 | 1558 | if not should_send_default_pii() or self._user is None: |
1537 | 1559 | return |
@@ -1651,16 +1673,19 @@ def apply_to_event( |
1651 | 1673 | return event |
1652 | 1674 |
|
1653 | 1675 | @_disable_capture |
1654 | | - def apply_to_telemetry(self, telemetry: "Union[Log, Metric]") -> None: |
| 1676 | + def apply_to_telemetry(self, telemetry: "Union[Log, Metric, StreamedSpan]") -> None: |
1655 | 1677 | # Attributes-based events and telemetry go through here (logs, metrics, |
1656 | 1678 | # spansV2) |
1657 | | - trace_context = self.get_trace_context() |
1658 | | - trace_id = trace_context.get("trace_id") |
1659 | | - if telemetry.get("trace_id") is None: |
1660 | | - telemetry["trace_id"] = trace_id or "00000000-0000-0000-0000-000000000000" |
1661 | | - span_id = trace_context.get("span_id") |
1662 | | - if telemetry.get("span_id") is None and span_id: |
1663 | | - telemetry["span_id"] = span_id |
| 1679 | + if not isinstance(telemetry, StreamedSpan): |
| 1680 | + trace_context = self.get_trace_context() |
| 1681 | + trace_id = trace_context.get("trace_id") |
| 1682 | + if telemetry.get("trace_id") is None: |
| 1683 | + telemetry["trace_id"] = ( |
| 1684 | + trace_id or "00000000-0000-0000-0000-000000000000" |
| 1685 | + ) |
| 1686 | + span_id = trace_context.get("span_id") |
| 1687 | + if telemetry.get("span_id") is None and span_id: |
| 1688 | + telemetry["span_id"] = span_id |
1664 | 1689 |
|
1665 | 1690 | self._apply_scope_attributes_to_telemetry(telemetry) |
1666 | 1691 | self._apply_user_attributes_to_telemetry(telemetry) |
|
0 commit comments