Skip to content
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

Merged
merged 6 commits into from
Feb 13, 2025

Conversation

skoob13
Copy link
Contributor

@skoob13 skoob13 commented Feb 12, 2025

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, the ready() 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.

@skoob13 skoob13 requested a review from Twixes February 12, 2025 17:29
Copy link
Contributor

@greptile-apps greptile-apps bot left a 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
Comment on lines 27 to 28
if not getattr(inner, "debug_analytics_initialized", False):
from posthog.utils import aset_debugging_analytics_key

This comment was marked as resolved.

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.

posthog/utils.py Show resolved Hide resolved
@Twixes
Copy link
Member

Twixes commented Feb 13, 2025

Yup, looks like this is the way. I refactored things so that all self-capture, across backend and frontend, relies on this async initialize_self_capture_api_token() – see here: eb62ec7 (#28627)

@Twixes Twixes enabled auto-merge (squash) February 13, 2025 11:13
@skoob13 skoob13 disabled auto-merge February 13, 2025 15:52
@skoob13 skoob13 requested a review from Twixes February 13, 2025 17:16
@Twixes Twixes merged commit d939b64 into master Feb 13, 2025
96 checks passed
@Twixes Twixes deleted the fix/asgi-posthoganalytics-debug-token branch February 13, 2025 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants