From 0923d031e3b60f1286aa91038b17d522db05e145 Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Wed, 9 Nov 2022 11:50:23 -0500 Subject: [PATCH] ref(profiling): Do not error if already setup (#1731) We currently error if profiling is already setup which can be error prone depending on the end user's setup. This change ensures that we only setup profiling once and once setup, it's reused. --- sentry_sdk/profiler.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/sentry_sdk/profiler.py b/sentry_sdk/profiler.py index 68705cd5bc..28e96016ca 100644 --- a/sentry_sdk/profiler.py +++ b/sentry_sdk/profiler.py @@ -31,6 +31,7 @@ from sentry_sdk.utils import ( filename_for_module, handle_in_app_impl, + logger, nanosecond_time, ) @@ -92,7 +93,6 @@ ) -_sample_buffer = None # type: Optional[SampleBuffer] _scheduler = None # type: Optional[Scheduler] @@ -103,33 +103,33 @@ def setup_profiler(options): `buffer_secs` determines the max time a sample will be buffered for `frequency` determines the number of samples to take per second (Hz) """ - buffer_secs = 30 - frequency = 101 - if not PY33: - from sentry_sdk.utils import logger + global _scheduler - logger.warn("profiling is only supported on Python >= 3.3") + if _scheduler is not None: + logger.debug("profiling is already setup") return - global _sample_buffer - global _scheduler + if not PY33: + logger.warn("profiling is only supported on Python >= 3.3") + return - assert _sample_buffer is None and _scheduler is None + buffer_secs = 30 + frequency = 101 # To buffer samples for `buffer_secs` at `frequency` Hz, we need # a capcity of `buffer_secs * frequency`. - _sample_buffer = SampleBuffer(capacity=buffer_secs * frequency) + buffer = SampleBuffer(capacity=buffer_secs * frequency) profiler_mode = options["_experiments"].get("profiler_mode", SleepScheduler.mode) if profiler_mode == SigprofScheduler.mode: - _scheduler = SigprofScheduler(sample_buffer=_sample_buffer, frequency=frequency) + _scheduler = SigprofScheduler(sample_buffer=buffer, frequency=frequency) elif profiler_mode == SigalrmScheduler.mode: - _scheduler = SigalrmScheduler(sample_buffer=_sample_buffer, frequency=frequency) + _scheduler = SigalrmScheduler(sample_buffer=buffer, frequency=frequency) elif profiler_mode == SleepScheduler.mode: - _scheduler = SleepScheduler(sample_buffer=_sample_buffer, frequency=frequency) + _scheduler = SleepScheduler(sample_buffer=buffer, frequency=frequency) elif profiler_mode == EventScheduler.mode: - _scheduler = EventScheduler(sample_buffer=_sample_buffer, frequency=frequency) + _scheduler = EventScheduler(sample_buffer=buffer, frequency=frequency) else: raise ValueError("Unknown profiler mode: {}".format(profiler_mode)) _scheduler.setup() @@ -140,13 +140,11 @@ def setup_profiler(options): def teardown_profiler(): # type: () -> None - global _sample_buffer global _scheduler if _scheduler is not None: _scheduler.teardown() - _sample_buffer = None _scheduler = None @@ -728,7 +726,7 @@ def _should_profile(transaction, hub): return False # The profiler hasn't been properly initialized. - if _sample_buffer is None or _scheduler is None: + if _scheduler is None: return False hub = hub or sentry_sdk.Hub.current