Skip to content

Commit c61236b

Browse files
authored
fix: add middleware setting for custom client (#281)
* Add middleware setting for custom client * mypy * comment
1 parent b965332 commit c61236b

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 6.0.4 - 2025-07-09
2+
3+
- fix: add POSTHOG_MW_CLIENT setting to django middleware, to support custom clients for exception capture.
4+
15
# 6.0.3 - 2025-07-07
26

37
- feat: add a feature flag evaluation cache (local storage or redis) to support returning flag evaluations when the service is down

posthog/integrations/django.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import TYPE_CHECKING, cast
22
from posthog import contexts
3+
from posthog.client import Client
34

45
if TYPE_CHECKING:
56
from django.http import HttpRequest, HttpResponse # noqa: F401
@@ -16,7 +17,8 @@ class PosthogContextMiddleware:
1617
- Request Method as $request_method
1718
1819
The context will also auto-capture exceptions and send them to PostHog, unless you disable it by setting
19-
`POSTHOG_MW_CAPTURE_EXCEPTIONS` to `False` in your Django settings.
20+
`POSTHOG_MW_CAPTURE_EXCEPTIONS` to `False` in your Django settings. The exceptions are captured using the
21+
global client, unless the setting `POSTHOG_MW_CLIENT` is set to a custom client instance
2022
2123
The middleware behaviour is customisable through 3 additional functions:
2224
- `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.
@@ -74,6 +76,13 @@ def __init__(self, get_response):
7476
else:
7577
self.capture_exceptions = True
7678

79+
if hasattr(settings, "POSTHOG_MW_CLIENT") and isinstance(
80+
settings.POSTHOG_MW_CLIENT, Client
81+
):
82+
self.client = cast("Optional[Client]", settings.POSTHOG_MW_CLIENT)
83+
else:
84+
self.client = None
85+
7786
def extract_tags(self, request):
7887
# type: (HttpRequest) -> Dict[str, Any]
7988
tags = {}
@@ -153,7 +162,7 @@ def __call__(self, request):
153162
if self.request_filter and not self.request_filter(request):
154163
return self.get_response(request)
155164

156-
with contexts.new_context(self.capture_exceptions):
165+
with contexts.new_context(self.capture_exceptions, client=self.client):
157166
for k, v in self.extract_tags(request).items():
158167
contexts.tag(k, v)
159168

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "6.0.3"
1+
VERSION = "6.0.4"
22

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

0 commit comments

Comments
 (0)