-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
fix: set posthoganalytics key in asgi #28627
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This PR fixes an issue with PostHog analytics initialization in ASGI debug mode by implementing async-compatible analytics key setting functionality.
- Added
wrap_debug_analytics
decorator in/posthog/asgi.py
to handle async initialization of analytics key - Implemented flag-based initialization check to prevent redundant key setting on subsequent requests
- Applied wrapper only in debug mode to maintain production behavior
- Added async function
aset_debugging_analytics_key()
in/posthog/utils.py
for ASGI-compatible analytics key setting
2 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile
posthog/asgi.py
Outdated
if not getattr(inner, "debug_analytics_initialized", False): | ||
from posthog.utils import aset_debugging_analytics_key |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
posthog/utils.py
Outdated
else: | ||
team = await Team.objects.only("api_token").afirst() | ||
local_api_key = team.api_token | ||
except: |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
Yup, looks like this is the way. I refactored things so that all self-capture, across backend and frontend, relies on this async |
Problem
Django has been running on ASGI in debug mode for two days. Suddenly, local LLM traces stopped working somewhere around that time. The root problem is in the
posthog.apps.PostHogConfig.ready()
method, which now runs in an async context, so the ORM must also be called in async mode. However, theready()
method doesn't have an async alternative—Detailed explanation.Changes
Add a debug-only wrapper for the ASGI web server, which initializes the
posthoganalytics
package. @Twixes, you might have a better idea of how we can fix it.Does this work well for both Cloud and self-hosted?
Yes
How did you test this code?
Manual and local testing.