Skip to content

Commit b70a150

Browse files
authored
1 parent 26db4dd commit b70a150

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

sentry_sdk/traces.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
from typing import TYPE_CHECKING
1515

1616
import sentry_sdk
17+
from sentry_sdk.consts import SPANDATA
1718
from sentry_sdk.tracing_utils import Baggage
1819
from sentry_sdk.utils import (
1920
capture_internal_exceptions,
2021
format_attribute,
22+
get_current_thread_meta,
2123
logger,
2224
nanosecond_time,
2325
should_be_treated_as_error,
@@ -276,6 +278,8 @@ def __init__(
276278
self._status = SpanStatus.OK.value
277279
self.set_attribute("sentry.span.source", SegmentSource.CUSTOM.value)
278280

281+
self._update_active_thread()
282+
279283
self._start()
280284

281285
def __repr__(self) -> str:
@@ -450,6 +454,15 @@ def timestamp(self) -> "Optional[datetime]":
450454
def _is_segment(self) -> bool:
451455
return self._segment is self
452456

457+
def _update_active_thread(self) -> None:
458+
thread_id, thread_name = get_current_thread_meta()
459+
460+
if thread_id is not None:
461+
self.set_attribute(SPANDATA.THREAD_ID, str(thread_id))
462+
463+
if thread_name is not None:
464+
self.set_attribute(SPANDATA.THREAD_NAME, thread_name)
465+
453466

454467
class NoOpStreamedSpan(StreamedSpan):
455468
__slots__ = (

tests/tracing/test_span_streaming.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,8 @@ def test_transport_format(sentry_init, capture_envelopes):
12811281
"end_timestamp": mock.ANY,
12821282
"attributes": {
12831283
"sentry.span.source": {"value": "custom", "type": "string"},
1284-
# "thread.id": {"value": mock.ANY, "type": "string"},
1285-
# "thread.name": {"value": "MainThread", "type": "string"},
1284+
"thread.id": {"value": mock.ANY, "type": "string"},
1285+
"thread.name": {"value": "MainThread", "type": "string"},
12861286
"sentry.segment.id": {"value": mock.ANY, "type": "string"},
12871287
"sentry.segment.name": {"value": "test", "type": "string"},
12881288
"sentry.sdk.name": {"value": "sentry.python", "type": "string"},

0 commit comments

Comments
 (0)