question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

last_event_id() returns None after SentryAsgiMiddleware.__call__() finished

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
untitakercommented, Sep 9, 2020

Actually I have an idea, give me a couple hours

0reactions
kamilglodcommented, Sep 9, 2020

confirm - works fine with double SentryAsgiMiddleware middleware and this fix.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found