Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(conversations): count widget 429s by throttle scope and team (#127743)

Paul D'Ambra0ce9c032c6cc
diff --git a/posthog/rate_limit.py b/posthog/rate_limit.py
index 97fb90a70089..fe8f35012c71 100644
--- a/posthog/rate_limit.py
+++ b/posthog/rate_limit.py
@@ -1458,7 +1458,21 @@ class BreakGlassSustainedThrottle(UserOrEmailRateThrottle):
rate = "75/hour"
-class WidgetUserBurstThrottle(SimpleRateThrottle):
+class _CountedWidgetThrottle(SimpleRateThrottle):
+ """Each widget view has a bucket per widget session (or client IP) and a bucket per widget token, and a 429
+ response does not say which one is empty. The counter records the scope so that dashboards can tell them apart."""
+
+ def allow_request(self, request: "Request", view: "APIView") -> bool:
+ allowed = super().allow_request(request, view)
+ if not allowed:
+ # WidgetAuthentication returns the team as request.auth, because widget requests have no user
+ team_id = getattr(request.auth, "id", None)
+ route = get_route_from_path(getattr(request, "path", None))
+ RATE_LIMIT_EXCEEDED_COUNTER.labels(team_id=team_id, scope=self.scope, path=route, route=route).inc()
+ return allowed
+
+
+class WidgetUserBurstThrottle(_CountedWidgetThrottle):
"""Rate limit per widget_session_id or IP for POST/GET requests."""
scope = "widget_user_burst"
@@ -1474,7 +1488,7 @@ class WidgetUserBurstThrottle(SimpleRateThrottle):
return self.cache_format % {"scope": self.scope, "ident": ident}
-class _WidgetTeamTokenThrottle(SimpleRateThrottle):
+class _WidgetTeamTokenThrottle(_CountedWidgetThrottle):
rate = "3600/hour"
def get_cache_key(self, request: "Request", view: "APIView") -> str:
diff --git a/posthog/test/test_rate_limit.py b/posthog/test/test_rate_limit.py
index 53ee6990fc67..1bc1026a6df3 100644
--- a/posthog/test/test_rate_limit.py
+++ b/posthog/test/test_rate_limit.py
@@ -11,6 +11,7 @@ from django.test import SimpleTestCase
from django.utils.timezone import now
from parameterized import parameterized
+from prometheus_client import REGISTRY
from rest_framework import status
from posthog import models, rate_limit
@@ -1056,10 +1057,38 @@ class TestWidgetTeamPollWriteThrottleSplit(SimpleTestCase):
cache.clear()
def test_exhausted_poll_bucket_does_not_block_write(self) -> None:
- request = Mock(headers={"X-Conversations-Token": "widget-token"})
+ request = Mock(headers={"X-Conversations-Token": "widget-token"}, path="/api/conversations/v1/widget/tickets")
view = Mock()
with patch.object(rate_limit.WidgetTeamPollThrottle, "rate", "1/minute"):
poll = rate_limit.WidgetTeamPollThrottle()
self.assertTrue(poll.allow_request(request, view))
self.assertFalse(poll.allow_request(request, view))
self.assertTrue(rate_limit.WidgetTeamWriteThrottle().allow_request(request, view))
+
+ @parameterized.expand(
+ [
+ ("user_burst", rate_limit.WidgetUserBurstThrottle),
+ ("team_poll", rate_limit.WidgetTeamPollThrottle),
+ ]
+ )
+ def test_exhausted_bucket_is_counted_by_scope_and_team(
+ self, _name: str, throttle_class: type[rate_limit.WidgetUserBurstThrottle | rate_limit.WidgetTeamPollThrottle]
+ ) -> None:
+ path = "/api/conversations/v1/widget/tickets"
+ request = Mock(
+ headers={"X-Conversations-Token": "widget-token"},
+ auth=Mock(id=7),
+ path=path,
+ data={},
+ query_params={"widget_session_id": "session"},
+ )
+ route = rate_limit.get_route_from_path(path)
+ labels = {"team_id": "7", "scope": throttle_class.scope, "path": route, "route": route}
+ before = REGISTRY.get_sample_value("rate_limit_exceeded_total", labels) or 0
+
+ with patch.object(throttle_class, "rate", "1/minute"):
+ throttle = throttle_class()
+ self.assertTrue(throttle.allow_request(request, Mock()))
+ self.assertFalse(throttle.allow_request(request, Mock()))
+
+ self.assertEqual(REGISTRY.get_sample_value("rate_limit_exceeded_total", labels), before + 1)