Skip to content

Commit

Permalink
ref(profiling): Do not error if already setup (#1731)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Zylphrex authored Nov 9, 2022
1 parent e6238d8 commit 0923d03
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from sentry_sdk.utils import (
filename_for_module,
handle_in_app_impl,
logger,
nanosecond_time,
)

Expand Down Expand Up @@ -92,7 +93,6 @@
)


_sample_buffer = None # type: Optional[SampleBuffer]
_scheduler = None # type: Optional[Scheduler]


Expand All @@ -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()
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down

1 comment on commit 0923d03

@vercel
Copy link

@vercel vercel bot commented on 0923d03 Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.