Skip to content
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

ref(profiling): Do not error if already setup #1731

Merged
merged 1 commit into from Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 15 additions & 17 deletions sentry_sdk/profiler.py
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