Skip to content

Commit 7a5c168

Browse files
committed
ref: Add thread ID, name to attributes
1 parent a834583 commit 7a5c168

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
@@ -1279,8 +1279,8 @@ def test_transport_format(sentry_init, capture_envelopes):
12791279
"end_timestamp": mock.ANY,
12801280
"attributes": {
12811281
"sentry.span.source": {"value": "custom", "type": "string"},
1282-
# "thread.id": {"value": mock.ANY, "type": "string"},
1283-
# "thread.name": {"value": "MainThread", "type": "string"},
1282+
"thread.id": {"value": mock.ANY, "type": "string"},
1283+
"thread.name": {"value": "MainThread", "type": "string"},
12841284
"sentry.segment.id": {"value": mock.ANY, "type": "string"},
12851285
"sentry.segment.name": {"value": "test", "type": "string"},
12861286
"sentry.sdk.name": {"value": "sentry.python", "type": "string"},

0 commit comments

Comments
 (0)