Skip to content

Conversation

@oliverb123
Copy link
Contributor

@oliverb123 oliverb123 commented Jun 17, 2025

The distinct_id bit is currently kind-of unused - I'm going to add additional machinery and handling for it in future PRs

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

Added Django middleware integration to automatically track requests and capture exceptions in PostHog Python client, with significant refactoring of exception handling logic.

  • Introduced PosthogContextMiddleware in posthog/integrations/django.py for automatic request tracking and scope management
  • Refactored exception tracking into utility functions in posthog/exception_utils.py to prevent duplicate captures
  • Added comprehensive test suite in test_middleware.py covering 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

Comment on lines 787 to 789
return error[2] is not None and hasattr(
error[2], "__posthog_exception_captured"
)
Copy link
Contributor

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

@oliverb123 oliverb123 requested review from a team, daibhin and hpouillot June 17, 2025 12:06
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`)
Copy link
Contributor

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

Copy link
Contributor Author

@oliverb123 oliverb123 Jun 17, 2025

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

Copy link
Contributor

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

@oliverb123 oliverb123 merged commit 250bd42 into master Jun 17, 2025
7 checks passed
@oliverb123 oliverb123 deleted the err/django-middleware branch June 17, 2025 14:54
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.

4 participants