Skip to content

Commit 6463f73

Browse files
authored
fix(profiling): Re-init continuous profiler (#4772)
Re-initializing the continuous profiler should use new settings.
1 parent ca1a898 commit 6463f73

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

sentry_sdk/profiler/continuous_profiler.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ def setup_continuous_profiler(options, sdk_info, capture_func):
7575
# type: (Dict[str, Any], SDKInfo, Callable[[Envelope], None]) -> bool
7676
global _scheduler
7777

78-
if _scheduler is not None:
78+
already_initialized = _scheduler is not None
79+
80+
if already_initialized:
7981
logger.debug("[Profiling] Continuous Profiler is already setup")
80-
return False
82+
teardown_continuous_profiler()
8183

8284
if is_gevent():
8385
# If gevent has patched the threading modules then we cannot rely on
@@ -117,11 +119,19 @@ def setup_continuous_profiler(options, sdk_info, capture_func):
117119
)
118120
)
119121

120-
atexit.register(teardown_continuous_profiler)
122+
if not already_initialized:
123+
atexit.register(teardown_continuous_profiler)
121124

122125
return True
123126

124127

128+
def is_profile_session_sampled():
129+
# type: () -> bool
130+
if _scheduler is None:
131+
return False
132+
return _scheduler.sampled
133+
134+
125135
def try_autostart_continuous_profiler():
126136
# type: () -> None
127137

tests/profiler/test_continuous_profiler.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sentry_sdk
99
from sentry_sdk.consts import VERSION
1010
from sentry_sdk.profiler.continuous_profiler import (
11+
is_profile_session_sampled,
1112
get_profiler_id,
1213
setup_continuous_profiler,
1314
start_profiler,
@@ -113,19 +114,25 @@ def test_continuous_profiler_valid_mode(mode, make_options, teardown_profiling):
113114
],
114115
)
115116
def test_continuous_profiler_setup_twice(mode, make_options, teardown_profiling):
116-
options = make_options(mode=mode)
117+
assert not is_profile_session_sampled()
118+
117119
# setting up the first time should return True to indicate success
120+
options = make_options(mode=mode, profile_session_sample_rate=1.0)
118121
assert setup_continuous_profiler(
119122
options,
120123
mock_sdk_info,
121124
lambda envelope: None,
122125
)
123-
# setting up the second time should return False to indicate no-op
124-
assert not setup_continuous_profiler(
126+
assert is_profile_session_sampled()
127+
128+
# setting up the second time should return True to indicate re-init
129+
options = make_options(mode=mode, profile_session_sample_rate=0.0)
130+
assert setup_continuous_profiler(
125131
options,
126132
mock_sdk_info,
127133
lambda envelope: None,
128134
)
135+
assert not is_profile_session_sampled()
129136

130137

131138
def assert_single_transaction_with_profile_chunks(

0 commit comments

Comments
 (0)