Skip to content

Commit 9cf068b

Browse files
Make continuous profiler work in POtel span_processor (#4098)
Fixes #4063 Co-authored-by: Ivana Kellyer <[email protected]>
1 parent d5a09bc commit 9cf068b

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

sentry_sdk/integrations/opentelemetry/span_processor.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from sentry_sdk.profiler.continuous_profiler import (
1919
try_autostart_continuous_profiler,
2020
get_profiler_id,
21+
try_profile_lifecycle_trace_start,
2122
)
2223
from sentry_sdk.profiler.transaction_profiler import Profile
2324
from sentry_sdk.integrations.opentelemetry.sampler import create_sampling_context
@@ -80,7 +81,8 @@ def on_end(self, span):
8081

8182
is_root_span = not span.parent or span.parent.is_remote
8283
if is_root_span:
83-
# if have a root span ending, we build a transaction and send it
84+
# if have a root span ending, stop the profiler, build a transaction and send it
85+
self._stop_profile(span)
8486
self._flush_root_span(span)
8587
else:
8688
self._append_child_span(span)
@@ -113,6 +115,7 @@ def _add_root_span(self, span, parent_span):
113115
def _start_profile(self, span):
114116
# type: (Span) -> None
115117
try_autostart_continuous_profiler()
118+
116119
profiler_id = get_profiler_id()
117120
thread_id, thread_name = get_current_thread_meta()
118121

@@ -131,13 +134,26 @@ def _start_profile(self, span):
131134
# unix timestamp that is on span.start_time
132135
# setting it to 0 means the profiler will internally measure time on start
133136
profile = Profile(sampled, 0)
137+
134138
sampling_context = create_sampling_context(
135139
span.name, span.attributes, span.parent, span.context.trace_id
136140
)
137141
profile._set_initial_sampling_decision(sampling_context)
138142
profile.__enter__()
139143
set_sentry_meta(span, "profile", profile)
140144

145+
continuous_profile = try_profile_lifecycle_trace_start()
146+
profiler_id = get_profiler_id()
147+
if profiler_id:
148+
span.set_attribute(SPANDATA.PROFILER_ID, profiler_id)
149+
set_sentry_meta(span, "continuous_profile", continuous_profile)
150+
151+
def _stop_profile(self, span):
152+
# type: (ReadableSpan) -> None
153+
continuous_profiler = get_sentry_meta(span, "continuous_profile")
154+
if continuous_profiler:
155+
continuous_profiler.stop()
156+
141157
def _flush_root_span(self, span):
142158
# type: (ReadableSpan) -> None
143159
transaction_event = self._root_span_to_transaction_event(span)

tests/profiler/test_continuous_profiler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(
239239

240240
with sentry_sdk.start_span(name="profiling"):
241241
with sentry_sdk.start_span(op="op"):
242-
time.sleep(0.05)
242+
time.sleep(0.1)
243243

244244
assert_single_transaction_with_profile_chunks(envelopes, thread)
245245

@@ -250,7 +250,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(
250250

251251
with sentry_sdk.start_span(name="profiling"):
252252
with sentry_sdk.start_span(op="op"):
253-
time.sleep(0.05)
253+
time.sleep(0.1)
254254

255255
assert_single_transaction_without_profile_chunks(envelopes)
256256

@@ -260,7 +260,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(
260260

261261
with sentry_sdk.start_span(name="profiling"):
262262
with sentry_sdk.start_span(op="op"):
263-
time.sleep(0.05)
263+
time.sleep(0.1)
264264

265265
assert_single_transaction_with_profile_chunks(envelopes, thread)
266266

0 commit comments

Comments
 (0)