Skip to content

Commit

Permalink
feat: Switch from wsgi to asgi (attempt 3) (#21579)
Browse files Browse the repository at this point in the history
* Try switching from wsgi to asgi

By default I don't believe this will change anything (everything runs
sync by default), but it will allow us to wrap e.g. clickhouse calls in
sync_to_async wrappers to stop them blocking for ages

* Trigger e2e tests

* Set env var with ASGI/WSGI info

* Fix sentry tracing sampling in ASGI

This was relying on wsgi_environ which is replaced by asgi_scope

* Remove bad headers typecast

* fix linting errors
  • Loading branch information
frankh authored Apr 17, 2024
1 parent 18b2350 commit 2c0dc75
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ jobs:
timeout-minutes: 5
outputs:
chunks: ${{ steps.chunk.outputs.chunks }}

steps:
- name: Check out
uses: actions/checkout@v3
Expand Down
8 changes: 8 additions & 0 deletions posthog/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

from django.core.asgi import get_asgi_application

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

application = get_asgi_application()
6 changes: 6 additions & 0 deletions posthog/settings/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ def traces_sampler(sampling_context: dict) -> float:
if op == "http.server":
path = sampling_context.get("wsgi_environ", {}).get("PATH_INFO")
force_sample = bool(sampling_context.get("wsgi_environ", {}).get("HTTP_FORCE_SAMPLE"))
if os.environ.get("SERVER_GATEWAY_INTERFACE") == "ASGI":
path = sampling_context.get("asgi_scope", {}).get("path")
headers = sampling_context.get("asgi_scope", {}).get("headers", [])
for name, value in headers:
if name.lower().replace(b"_", b"-") == "force-sample":
force_sample = bool(value)

# HTTP header to force sampling set
if force_sample:
Expand Down
1 change: 1 addition & 0 deletions posthog/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()
3 changes: 2 additions & 1 deletion unit.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"processes": 4,
"working_directory": "/code",
"path": ".",
"module": "posthog.wsgi",
"module": "posthog.asgi",
"protocol": "asgi",
"user": "nobody",
"limits": {
"requests": 50000
Expand Down

0 comments on commit 2c0dc75

Please sign in to comment.