Skip to content

Commit

Permalink
Move context manager to profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Jul 28, 2022
1 parent d1ca7d9 commit c1f0b64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
18 changes: 1 addition & 17 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import sys
import sentry_sdk.profiler as profiler

from contextlib import contextmanager
from sentry_sdk._functools import partial
from sentry_sdk.hub import Hub, _should_send_default_pii
from sentry_sdk.utils import (
Expand All @@ -13,6 +11,7 @@
from sentry_sdk.tracing import Transaction
from sentry_sdk.sessions import auto_session_tracking
from sentry_sdk.integrations._wsgi_common import _filter_headers
from sentry_sdk.profiler import profiling

from sentry_sdk._types import MYPY

Expand All @@ -25,7 +24,6 @@
from typing import Optional
from typing import TypeVar
from typing import Protocol
from typing import Generator

from sentry_sdk.utils import ExcInfo
from sentry_sdk._types import EventProcessor
Expand Down Expand Up @@ -96,20 +94,6 @@ def get_request_url(environ, use_x_forwarded_for=False):
)


@contextmanager
def profiling(transaction, hub=None):
# type: (Transaction, Optional[Hub]) -> Generator[None, None, None]
if hub is None:
hub = Hub.current
if hub.client is not None and hub.client.options["_experiments"].get(
"enable_profiling", False
):
with profiler.Sampler(transaction):
yield
else:
yield


class SentryWsgiMiddleware(object):
__slots__ = ("app", "use_x_forwarded_for")

Expand Down
22 changes: 22 additions & 0 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import atexit
import signal
import time
from contextlib import contextmanager

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

if PY2:
Expand All @@ -27,6 +30,8 @@

if MYPY:
import typing
from typing import Generator
from typing import Optional
import sentry_sdk.tracing


Expand Down Expand Up @@ -186,3 +191,20 @@ def stop(self):
def transaction_name(self):
# type: () -> str
return self._transaction.name


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

if (
hub.client
and hub.client.options
and hub.client.options["_experiments"].get("enable_profiling", False)
):
with Sampler(transaction):
yield
else:
yield

0 comments on commit c1f0b64

Please sign in to comment.