Skip to content

Commit a51d615

Browse files
authored
feat(profiling): Enable profiling on all transactions (#1797)
Up to now, we've only been profiling WSGI + ASGI transactions. This change will enable profiling for all transactions.
1 parent 762557a commit a51d615

File tree

13 files changed

+292
-88
lines changed

13 files changed

+292
-88
lines changed

sentry_sdk/hub.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from sentry_sdk.consts import INSTRUMENTER
99
from sentry_sdk.scope import Scope
1010
from sentry_sdk.client import Client
11+
from sentry_sdk.profiler import Profile
1112
from sentry_sdk.tracing import NoOpSpan, Span, Transaction
1213
from sentry_sdk.session import Session
1314
from sentry_sdk.utils import (
@@ -548,6 +549,9 @@ def start_transaction(
548549
sampling_context.update(custom_sampling_context)
549550
transaction._set_initial_sampling_decision(sampling_context=sampling_context)
550551

552+
profile = Profile(transaction, hub=self)
553+
profile._set_initial_sampling_decision(sampling_context=sampling_context)
554+
551555
# we don't bother to keep spans if we already know we're not going to
552556
# send the transaction
553557
if transaction.sampled:

sentry_sdk/integrations/asgi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from sentry_sdk.hub import Hub, _should_send_default_pii
1515
from sentry_sdk.integrations._wsgi_common import _filter_headers
1616
from sentry_sdk.integrations.modules import _get_installed_modules
17-
from sentry_sdk.profiler import start_profiling
1817
from sentry_sdk.sessions import auto_session_tracking
1918
from sentry_sdk.tracing import (
2019
SOURCE_FOR_STYLE,
@@ -176,7 +175,7 @@ async def _run_app(self, scope, callback):
176175

177176
with hub.start_transaction(
178177
transaction, custom_sampling_context={"asgi_scope": scope}
179-
), start_profiling(transaction, hub):
178+
):
180179
# XXX: Would be cool to have correct span status, but we
181180
# would have to wrap send(). That is a bit hard to do with
182181
# the current abstraction over ASGI 2/3.

sentry_sdk/integrations/django/asgi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""
88

99
import asyncio
10-
import threading
1110

1211
from sentry_sdk import Hub, _functools
1312
from sentry_sdk._types import MYPY
@@ -92,7 +91,7 @@ async def sentry_wrapped_callback(request, *args, **kwargs):
9291

9392
with hub.configure_scope() as sentry_scope:
9493
if sentry_scope.profile is not None:
95-
sentry_scope.profile.active_thread_id = threading.current_thread().ident
94+
sentry_scope.profile.update_active_thread_id()
9695

9796
with hub.start_span(
9897
op=OP.VIEW_RENDER, description=request.resolver_match.view_name

sentry_sdk/integrations/django/views.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import threading
2-
31
from sentry_sdk.consts import OP
42
from sentry_sdk.hub import Hub
53
from sentry_sdk._types import MYPY
@@ -79,7 +77,7 @@ def sentry_wrapped_callback(request, *args, **kwargs):
7977
# set the active thread id to the handler thread for sync views
8078
# this isn't necessary for async views since that runs on main
8179
if sentry_scope.profile is not None:
82-
sentry_scope.profile.active_thread_id = threading.current_thread().ident
80+
sentry_scope.profile.update_active_thread_id()
8381

8482
with hub.start_span(
8583
op=OP.VIEW_RENDER, description=request.resolver_match.view_name

sentry_sdk/integrations/fastapi.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import threading
32

43
from sentry_sdk._types import MYPY
54
from sentry_sdk.hub import Hub, _should_send_default_pii
@@ -78,9 +77,7 @@ def _sentry_call(*args, **kwargs):
7877
hub = Hub.current
7978
with hub.configure_scope() as sentry_scope:
8079
if sentry_scope.profile is not None:
81-
sentry_scope.profile.active_thread_id = (
82-
threading.current_thread().ident
83-
)
80+
sentry_scope.profile.update_active_thread_id()
8481
return old_call(*args, **kwargs)
8582

8683
dependant.call = _sentry_call

sentry_sdk/integrations/starlette.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import asyncio
44
import functools
5-
import threading
65

76
from sentry_sdk._compat import iteritems
87
from sentry_sdk._types import MYPY
@@ -413,9 +412,7 @@ def _sentry_sync_func(*args, **kwargs):
413412

414413
with hub.configure_scope() as sentry_scope:
415414
if sentry_scope.profile is not None:
416-
sentry_scope.profile.active_thread_id = (
417-
threading.current_thread().ident
418-
)
415+
sentry_scope.profile.update_active_thread_id()
419416

420417
request = args[0]
421418

sentry_sdk/integrations/wsgi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_ROUTE
1313
from sentry_sdk.sessions import auto_session_tracking
1414
from sentry_sdk.integrations._wsgi_common import _filter_headers
15-
from sentry_sdk.profiler import start_profiling
1615

1716
from sentry_sdk._types import MYPY
1817

@@ -132,7 +131,7 @@ def __call__(self, environ, start_response):
132131

133132
with hub.start_transaction(
134133
transaction, custom_sampling_context={"wsgi_environ": environ}
135-
), start_profiling(transaction, hub):
134+
):
136135
try:
137136
rv = self.app(
138137
environ,

0 commit comments

Comments
 (0)