last_event_id() returns None after SentryAsgiMiddleware.__call__() finished
See original GitHub issueI’m unable to access last_event_id
of SentryAsgiMiddleware
on exception handler in Starlette
framework.
from sentry_sdk import last_event_id
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
async def test_endpoint(request):
raise RuntimeError("test")
def exception_handler(*args, **kwargs):
return JSONResponse({"last_event_id": last_event_id()})
app = Starlette(
routes=[Route('/', test_endpoint)],
exception_handlers={
Exception: exception_handler,
}
)
app.add_middleware(SentryAsgiMiddleware)
the problem is probably with usage of Hub’s context manager in SentryAsgiMiddleware._run_app()
- after throwing exception you are clearing local ContextVar
so last_event_id
function tries to access wrong Hub instance.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Browser refresh not sending the last-event-id received as part ...
I'm trying to read it using request.headers.get('last-event-id'), but this is always null. Any pointers on how to get the last event id would...
Read more >Main API — sentry-python 1.12.1 documentation - GitHub Pages
If not using context managers, call the .finish() method. When the transaction is finished, it will be sent to Sentry with all its...
Read more >sentry Changelog - PyUp.io
Transaction name is reset after the transaction finishes ([1125](https://github.com/getsentry/sentry-dart/pull/1125)) Dependencies
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Actually I have an idea, give me a couple hours
confirm - works fine with double
SentryAsgiMiddleware
middleware and this fix.