I'm using Sentry to monitor a Python worker process that is running under some restrictions. For example it can't open files or connect to the internet. For this reason I can't use sentry_sdk.capture_exception
directly.
What I can do is put an object on a multiprocessing.Queue
to send it to the parent process which can talk to the Sentry server. So I tried doing something like this:
from sentry_sdk.utils import event_from_exception
try:
# failing code
except Exception:
exc_info = sys.exc_info()
sentry_event = event_from_exception(exc_info)
queue.put(sentry_event)
and then in the parent process sentry_sdk.capture_event(sentry_event)
.
However it turns out that sentry_event
still contains some raw Python objects (local variables) which cannot be pickled so putting it on the queue fails.
Is there a way to obtain an event where the data has already been extracted and it can easily be serialised, e.g. as JSON?
I've also asked about this here: https://github.com/getsentry/sentry-python/issues/654
Based on Markus' answer, without modifying global state:
import sentry_sdk
def get_exception_event():
event = None
def transport(e):
nonlocal event
event = e
client = sentry_sdk.Client(transport=transport)
hub = sentry_sdk.Hub(client)
hub.capture_exception()
assert event is not None
return event
try:
1 / 0
except Exception:
print(get_exception_event())