diff --git a/sentry_sdk/traces.py b/sentry_sdk/traces.py index deb979ffd0..adf77a4ca8 100644 --- a/sentry_sdk/traces.py +++ b/sentry_sdk/traces.py @@ -14,10 +14,12 @@ from typing import TYPE_CHECKING import sentry_sdk +from sentry_sdk.consts import SPANDATA from sentry_sdk.tracing_utils import Baggage from sentry_sdk.utils import ( capture_internal_exceptions, format_attribute, + get_current_thread_meta, logger, nanosecond_time, should_be_treated_as_error, @@ -276,6 +278,8 @@ def __init__( self._status = SpanStatus.OK.value self.set_attribute("sentry.span.source", SegmentSource.CUSTOM.value) + self._update_active_thread() + self._start() def __repr__(self) -> str: @@ -450,6 +454,15 @@ def timestamp(self) -> "Optional[datetime]": def _is_segment(self) -> bool: return self._segment is self + def _update_active_thread(self) -> None: + thread_id, thread_name = get_current_thread_meta() + + if thread_id is not None: + self.set_attribute(SPANDATA.THREAD_ID, str(thread_id)) + + if thread_name is not None: + self.set_attribute(SPANDATA.THREAD_NAME, thread_name) + class NoOpStreamedSpan(StreamedSpan): __slots__ = ( diff --git a/tests/tracing/test_span_streaming.py b/tests/tracing/test_span_streaming.py index 7dbad08992..eb9446c1ee 100644 --- a/tests/tracing/test_span_streaming.py +++ b/tests/tracing/test_span_streaming.py @@ -1281,8 +1281,8 @@ def test_transport_format(sentry_init, capture_envelopes): "end_timestamp": mock.ANY, "attributes": { "sentry.span.source": {"value": "custom", "type": "string"}, - # "thread.id": {"value": mock.ANY, "type": "string"}, - # "thread.name": {"value": "MainThread", "type": "string"}, + "thread.id": {"value": mock.ANY, "type": "string"}, + "thread.name": {"value": "MainThread", "type": "string"}, "sentry.segment.id": {"value": mock.ANY, "type": "string"}, "sentry.segment.name": {"value": "test", "type": "string"}, "sentry.sdk.name": {"value": "sentry.python", "type": "string"},