Skip to content

Commit cb89d65

Browse files
committed
Make continuous profiling work in potel
1 parent eb93c1f commit cb89d65

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
@@ -79,7 +80,8 @@ def on_end(self, span):
7980

8081
is_root_span = not span.parent or span.parent.is_remote
8182
if is_root_span:
82-
# if have a root span ending, we build a transaction and send it
83+
# if have a root span ending, stop the profiler, build a transaction and send it
84+
self._stop_profile(span)
8385
self._flush_root_span(span)
8486
else:
8587
self._append_child_span(span)
@@ -112,6 +114,7 @@ def _add_root_span(self, span, parent_span):
112114
def _start_profile(self, span):
113115
# type: (Span) -> None
114116
try_autostart_continuous_profiler()
117+
115118
profiler_id = get_profiler_id()
116119
thread_id, thread_name = get_current_thread_meta()
117120

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

144+
continuous_profile = try_profile_lifecycle_trace_start()
145+
profiler_id = get_profiler_id()
146+
if profiler_id:
147+
span.set_attribute(SPANDATA.PROFILER_ID, profiler_id)
148+
set_sentry_meta(span, "continuous_profile", continuous_profile)
149+
150+
def _stop_profile(self, span):
151+
# type: (ReadableSpan) -> None
152+
continuous_profiler = get_sentry_meta(span, "continuous_profile")
153+
if continuous_profiler:
154+
continuous_profiler.stop()
155+
140156
def _flush_root_span(self, span):
141157
# type: (ReadableSpan) -> None
142158
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
@@ -253,7 +253,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(
253253

254254
with sentry_sdk.start_span(name="profiling"):
255255
with sentry_sdk.start_span(op="op"):
256-
time.sleep(0.05)
256+
time.sleep(0.1)
257257

258258
assert_single_transaction_with_profile_chunks(envelopes, thread)
259259

@@ -264,7 +264,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(
264264

265265
with sentry_sdk.start_span(name="profiling"):
266266
with sentry_sdk.start_span(op="op"):
267-
time.sleep(0.05)
267+
time.sleep(0.1)
268268

269269
assert_single_transaction_without_profile_chunks(envelopes)
270270

@@ -274,7 +274,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(
274274

275275
with sentry_sdk.start_span(name="profiling"):
276276
with sentry_sdk.start_span(op="op"):
277-
time.sleep(0.05)
277+
time.sleep(0.1)
278278

279279
assert_single_transaction_with_profile_chunks(envelopes, thread)
280280

0 commit comments

Comments
 (0)