From 8f7f07a2639c216c43ec04bc59a8b90f3c46c811 Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Tue, 11 Jun 2024 14:33:10 -0400 Subject: [PATCH] fix(profiling): Move thread data to trace context The thread data was added to the profile context in #2830. It should live in the trace context to align with other SDKs. --- sentry_sdk/tracing.py | 30 +++++++++++----------- tests/profiler/test_continuous_profiler.py | 19 +++++++++----- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index abed43f26e..6747848821 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -109,10 +109,7 @@ class TransactionKwargs(SpanKwargs, total=False): "ProfileContext", { "profiler.id": str, - "thread.id": str, - "thread.name": str, }, - total=False, ) @@ -661,6 +658,19 @@ def get_trace_context(self): self.containing_transaction.get_baggage().dynamic_sampling_context() ) + data = {} + + thread_id = self._data.get(SPANDATA.THREAD_ID) + if thread_id is not None: + data["thread.id"] = thread_id + + thread_name = self._data.get(SPANDATA.THREAD_NAME) + if thread_name is not None: + data["thread.name"] = thread_name + + if data: + rv["data"] = data + return rv def get_profile_context(self): @@ -669,19 +679,9 @@ def get_profile_context(self): if profiler_id is None: return None - rv = { + return { "profiler.id": profiler_id, - } # type: ProfileContext - - thread_id = self._data.get(SPANDATA.THREAD_ID) - if thread_id is not None: - rv["thread.id"] = thread_id - - thread_name = self._data.get(SPANDATA.THREAD_NAME) - if thread_name is not None: - rv["thread.name"] = thread_name - - return rv + } class Transaction(Span): diff --git a/tests/profiler/test_continuous_profiler.py b/tests/profiler/test_continuous_profiler.py index f2e75aec5e..2fedbbdd7d 100644 --- a/tests/profiler/test_continuous_profiler.py +++ b/tests/profiler/test_continuous_profiler.py @@ -86,18 +86,25 @@ def assert_single_transaction_with_profile_chunks(envelopes, thread): assert len(items["profile_chunk"]) > 0 transaction = items["transaction"][0].payload.json - profile_context = transaction["contexts"]["profile"] - profiler_id = profile_context["profiler.id"] + trace_context = transaction["contexts"]["trace"] - assert profile_context == ApproxDict( + assert trace_context == ApproxDict( { - "profiler.id": profiler_id, - "thread.id": str(thread.ident), - "thread.name": thread.name, + "data": ApproxDict( + { + "thread.id": str(thread.ident), + "thread.name": thread.name, + } + ), } ) + profile_context = transaction["contexts"]["profile"] + profiler_id = profile_context["profiler.id"] + + assert profile_context == ApproxDict({"profiler.id": profiler_id}) + spans = transaction["spans"] assert len(spans) > 0 for span in spans: