Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: Added profiling to the worker thread #2439

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion sentry_sdk/worker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import os
import threading

Expand All @@ -17,6 +18,15 @@

_TERMINATOR = object()

import cProfile


class ProfiledThread(threading.Thread):
# Overrides threading.Thread.run()
def run(self):
self._anton_profiler = cProfile.Profile()
return self._anton_profiler.runcall(threading.Thread.run, self)

Check warning on line 28 in sentry_sdk/worker.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/worker.py#L27-L28

Added lines #L27 - L28 were not covered by tests


class BackgroundWorker(object):
def __init__(self, queue_size=DEFAULT_QUEUE_SIZE):
Expand Down Expand Up @@ -63,7 +73,7 @@
# type: () -> None
with self._lock:
if not self.is_alive:
self._thread = threading.Thread(
self._thread = ProfiledThread(

Check warning on line 76 in sentry_sdk/worker.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/worker.py#L76

Added line #L76 was not covered by tests
target=self._target, name="raven-sentry.BackgroundWorker"
)
self._thread.daemon = True
Expand Down Expand Up @@ -94,6 +104,10 @@
if self.is_alive and timeout > 0.0:
self._wait_flush(timeout, callback)
logger.debug("background worker flushed")
# self._thread._anton_profiler.dump_stats('%s/profiles/%s.profile' % (os.getcwd(), datetime.datetime.now().strftime("%H%M%S"),))
self._thread._anton_profiler.dump_stats(

Check warning on line 108 in sentry_sdk/worker.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/worker.py#L108

Added line #L108 was not covered by tests
"/profiledata/%s.profile" % (datetime.datetime.now().strftime("%H%M%S"),)
)

def full(self):
# type: () -> bool
Expand Down
Loading