Skip to content

Commit

Permalink
feat(profiling): Support for multithreaded profiles
Browse files Browse the repository at this point in the history
A signal handler can only be installed on the main thread, this was the reason
why we could not use signals to profile multithreaded programs. This change
installs the signal handler during sdk initialization which should happen on the
main thread. The timers are still started on the individual threads to allow for
profiles being recorded from different threads.
  • Loading branch information
Zylphrex committed Aug 18, 2022
1 parent b3bd629 commit f5b789f
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 161 deletions.
1 change: 1 addition & 0 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"attachment",
"session",
"internal",
"profile",
]
SessionStatus = Literal["ok", "exited", "crashed", "abnormal"]
EndpointType = Literal["store", "envelope"]
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def capture_event(
if is_transaction:
if "profile" in event_opt:
event_opt["profile"]["transaction_id"] = event_opt["event_id"]
event_opt["profile"]["environment"] = event_opt.get("environment")
event_opt["profile"]["version_name"] = event_opt.get("release", "")
envelope.add_profile(event_opt.pop("profile"))
envelope.add_transaction(event_opt)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ def data_category(self):
return "error"
elif ty == "client_report":
return "internal"
elif ty == "profile":
return "profile"
else:
return "default"

Expand Down
14 changes: 14 additions & 0 deletions sentry_sdk/integrations/profiling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.profiler import _setup_profiler


class ProfilingIntegration(Integration):
identifier = "profiling"

@staticmethod
def setup_once():
# type: () -> None
try:
_setup_profiler()
except ValueError:
raise DidNotEnable("Profiling can only be enabled from the main thread.")
4 changes: 2 additions & 2 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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 profiling
from sentry_sdk.profiler import start_profiling

from sentry_sdk._types import MYPY

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

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

0 comments on commit f5b789f

Please sign in to comment.