diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index bcdd964b8d..5e89658acd 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -34,6 +34,7 @@ parse_url, parse_version, reraise, + set_thread_info_from_span, transaction_from_function, HAS_REAL_CONTEXTVARS, CONTEXTVARS_ERROR_MESSAGE, @@ -241,6 +242,8 @@ async def on_request_start(session, trace_config_ctx, params): data = { SPANDATA.HTTP_METHOD: method, } + set_thread_info_from_span(data, span) + if parsed_url is not None: data["url"] = parsed_url.url data[SPANDATA.HTTP_QUERY] = parsed_url.query diff --git a/sentry_sdk/integrations/httpx.py b/sentry_sdk/integrations/httpx.py index 988778acd0..61ce75734b 100644 --- a/sentry_sdk/integrations/httpx.py +++ b/sentry_sdk/integrations/httpx.py @@ -9,6 +9,7 @@ http_client_status_to_breadcrumb_level, logger, parse_url, + set_thread_info_from_span, ) from typing import TYPE_CHECKING @@ -65,6 +66,8 @@ def send(self, request, **kwargs): data = { SPANDATA.HTTP_METHOD: request.method, } + set_thread_info_from_span(data, span) + if parsed_url is not None: data["url"] = parsed_url.url data[SPANDATA.HTTP_QUERY] = parsed_url.query diff --git a/sentry_sdk/integrations/stdlib.py b/sentry_sdk/integrations/stdlib.py index adc0de4f28..2507eb7895 100644 --- a/sentry_sdk/integrations/stdlib.py +++ b/sentry_sdk/integrations/stdlib.py @@ -19,6 +19,7 @@ logger, safe_repr, parse_url, + set_thread_info_from_span, ) from typing import TYPE_CHECKING @@ -102,6 +103,8 @@ def putrequest(self, method, url, *args, **kwargs): data = { SPANDATA.HTTP_METHOD: method, } + set_thread_info_from_span(data, span) + if parsed_url is not None: data["url"] = parsed_url.url data[SPANDATA.HTTP_QUERY] = parsed_url.query @@ -256,8 +259,8 @@ def sentry_patched_popen_init(self, *a, **kw): thread_id, thread_name = get_current_thread_meta() breadcrumb_data = { "subprocess.pid": self.pid, - "thread.id": thread_id, - "thread.name": thread_name, + SPANDATA.THREAD_ID: thread_id, + SPANDATA.THREAD_NAME: thread_name, } if cwd: breadcrumb_data["subprocess.cwd"] = cwd diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index 92ac4d7671..20c3f3ffac 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -247,6 +247,8 @@ def __init__( if status is not None: self.set_status(status) + self.update_active_thread() + def __eq__(self, other): # type: (object) -> bool if not isinstance(other, Span): diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index bb5aaf65e2..1420f41501 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -29,6 +29,7 @@ DEFAULT_ADD_FULL_STACK, DEFAULT_MAX_STACK_FRAMES, DEFAULT_MAX_VALUE_LENGTH, + SPANDATA, EndpointType, ) from sentry_sdk._types import Annotated, AnnotatedValue, SENSITIVE_DATA_SUBSTITUTE @@ -1941,3 +1942,11 @@ def http_client_status_to_breadcrumb_level(status_code): return "warning" return "info" + + +def set_thread_info_from_span(data, span): + # type: (Dict[str, Any], sentry_sdk.tracing.Span) -> None + if span.get_attribute(SPANDATA.THREAD_ID) is not None: + data[SPANDATA.THREAD_ID] = span.get_attribute(SPANDATA.THREAD_ID) + if span.get_attribute(SPANDATA.THREAD_NAME) is not None: + data[SPANDATA.THREAD_NAME] = span.get_attribute(SPANDATA.THREAD_NAME)