-
Notifications
You must be signed in to change notification settings - Fork 514
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
Sanic integration does not send any errors outside a request (like in a background task) #1690
Comments
How do other integrations handle exception handling outside the request/response cycle? I'm happy to work on this, but I guess the question really is what is the expected behavior for the SDK? Once an operation is outside the framework, the question becomes whether the integration should handle this or an api should be exposed for the developer to catch on their own. |
I was thinking about the same thing, since Sentry shows a lot of Request information. Then I realized it also has a Celery integration, which works outside the request process. Maybe it would be a good example? |
(I'm trying to understand what is going on at the Celery Integration. From my quick review, it wraps the call to the Task with a Sentry "kind-of" decorator. Maybe it would be interesting to do the same on any added background task?) |
Yeah, if the whole thing should be handled and transparent, I was thinking we'd monkey patch Will checkout how the other integrations handle similar issues. |
@antonpirker maybe we should also add a |
Yes, good idea. For context. We are developing a low level ayncio integration that creates spans for every background task that is created in asyncio. In this integration we also can capture exceptions. |
Ahh, okay. So this ticket should probably wait for that. |
@antonpirker Do you have an ETA on when this will be published? Not pushing or anything, just to know what to expect here. I plan to work on a wrapper this afternoon to have a quick fix until the release. I'll post the code here once it's done. |
I hope to get something out today. We have a PR ready for the change: #1695 |
Wow! That fast ?! That's awesome! Can you ping here once the SDK is released? I'll update it right away. Thank you! |
The new release is out: https://github.com/getsentry/sentry-python/releases/tag/1.10.0 You probably have to manually enable the new asyncio integration to have the errors show up. Something like this:
hope this helps |
Currently we are not auto-enabling the |
Thank you! I'll update this soon. One question; when you'll enable it automatically, will it cause problems on my code since I'm also enabling it manually? |
No |
Ok, thank you :) |
Could you test the new version and see if this fixes your problem? |
Easy! I just updated my code by adding your "AsyncioIntegration":
Ran it. I got no errors at all at Sentry :/ |
It appears that this issue is caused by an Asyncio integration bug documented in #2328 and #2333, rather than a problem with the Sanic integration. To work around the issue, whenever you are using the Sentry SDK in an async application, the Sentry SDK needs to be initialized from within an async function. We would eventually like to fix this behavior so that the Asyncio integration can still work when the SDK is initialized from outside an async function, but because fixing this bug would likely require us to rewrite significant portions of our Asyncio integration, it will likely take some time before we get around to fixing this issue. In this case (and with Sanic apps generally), you can work around the bug by initializing the SDK from within a from sanic import Sanic
import asyncio, sentry_sdk
from sentry_sdk.integrations.sanic import SanicIntegration
from sentry_sdk.integrations.asyncio import AsyncioIntegration
app = Sanic("<your_app_name>")
# ...
@app.listener("before_server_start")
async def init_sentry(_):
sentry_sdk.init(
dsn="<your_dsn>",
integrations=[SanicIntegration(), AsyncioIntegration()],
) @cnicodeme Please let us know if you have any questions, or if you encounter any difficulties while implementing the workaround. |
That's quite an interesting approach! I've done the code you recommended (well, excepted that I'm calling the before_server_start via I'll try to bring some news in a few weeks to let you know if this resolved the issue. Don't hesitate to ping me if I forgot ;) |
Just out of curiosity, is there a specific reason to use "before_server_start" instead of "main_process_start" ? |
Okay, just please make sure that your
Yes, there is -- if you use |
A month after having implemented it, I can confirm that this resolves the issue. I stopped having the I'll close this ticket. Thanks for your help! |
How do you use Sentry?
Sentry Saas (sentry.io)
Version
1.9.9
Steps to Reproduce
Sanic is using Sentry Hub by plugging the 'http.lifecycle.request' signal to start the Hub, and the 'http.lifecycle.response' to exit it.
The problem is that when errors occurs outside the HTTP lifecycle request, like in a background task, no errors are reported by Sentry/Sanic.
Here's a reproducible bug:
Of course, the same code, but with the ZeroDivisionError directly on
hello_world
properly appears at Sentry, which means the integration is working as expected when inside a request, but not when outside.I've seen with the team at Sanic, including @ahopkins about this on sanic-org/sanic#2576.
Expected Result
The exception should be logged at Sentry
Actual Result
Nothing is logged
The text was updated successfully, but these errors were encountered: