Description
If an error occurs in a user's traces_sampler
function, this can cause an unhandled exception, which may in some cases not even be reported to Sentry (see #2582).
As an example, try running the following code:
import sentry_sdk
sentry_sdk.init(
traces_sampler=lambda _: 1 / 0,
)
with sentry_sdk.start_transaction(name="test"):
print("hello, world!")
This program will crash with the ZeroDivisionError. In this case, the exception does get reported to Sentry via excepthook, but that will not always be the case (e.g. if the exception is not fatal).
When calling our other user-defined callbacks, such as before_send
or before_send_transaction
, we wrap the calls in a with capture_internal_exceptions
block. You can therefore observe that the following example simply prints "hello, world!" without any errors.
import sentry_sdk
sentry_sdk.init(
before_send_transaction=lambda event, hint: 1 / 0,
)
with sentry_sdk.start_transaction(name="test"):
print("hello, world!")
We should likely also wrap traces_sampler
calls in capture_internal_exceptions
to prevent these errors from causing crashes and other undesirable behavior.