Skip to content

Commit

Permalink
Wrap asgi application to ignore lifetime requests
Browse files Browse the repository at this point in the history
ASGI servers send "lifetime" requests to django at startup and shutdown.

The ASGI spec allows these to fail, in which case the server ignores them, however
the way django fails them sends an exception to sentry which we don't want.

Overwrite the handler to return a 501 error without throwing an exception
  • Loading branch information
frankh committed Apr 25, 2024
1 parent 06fe778 commit 4c914e3
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion posthog/asgi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import os

from django.core.asgi import get_asgi_application
from django.http.response import HttpResponse

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "posthog.settings")
os.environ.setdefault("SERVER_GATEWAY_INTERFACE", "ASGI")

application = get_asgi_application()

# Django doesn't support lifetime requests and raises an exception
# when it receives them. This creates a lot of noise in sentry so
# intercept these requests and return a 501 error without raising an exception
def lifetime_wrapper(func):
async def inner(scope, receive, send):
if scope["type"] != "http":
return HttpResponse(status=501)
return await func(scope, receive, send)
return inner


application = lifetime_wrapper(get_asgi_application())

0 comments on commit 4c914e3

Please sign in to comment.