Skip to content

Commit a5ce968

Browse files
authored
feat(profiling): Add new functions to start/stop continuous profiler (#4056)
The `start_profiler` and `stop_profiler` functions were renamed to `start_profile_session` and `stop_profile_session` respectively.
1 parent 74b3bbf commit a5ce968

File tree

2 files changed

+92
-8
lines changed

2 files changed

+92
-8
lines changed

sentry_sdk/profiler/continuous_profiler.py

+14
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ def try_profile_lifecycle_trace_start():
145145

146146
def start_profiler():
147147
# type: () -> None
148+
149+
# TODO: deprecate this as it'll be replaced by `start_profile_session`
150+
start_profile_session()
151+
152+
153+
def start_profile_session():
154+
# type: () -> None
148155
if _scheduler is None:
149156
return
150157

@@ -153,6 +160,13 @@ def start_profiler():
153160

154161
def stop_profiler():
155162
# type: () -> None
163+
164+
# TODO: deprecate this as it'll be replaced by `stop_profile_session`
165+
stop_profile_session()
166+
167+
168+
def stop_profile_session():
169+
# type: () -> None
156170
if _scheduler is None:
157171
return
158172

tests/profiler/test_continuous_profiler.py

+78-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
get_profiler_id,
1212
setup_continuous_profiler,
1313
start_profiler,
14+
start_profile_session,
1415
stop_profiler,
16+
stop_profile_session,
1517
)
1618
from tests.conftest import ApproxDict
1719

