-
Notifications
You must be signed in to change notification settings - Fork 53
feat(err): add django middleware #263
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
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
Added Django middleware integration to automatically track requests and capture exceptions in PostHog Python client, with significant refactoring of exception handling logic.
- Introduced
PosthogContextMiddlewareinposthog/integrations/django.pyfor automatic request tracking and scope management - Refactored exception tracking into utility functions in
posthog/exception_utils.pyto prevent duplicate captures - Added comprehensive test suite in
test_middleware.pycovering tag extraction and header handling - Created new integrations package structure with proper type hints and documentation
5 files reviewed, 6 comments
Edit PR Review Bot Settings | Greptile
posthog/exception_utils.py
Outdated
| return error[2] is not None and hasattr( | ||
| error[2], "__posthog_exception_captured" | ||
| ) |
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.
style: Extract __posthog_exception_captured into a constant at module level to avoid string duplication
| This middleware wraps all calls with a posthog scope. It attempts to extract the following from the request headers: | ||
| - Session ID as $session_id, (extracted from `X-POSTHOG-SESSION-ID`) | ||
| - Distinct ID as $distinct_id, (extracted from `X-POSTHOG-DISTINCT-ID`) |
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.
Is this set in our SDK? I didn't see it referenced anywhere but maybe it's coming in a separate PR. I know we append X-POSTHOG-SESSION-ID when tracing headers are enabled
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.
Not in the JS one, no. We should start, but I leave that in more capable hands 😄 (or I can take it later). One thing is we basically can't use it in the python SDK right now, users would have to manually handle it, because we requires users pass a distinct ID to capture et-al
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.
This is all it should be I think: PostHog/posthog-js#2026
Co-authored-by: David Newell <[email protected]>
The distinct_id bit is currently kind-of unused - I'm going to add additional machinery and handling for it in future PRs