Skip to content
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
16 changes: 13 additions & 3 deletions sentry_sdk/profiler/continuous_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ def setup_continuous_profiler(options, sdk_info, capture_func):
# type: (Dict[str, Any], SDKInfo, Callable[[Envelope], None]) -> bool
global _scheduler

if _scheduler is not None:
already_initialized = _scheduler is not None

if already_initialized:
logger.debug("[Profiling] Continuous Profiler is already setup")
return False
teardown_continuous_profiler()

if is_gevent():
# If gevent has patched the threading modules then we cannot rely on
Expand Down Expand Up @@ -117,11 +119,19 @@ def setup_continuous_profiler(options, sdk_info, capture_func):
)
)

atexit.register(teardown_continuous_profiler)
if not already_initialized:
atexit.register(teardown_continuous_profiler)

return True


def is_profile_session_sampled():
# type: () -> bool
if _scheduler is None:
return False
return _scheduler.sampled


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

Expand Down
13 changes: 10 additions & 3 deletions tests/profiler/test_continuous_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sentry_sdk
from sentry_sdk.consts import VERSION
from sentry_sdk.profiler.continuous_profiler import (
is_profile_session_sampled,
get_profiler_id,
setup_continuous_profiler,
start_profiler,
Expand Down Expand Up @@ -113,19 +114,25 @@ def test_continuous_profiler_valid_mode(mode, make_options, teardown_profiling):
],
)
def test_continuous_profiler_setup_twice(mode, make_options, teardown_profiling):
options = make_options(mode=mode)
assert not is_profile_session_sampled()

# setting up the first time should return True to indicate success
options = make_options(mode=mode, profile_session_sample_rate=1.0)
assert setup_continuous_profiler(
options,
mock_sdk_info,
lambda envelope: None,
)
# setting up the second time should return False to indicate no-op
assert not setup_continuous_profiler(
assert is_profile_session_sampled()

# setting up the second time should return True to indicate re-init
options = make_options(mode=mode, profile_session_sample_rate=0.0)
assert setup_continuous_profiler(
options,
mock_sdk_info,
lambda envelope: None,
)
assert not is_profile_session_sampled()


def assert_single_transaction_with_profile_chunks(
Expand Down