Skip to content

Commit

Permalink
feat(profiling): Enable profiling on all transactions (#1797)
Browse files Browse the repository at this point in the history
Up to now, we've only been profiling WSGI + ASGI transactions. This change will enable profiling for all transactions.
  • Loading branch information
Zylphrex committed Jan 30, 2023
1 parent 762557a commit a51d615
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 88 deletions.
4 changes: 4 additions & 0 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sentry_sdk.consts import INSTRUMENTER
from sentry_sdk.scope import Scope
from sentry_sdk.client import Client
from sentry_sdk.profiler import Profile
from sentry_sdk.tracing import NoOpSpan, Span, Transaction
from sentry_sdk.session import Session
from sentry_sdk.utils import (
Expand Down Expand Up @@ -548,6 +549,9 @@ def start_transaction(
sampling_context.update(custom_sampling_context)
transaction._set_initial_sampling_decision(sampling_context=sampling_context)

profile = Profile(transaction, hub=self)
profile._set_initial_sampling_decision(sampling_context=sampling_context)

# we don't bother to keep spans if we already know we're not going to
# send the transaction
if transaction.sampled:
Expand Down
3 changes: 1 addition & 2 deletions sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from sentry_sdk.hub import Hub, _should_send_default_pii
from sentry_sdk.integrations._wsgi_common import _filter_headers
from sentry_sdk.integrations.modules import _get_installed_modules
from sentry_sdk.profiler import start_profiling
from sentry_sdk.sessions import auto_session_tracking
from sentry_sdk.tracing import (
SOURCE_FOR_STYLE,
Expand Down Expand Up @@ -176,7 +175,7 @@ async def _run_app(self, scope, callback):

with hub.start_transaction(
transaction, custom_sampling_context={"asgi_scope": scope}
), start_profiling(transaction, hub):
):
# XXX: Would be cool to have correct span status, but we
# would have to wrap send(). That is a bit hard to do with
# the current abstraction over ASGI 2/3.
Expand Down
3 changes: 1 addition & 2 deletions sentry_sdk/integrations/django/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import asyncio
import threading

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

with hub.configure_scope() as sentry_scope:
if sentry_scope.profile is not None:
sentry_scope.profile.active_thread_id = threading.current_thread().ident
sentry_scope.profile.update_active_thread_id()

with hub.start_span(
op=OP.VIEW_RENDER, description=request.resolver_match.view_name
Expand Down
4 changes: 1 addition & 3 deletions sentry_sdk/integrations/django/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import threading

from sentry_sdk.consts import OP
from sentry_sdk.hub import Hub
from sentry_sdk._types import MYPY
Expand Down Expand Up @@ -79,7 +77,7 @@ def sentry_wrapped_callback(request, *args, **kwargs):
# set the active thread id to the handler thread for sync views
# this isn't necessary for async views since that runs on main
if sentry_scope.profile is not None:
sentry_scope.profile.active_thread_id = threading.current_thread().ident
sentry_scope.profile.update_active_thread_id()

with hub.start_span(
op=OP.VIEW_RENDER, description=request.resolver_match.view_name
Expand Down
5 changes: 1 addition & 4 deletions sentry_sdk/integrations/fastapi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import threading

from sentry_sdk._types import MYPY
from sentry_sdk.hub import Hub, _should_send_default_pii
Expand Down Expand Up @@ -78,9 +77,7 @@ def _sentry_call(*args, **kwargs):
hub = Hub.current
with hub.configure_scope() as sentry_scope:
if sentry_scope.profile is not None:
sentry_scope.profile.active_thread_id = (
threading.current_thread().ident
)
sentry_scope.profile.update_active_thread_id()
return old_call(*args, **kwargs)

dependant.call = _sentry_call
Expand Down
5 changes: 1 addition & 4 deletions sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import asyncio
import functools
import threading

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

with hub.configure_scope() as sentry_scope:
if sentry_scope.profile is not None:
sentry_scope.profile.active_thread_id = (
threading.current_thread().ident
)
sentry_scope.profile.update_active_thread_id()

request = args[0]

Expand Down
3 changes: 1 addition & 2 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_ROUTE
from sentry_sdk.sessions import auto_session_tracking
from sentry_sdk.integrations._wsgi_common import _filter_headers
from sentry_sdk.profiler import start_profiling

from sentry_sdk._types import MYPY

Expand Down Expand Up @@ -132,7 +131,7 @@ def __call__(self, environ, start_response):

with hub.start_transaction(
transaction, custom_sampling_context={"wsgi_environ": environ}
), start_profiling(transaction, hub):
):
try:
rv = self.app(
environ,
Expand Down
Loading

0 comments on commit a51d615

Please sign in to comment.