Skip to content

Commit

Permalink
Factor out profiling hceck
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Jul 28, 2022
1 parent c1f0b64 commit 837c409
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
22 changes: 12 additions & 10 deletions sentry_sdk/profiler.py
Expand Up @@ -17,8 +17,8 @@
import time
from contextlib import contextmanager

import sentry_sdk
from sentry_sdk._compat import PY2
from sentry_sdk.hub import Hub
from sentry_sdk.utils import logger

if PY2:
Expand Down Expand Up @@ -193,17 +193,19 @@ def transaction_name(self):
return self._transaction.name


@contextmanager
def profiling(transaction, hub=None):
# type: (sentry_sdk.tracing.Transaction, Optional[Hub]) -> Generator[None, None, None]
def has_profiling_enabled(hub=None):
# type: (Optional[sentry_sdk.Hub]) -> bool
if hub is None:
hub = Hub.current
hub = sentry_sdk.Hub.current

options = hub.client and hub.client.options
return bool(options and options["_experiments"].get("enable_profiling"))

if (
hub.client
and hub.client.options
and hub.client.options["_experiments"].get("enable_profiling", False)
):

@contextmanager
def profiling(transaction, hub=None):
# type: (sentry_sdk.tracing.Transaction, Optional[sentry_sdk.Hub]) -> Generator[None, None, None]
if has_profiling_enabled(hub):
with Sampler(transaction):
yield
else:
Expand Down
5 changes: 3 additions & 2 deletions sentry_sdk/tracing.py
Expand Up @@ -7,6 +7,7 @@

import sentry_sdk

from sentry_sdk.profiler import has_profiling_enabled
from sentry_sdk.utils import logger
from sentry_sdk._types import MYPY

Expand Down Expand Up @@ -663,8 +664,8 @@ def finish(self, hub=None):
}

if (
hub.client is not None
and hub.client.options["_experiments"].get("enable_profiling", False)
has_profiling_enabled(hub)
and hub.client is not None
and self._profile is not None
):
event["profile"] = {
Expand Down

0 comments on commit 837c409

Please sign in to comment.