@@ -207,6 +209,21 @@ def assert_single_transaction_without_profile_chunks(envelopes):
207209
pytest.param("gevent", marks=requires_gevent),
208210
],
209211
)
212+
@pytest.mark.parametrize(
213+
["start_profiler_func", "stop_profiler_func"],
214+
[
215+
pytest.param(
216+
start_profile_session,
217+
stop_profile_session,
218+
id="start_profile_session/stop_profile_session",
219+
),
220+
pytest.param(
221+
start_profiler,
222+
stop_profiler,
223+
id="start_profiler/stop_profiler (deprecated)",
224+
),
225+
],
226+
)
210227
@pytest.mark.parametrize(
211228
"make_options",
212229
[
@@ -219,6 +236,8 @@ def test_continuous_profiler_auto_start_and_manual_stop(
219236
sentry_init,
220237
capture_envelopes,
221238
mode,
239+
start_profiler_func,
240+
stop_profiler_func,
222241
make_options,
223242
teardown_profiling,
224243
):
@@ -239,7 +258,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(
239258
assert_single_transaction_with_profile_chunks(envelopes, thread)
240259

241260
for _ in range(3):
242-
stop_profiler()
261+
stop_profiler_func()
243262

244263
envelopes.clear()
245264

@@ -249,7 +268,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(
249268

250269
assert_single_transaction_without_profile_chunks(envelopes)
251270

252-
start_profiler()
271+
start_profiler_func()
253272

254273
envelopes.clear()
255274

@@ -267,6 +286,21 @@ def test_continuous_profiler_auto_start_and_manual_stop(
267286
pytest.param("gevent", marks=requires_gevent),
268287
],
269288
)
289+
@pytest.mark.parametrize(
290+
["start_profiler_func", "stop_profiler_func"],
291+
[
292+
pytest.param(
293+
start_profile_session,
294+
stop_profile_session,
295+
id="start_profile_session/stop_profile_session",
296+
),
297+
pytest.param(
298+
start_profiler,
299+
stop_profiler,
300+
id="start_profiler/stop_profiler (deprecated)",
301+
),
302+
],
303+
)
270304
@pytest.mark.parametrize(
271305
"make_options",
272306
[
@@ -279,6 +313,8 @@ def test_continuous_profiler_manual_start_and_stop_sampled(
279313
sentry_init,
280314
capture_envelopes,
281315
mode,
316+
start_profiler_func,
317+
stop_profiler_func,
282318
make_options,
283319
teardown_profiling,
284320
):
@@ -295,7 +331,7 @@ def test_continuous_profiler_manual_start_and_stop_sampled(
295331
thread = threading.current_thread()
296332

297333
for _ in range(3):
298-
start_profiler()
334+
start_profiler_func()
299335

300336
envelopes.clear()
301337

@@ -309,7 +345,7 @@ def test_continuous_profiler_manual_start_and_stop_sampled(
309345

310346
assert get_profiler_id() is not None, "profiler should be running"
311347

312-
stop_profiler()
348+
stop_profiler_func()
313349

314350
# the profiler stops immediately in manual mode
315351
assert get_profiler_id() is None, "profiler should not be running"
@@ -332,6 +368,21 @@ def test_continuous_profiler_manual_start_and_stop_sampled(
332368
pytest.param("gevent", marks=requires_gevent),
333369
],
334370
)
371+
@pytest.mark.parametrize(
372+
["start_profiler_func", "stop_profiler_func"],
373+
[
374+
pytest.param(
375+
start_profile_session,
376+
stop_profile_session,
377+
id="start_profile_session/stop_profile_session",
378+
),
379+
pytest.param(
380+
start_profiler,
381+
stop_profiler,
382+
id="start_profiler/stop_profiler (deprecated)",
383+
),
384+
],
385+
)
335386
@pytest.mark.parametrize(
336387
"make_options",
337388
[
@@ -343,6 +394,8 @@ def test_continuous_profiler_manual_start_and_stop_unsampled(
343394
sentry_init,
344395
capture_envelopes,
345396
mode,
397+
start_profiler_func,
398+
stop_profiler_func,
346399
make_options,
347400
teardown_profiling,
348401
):
@@ -356,15 +409,15 @@ def test_continuous_profiler_manual_start_and_stop_unsampled(
356409

357410
envelopes = capture_envelopes()
358411

359-
start_profiler()
412+
start_profiler_func()
360413

361414
with sentry_sdk.start_transaction(name="profiling"):
362415
with sentry_sdk.start_span(op="op"):
363416
time.sleep(0.05)
364417

365418
assert_single_transaction_without_profile_chunks(envelopes)
366419

367-
stop_profiler()
420+
stop_profiler_func()
368421

369422

370423
@pytest.mark.parametrize(
@@ -485,6 +538,21 @@ def test_continuous_profiler_auto_start_and_stop_unsampled(
485538
),
486539
],
487540
)
541+
@pytest.mark.parametrize(
542+
["start_profiler_func", "stop_profiler_func"],
543+
[
544+
pytest.param(
545+
start_profile_session,
546+
stop_profile_session,
547+
id="start_profile_session/stop_profile_session",
548+
),
549+
pytest.param(
550+
start_profiler,
551+
stop_profiler,
552+
id="start_profiler/stop_profiler (deprecated)",
553+
),
554+
],
555+
)
488556
@pytest.mark.parametrize(
489557
"make_options",
490558
[
@@ -495,6 +563,8 @@ def test_continuous_profiler_auto_start_and_stop_unsampled(
495563
def test_continuous_profiler_manual_start_and_stop_noop_when_using_trace_lifecyle(
496564
sentry_init,
497565
mode,
566+
start_profiler_func,
567+
stop_profiler_func,
498568
class_name,
499569
make_options,
500570
teardown_profiling,
@@ -510,11 +580,11 @@ def test_continuous_profiler_manual_start_and_stop_noop_when_using_trace_lifecyl
510580
with mock.patch(
511581
f"sentry_sdk.profiler.continuous_profiler.{class_name}.ensure_running"
512582
) as mock_ensure_running:
513-
start_profiler()
583+
start_profiler_func()
514584
mock_ensure_running.assert_not_called()
515585

516586
with mock.patch(
517587
f"sentry_sdk.profiler.continuous_profiler.{class_name}.teardown"
518588
) as mock_teardown:
519-
stop_profiler()
589+
stop_profiler_func()
520590
mock_teardown.assert_not_called()

0 commit comments

Comments
 (0)