Python Sentry Alerts not working

This is probably me doing something stupid but I am not getting any errors from sentry. Trying to trigger it from ipython
>
> In [1]: import sentry_sdk
> …:
>
> In [2]: sentry_sdk.init(“Sign In | Sentry”)
> …:
> Out[2]: <sentry_sdk.hub._InitGuard at 0x10a842d10>
>
> In [3]: division_by_zero = 1 / 0
> …:
> ---------------------------------------------------------------------------
> ZeroDivisionError Traceback (most recent call last)
> in
> ----> 1 division_by_zero = 1 / 0
>
> ZeroDivisionError: division by zero

We explicitly do not send any uncaught errors from the REPL to Sentry. Otherwise you would get an email for every typo you make. Try calling capture_exception or capture_message explicitly, that should always work

Calling capture_exception and capture_message directly didn’t help either.

I’m using sentry-sdk==0.14.2 and the following IPython console:
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
Type ‘copyright’, ‘credits’ or ‘license’ for more information
IPython 7.13.0 – An enhanced Interactive Python. Type ‘?’ for help.

What I observe is the following.

One statement is setup:
import sentry_sdk
from sentry_sdk import capture_exception
from sentry_sdk import capture_message
sentry_sdk.init(“https://******”, debug=True)

The followup statement is:
capture_message(‘Test message captured directly’)

and this leads to nothing.

However when I execute everything as single statement (init+capture_message) the event fires and captured on the server.

When I use legacy Raven and create client in one statement and use client to capture exception in follow-up statements - all works fine.

@oleksandr could this be a IPython speciality? How does the regular python REPL work for you?

@untitaker yes, it is IPython’s works differently.
using regular python REPL works just fine (with explicit captures of course).

I am noticing this same behaviour when using Sentry on Jupyter Notebooks. From the looks of it, the notebooks are using ipython.

Ideally, I would like to have the errors reported without having to add scaffolding to capture the exceptions.

Do you know of a way to disable this behaviour?

According to the doc, using something like this should work in jupyter notebooks:

sentry_sdk.init(..., integrations=[ExcepthookIntegration(always_run=True)])

but it’s still not working for me, does anyone have a solution?
Thanks!

This also doesn’t work for me, no matter that now is March 2022. I wrote to support - let us see what they will answer.

After some hours of digging in Sentry SDK code, I’ve developed my own Integration class which does the job.

from IPython.core.getipython import get_ipython
from sentry_sdk.hub import Hub
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
from sentry_sdk.integrations import Integration


class JupyterExcepthookIntegration(Integration):
    identifier = "jupyter_excepthook"

    @staticmethod
    def setup_once():
        get_ipython().set_custom_exc((BaseException,), _custom_exc_handler)

        
def _custom_exc_handler(shell, etype, evalue, tb, tb_offset=None):
    hub = Hub.current
    integration = hub.get_integration(JupyterExcepthookIntegration)

    if integration is not None:
        client = hub.client

        with capture_internal_exceptions():
            event, hint = event_from_exception(
                (etype, evalue, tb),
                client_options=client.options,
                mechanism={"type": "jupyter_excepthook", "handled": False},
            )
            hub.capture_event(event, hint=hint)
    
    shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset)    


import sys
import sentry_sdk
from sentry_sdk import capture_exception, capture_message, set_level, set_tag

sentry_sdk.init(
    "https://*********************.ingest.sentry.io/********",   #REPLACE THIS
    release="1.0",
    environment="local",
    sample_rate=1,
    traces_sample_rate=1.0,
    default_integrations=True,
    integrations=[JupyterExcepthookIntegration()]
)


print("Hello, World")
print(1/0)   # will raise an exception which will be written to Sentry
print("one divided by zero")