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(cdp): count transactional emails that fail for good (#129089)

Harley Alexander4a98da8be421
diff --git a/nodejs/src/cdp/services/messaging/email-status.service.ts b/nodejs/src/cdp/services/messaging/email-status.service.ts
index 8a36cc9e7cd7..42cccbed5756 100644
--- a/nodejs/src/cdp/services/messaging/email-status.service.ts
+++ b/nodejs/src/cdp/services/messaging/email-status.service.ts
@@ -11,8 +11,8 @@ import { classifyError } from '../monitoring/hog-invocation-results.service'
const counterEmailStatusRowsProduced = new Counter({
name: 'cdp_email_status_rows_produced',
- help: 'Transactional email status rows produced to the email_status Kafka topic.',
- labelNames: ['status'],
+ help: 'Transactional email status rows produced to the email_status Kafka topic, by status and reason. The reason is empty for a status without one, a skip reason or a run error kind for failed, and the bounce type for bounced.',
+ labelNames: ['status', 'reason'],
})
const counterEmailStatusWriteFailed = new Counter({
@@ -253,7 +253,7 @@ export class EmailStatusService {
key: Buffer.from(row.message_id),
value: Buffer.from(JSON.stringify(row)),
})
- counterEmailStatusRowsProduced.inc({ status: row.status })
+ counterEmailStatusRowsProduced.inc({ status: row.status, reason: row.reason })
} catch (error) {
counterEmailStatusWriteFailed.inc({ status: row.status })
logger.error('⚠️', 'failed to produce an email status row', {
diff --git a/posthog/email.py b/posthog/email.py
index 2b4d5e14f18f..e7950ccbb720 100644
--- a/posthog/email.py
+++ b/posthog/email.py
@@ -122,6 +122,14 @@ EMAIL_SEND_COUNTER = Counter(
labelnames=["outcome", "transport"],
)
+# `failed` counts every attempt, including one that a retry later sends. This counts only the recipients a send gave
+# up on, so it shows the emails that did not go out.
+EMAIL_SEND_DROPPED_COUNTER = Counter(
+ "posthog_email_send_dropped_total",
+ "Recipients whose email send failed with no retry to follow, so the email did not go out, by transport.",
+ labelnames=["transport"],
+)
+
# Retryable connection/network errors only. NOT bare OSError: every smtplib exception subclasses
# it, so OSError would also retry auth/recipient failures and re-hammer the relay's per-IP limit.
_TRANSIENT_SMTP_ERRORS = (
@@ -648,9 +656,10 @@ def _send_via_http_batch(
)
# Count every recipient that did not get through (the failing one + any not yet attempted),
# so `failed` shares the per-recipient unit with `sent` instead of registering once per batch.
- EMAIL_SEND_COUNTER.labels(outcome="failed", transport="http").inc(
- len(to) - already_sent_count - sent_count - undeliverable_count
- )
+ unsent_count = len(to) - already_sent_count - sent_count - undeliverable_count
+ EMAIL_SEND_COUNTER.labels(outcome="failed", transport="http").inc(unsent_count)
+ if not retrying:
+ EMAIL_SEND_DROPPED_COUNTER.labels(transport="http").inc(unsent_count)
if retryable:
# The retry sends the same idempotency key, so Workflows returns an email it already queued instead of
# sending a second one, and recipients already marked sent are skipped.
diff --git a/posthog/test/test_email.py b/posthog/test/test_email.py
index 70d8c5f07f47..f57a6b652426 100644
--- a/posthog/test/test_email.py
+++ b/posthog/test/test_email.py
@@ -916,8 +916,8 @@ class TestEmail(BaseTest):
@parameterized.expand(
[
- ("first_attempt_waits_10s", 0, 10, True),
- ("last_attempt_gives_up", len(HTTP_SEND_RETRY_COUNTDOWNS_SECONDS), None, False),
+ ("first_attempt_waits_10s", 0, 10, True, 0.0),
+ ("last_attempt_gives_up", len(HTTP_SEND_RETRY_COUNTDOWNS_SECONDS), None, False, 1.0),
]
)
@patch.object(_send_email, "apply_async")
@@ -930,11 +930,14 @@ class TestEmail(BaseTest):
retries: int,
expected_countdown: int | None,
expected_retrying: bool,
+ expected_dropped: float,
_mock_post: MagicMock,
_mock_feature_enabled: MagicMock,
mock_scoped_capture: MagicMock,
mock_apply_async: MagicMock,
) -> None:
+ dropped_labels = {"transport": "http"}
+ dropped_before = REGISTRY.get_sample_value("posthog_email_send_dropped_total", dropped_labels) or 0.0
_send_email.push_request(id="worker-task", retries=retries, is_eager=False, called_directly=False)
try:
with (
@@ -967,6 +970,8 @@ class TestEmail(BaseTest):
capture = mock_scoped_capture.return_value.__enter__.return_value
failed = [c.kwargs for c in capture.call_args_list if c.kwargs.get("event") == "transactional email failed"]
self.assertEqual([f["properties"]["retrying"] for f in failed], [expected_retrying])
+ dropped_after = REGISTRY.get_sample_value("posthog_email_send_dropped_total", dropped_labels) or 0.0
+ self.assertEqual(dropped_after - dropped_before, expected_dropped)
def test_sanitize_email_properties(self) -> None:
# Test with various types of input including potential HTML injection