Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 6.0.4 - 2025-07-09

- fix: add POSTHOG_MW_CLIENT setting to django middleware, to support custom clients for exception capture.

# 6.0.3 - 2025-07-07

- feat: add a feature flag evaluation cache (local storage or redis) to support returning flag evaluations when the service is down
Expand Down
13 changes: 11 additions & 2 deletions posthog/integrations/django.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import TYPE_CHECKING, cast
from posthog import contexts
from posthog.client import Client

if TYPE_CHECKING:
from django.http import HttpRequest, HttpResponse # noqa: F401
Expand All @@ -16,7 +17,8 @@ class PosthogContextMiddleware:
- Request Method as $request_method

The context will also auto-capture exceptions and send them to PostHog, unless you disable it by setting
`POSTHOG_MW_CAPTURE_EXCEPTIONS` to `False` in your Django settings.
`POSTHOG_MW_CAPTURE_EXCEPTIONS` to `False` in your Django settings. The exceptions are captured using the
global client, unless the setting `POSTHOG_MW_CLIENT` is set to a custom client instance

The middleware behaviour is customisable through 3 additional functions:
- `POSTHOG_MW_EXTRA_TAGS`, which is a Callable[[HttpRequest], Dict[str, Any]] expected to return a dictionary of additional tags to be added to the context.
Expand Down Expand Up @@ -74,6 +76,13 @@ def __init__(self, get_response):
else:
self.capture_exceptions = True

if hasattr(settings, "POSTHOG_MW_CLIENT") and isinstance(
settings.POSTHOG_MW_CLIENT, Client
):
self.client = cast("Optional[Client]", settings.POSTHOG_MW_CLIENT)
else:
self.client = None

def extract_tags(self, request):
# type: (HttpRequest) -> Dict[str, Any]
tags = {}
Expand Down Expand Up @@ -153,7 +162,7 @@ def __call__(self, request):
if self.request_filter and not self.request_filter(request):
return self.get_response(request)

with contexts.new_context(self.capture_exceptions):
with contexts.new_context(self.capture_exceptions, client=self.client):
for k, v in self.extract_tags(request).items():
contexts.tag(k, v)

Expand Down
2 changes: 1 addition & 1 deletion posthog/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "6.0.3"
VERSION = "6.0.4"

if __name__ == "__main__":
print(VERSION, end="") # noqa: T201
Loading