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.

fix(ingress): log the parser error when a verified delivery has a bad body (#101425)

Julian Bez901cec4ba8e3
diff --git a/posthog/ingress/README.md b/posthog/ingress/README.md
index a602d5da0bc0..741a39f96307 100644
--- a/posthog/ingress/README.md
+++ b/posthog/ingress/README.md
@@ -161,5 +161,6 @@ A provider that sends no delivery id skips dedup entirely, and its own README sa
- **`posthog_ingress_deliveries_total{provider,app,outcome}`** — what the transport answered: `accepted`, `method_not_allowed`, `not_configured`, `invalid_signature`, `invalid_payload`. A consumer failure is not here, because a failing consumer still gets a 2xx receipt.
- **`posthog_ingress_consumer_runs_total{provider,consumer,outcome}`** — `succeeded`, `failed`, `deduped`, `budget_exceeded`.
- **`posthog_ingress_consumer_duration_seconds{provider,consumer}`** — where a delivery's budget actually went.
+- **`ingress_delivery_invalid_payload`** — a warning log with the parser error text for a verified delivery whose body did not parse. The counter above cannot carry that text.
A secret in a URL or header is the credential and never becomes a metric label.
diff --git a/posthog/ingress/test/test_views.py b/posthog/ingress/test/test_views.py
index 31ad3bd3a7d3..5875779de741 100644
--- a/posthog/ingress/test/test_views.py
+++ b/posthog/ingress/test/test_views.py
@@ -5,6 +5,7 @@ from unittest.mock import Mock, patch
from django.test import RequestFactory, SimpleTestCase, override_settings
+import structlog.testing
from parameterized import parameterized
from posthog.ingress.github.provider import build_github_provider
@@ -62,15 +63,21 @@ class TestWebhookView(SimpleTestCase):
self.assertEqual(response.content, b"Invalid signature")
self.dispatcher.dispatch.assert_not_called()
- def test_an_unparseable_body_is_400(self) -> None:
+ def test_an_unparseable_body_is_400_and_logs_the_parser_error(self) -> None:
body = b"{not json"
request = self._post(body, {"X-Hub-Signature-256": _github_signature(body), "X-GitHub-Event": "push"})
with patch("posthog.ingress.github.provider.get_instance_setting", return_value=SECRET):
- response = self._github_view()(request)
+ with structlog.testing.capture_logs() as logs:
+ response = self._github_view()(request)
self.assertEqual(response.status_code, 400)
self.dispatcher.dispatch.assert_not_called()
+ warning = next(log for log in logs if log["event"] == "ingress_delivery_invalid_payload")
+ self.assertEqual(warning["log_level"], "warning")
+ self.assertEqual(warning["provider"], "github")
+ self.assertEqual(warning["app"], "posthog")
+ self.assertIn("Expecting property name", warning["error"])
def test_a_verified_delivery_is_202_whatever_the_consumers_did(self) -> None:
body = json.dumps({"action": "opened", "installation": {"id": 42}}).encode()
diff --git a/posthog/ingress/views.py b/posthog/ingress/views.py
index 47988586afbd..586f9611354c 100644
--- a/posthog/ingress/views.py
+++ b/posthog/ingress/views.py
@@ -44,7 +44,10 @@ def build_webhook_view(provider: WebhookProvider) -> Callable[[HttpRequest], Htt
try:
# RecursionError: deeply nested JSON must answer 400, not 500.
payload = json.loads(request.body)
- except (json.JSONDecodeError, UnicodeDecodeError, RecursionError):
+ except (json.JSONDecodeError, UnicodeDecodeError, RecursionError) as exc:
+ logger.warning(
+ "ingress_delivery_invalid_payload", provider=provider.provider, app=provider.app, error=str(exc)
+ )
observe_delivery(provider=provider.provider, app=provider.app, outcome="invalid_payload")
return HttpResponse("Invalid JSON", status=400)