8000 feat(profiling): Add new functions to start/stop continuous profiler by Zylphrex · Pull Request #4056 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

feat(profiling): Add new functions to start/stop continuous profiler #4056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions sentry_sdk/profiler/continuous_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ def try_profile_lifecycle_trace_start():

def start_profiler():
# type: () -> None

# TODO: deprecate this as it'll be replaced by `start_profile_session`
start_profile_session()


def start_profile_session():
# type: () -> None
if _scheduler is None:
return

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

def stop_profiler():
# type: () -> None

# TODO: deprecate this as it'll be replaced by `stop_profile_session`
stop_profile_session()


def stop_profile_session():
# type: () -> None
if _scheduler is None:
return

Expand Down
86 changes: 78 additions & 8 deletions tests/profiler/test_continuous_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
get_profiler_id,
setup_continuous_profiler,
start_profiler,
start_profile_session,
stop_profiler,
stop_profile_session,
)
from tests.conftest import ApproxDict

Expand Down Expand Up @@ -207,6 +209,21 @@ def assert_single_transaction_without_profile_chunks(envelopes):
pytest.param("gevent", marks=requires_gevent),
],
)
@pytest.mark.parametrize(
["start_profiler_func", "stop_profiler_func"],
[
pytest.param(
start_profile_session,
stop_profile_session,
id="start_profile_session/stop_profile_session",
),
pytest.param(
start_profiler,
stop_profiler,
id="start_profiler/stop_profiler (deprecated)",
),
],
)
@pytest.mark.parametrize(
"make_options",
[
Expand All @@ -219,6 +236,8 @@ def test_continuous_profiler_auto_start_and_manual_stop(
sentry_init,
capture_envelopes,
mode,
start_profiler_func,
stop_profiler_func,
make_options,
teardown_profiling,
):
Expand All @@ -239,7 +258,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(
assert_single_transaction_with_profile_chunks(envelopes, thread)

for _ in range(3):
stop_profiler()
stop_profiler_func()

envelopes.clear()

Expand All @@ -249,7 +268,7 @@ def test_continuous_profiler_auto_start_and_manual_stop(

assert_single_transaction_without_profile_chunks(envelopes)

start_profiler()
start_profiler_func()

envelopes.clear()

Expand All @@ -267,6 +286,21 @@ def test_continuous_profiler_auto_start_and_manual_stop(
pytest.param("gevent", marks=requires_gevent),
],
)
@pytest.mark.parametrize(
["start_profiler_func", "stop_profiler_func"],
[
pytest.param(
start_profile_session,
stop_profile_session,
id="start_profile_session/stop_profile_session",
),
pytest.param(
start_profiler,
stop_profiler,
id="start_profiler/stop_profiler (deprecated)",
),
],
)
@pytest.mark.parametrize(
"make_options",
[
Expand All @@ -279,6 +313,8 @@ def test_continuous_profiler_manual_start_and_stop_sampled(
sentry_init,
capture_envelopes,
mode,
start_profiler_func,
stop_profiler_func,
make_options,
teardown_profiling,
):
Expand All @@ -295,7 +331,7 @@ def test_continuous_profiler_manual_start_and_stop_sampled(
thread = threading.current_thread()

for _ in range(3):
start_profiler()
start_profiler_func()

envelopes.clear()

Expand All @@ -309,7 +345,7 @@ def test_continuous_profiler_manual_start_and_stop_sampled(

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

stop_profiler()
stop_profiler_func()

# the profiler stops immediately in manual mode
assert get_profiler_id() is None, "profiler should not be running"
Expand All @@ -332,6 +368,21 @@ def test_continuous_profiler_manual_start_and_stop_sampled(
pytest.param("gevent", marks=requires_gevent),
],
)
@pytest.mark.parametrize(
["start_profiler_func", "stop_profiler_func"],
[
pytest.param(
start_profile_session,
stop_profile_session,
id="start_profile_session/stop_profile_session",
),
pytest.param(
start_profiler,
stop_profiler,
id="start_profiler/stop_profiler (deprecated)",
),
],
)
@pytest.mark.parametrize(
"make_options",
[
Expand All @@ -343,6 +394,8 @@ def test_continuous_profiler_manual_start_and_stop_unsampled(
sentry_init,
capture_envelopes,
mode,
start_profiler_func,
stop_profiler_func,
make_options,
teardown_profiling,
):
Expand All @@ -356,15 +409,15 @@ def test_continuous_profiler_manual_start_and_stop_unsampled(

envelopes = capture_envelopes()

start_profiler()
start_profiler_func()

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

assert_single_transaction_without_profile_chunks(envelopes)

stop_profiler()
stop_profiler_func()


@pytest.mark.parametrize(
Expand Down Expand Up @@ -485,6 +538,21 @@ def test_continuous_profiler_auto_start_and_stop_unsampled(
),
],
)
@pytest.mark.parametrize(
["start_profiler_func", "stop_profiler_func"],
[
pytest.param(
start_profile_session,
stop_profile_session,
id="start_profile_session/stop_profile_session",
),
pytest.param(
start_profiler,
stop_profiler,
id="start_profiler/stop_profiler (deprecated)",
),
],
)
@pytest.mark.parametrize(
"make_options",
[
Expand All @@ -495,6 +563,8 @@ def test_continuous_profiler_auto_start_and_stop_unsampled(
def test_continuous_profiler_manual_start_and_stop_noop_when_using_trace_lifecyle(
sentry_init,
mode,
start_profiler_func,
stop_profiler_func,
class_name,
make_options,
teardown_profiling,
Expand All @@ -510,11 +580,11 @@ def test_continuous_profiler_manual_start_and_stop_noop_when_using_trace_lifecyl
with mock.patch(
f"sentry_sdk.profiler.continuous_profiler.{class_name}.ensure_running"
) as mock_ensure_running:
start_profiler()
start_profiler_func()
mock_ensure_running.assert_not_called()

with mock.patch(
f"sentry_sdk.profiler.continuous_profiler.{class_name}.teardown"
) as mock_teardown:
stop_profiler()
stop_profiler_func()
mock_teardown.assert_not_called()
Loading
0