Prompt
How do I make email delivery observable?
Latest observation
To make email delivery observable, instrument the full email lifecycle so you can answer four questions:
- Was the email accepted by your provider?
- Was it delivered, deferred, bounced, or blocked?
- Did the user engage with it?
- Can you trace one email across retries and systems?
1) Assign a unique ID to every email
Create an internal message_id for each email before sending.
Store:
- recipient
- template/type
- campaign/job ID
- provider message ID
- status
- timestamps for each state change
This lets you correlate:
- app logs
- queue jobs
- provider events
- bounce/webhook callbacks
2) Track the email lifecycle as states
Use a simple state machine like:
queuedsendingsent/accepteddelivereddeferredbouncedcomplainedopenedclickedsuppressed/blocked
Log every transition with:
- timestamp
- reason/error code
- provider response
- retry count
3) Integrate provider event webhooks
Most providers (SES, SendGrid, Mailgun, Postmark, etc.) offer event webhooks.
Ingest events for:
- delivery
- bounce
- complaint
- open
- click
- unsubscribe
- deferral
- rejection
Important:
- verify webhook signatures
- make processing idempotent
- persist raw payloads for debugging
- deduplicate events
4) Emit structured logs
For every send attempt, log structured fields such as:
message_idproviderprovider_message_idrecipient_domaintemplate_namesend_resulterror_codelatency_ms
Use JSON logs so you can search and aggregate easily.
5) Add metrics
Track metrics such as:
- send volume
- accepted rate
- delivery rate
- bounce rate
- complaint rate
- open rate
- click rate
- retry rate
- queue age
- webhook lag
- provider latency
- suppressed recipients
Break these down by:
- provider
- domain
- template
- campaign
- tenant
- region
6) Build dashboards and alerts
Dashboards should show:
- daily send/delivery/bounce trends
- top failing domains
- provider error rates
- queue backlog
- webhook ingestion health
Alerts for:
- bounce rate spikes
- complaint spikes
- delivery rate drops
- webhook failures
- queue buildup
- provider API errors
- sudden domain-specific failures
7) Trace the path end-to-end
If you use distributed tracing, create a trace/span for:
- enqueue email job
- provider API call
- webhook event ingestion
- state update
Propagate trace_id and message_id through all systems.
8) Monitor deliverability signals
Delivery observability is not just technical success; it also includes deliverability health:
- domain reputation
- SPF/DKIM/DMARC alignment
- complaint rates
- inbox placement proxies
- suppression list growth
- hard bounce patterns
9) Store failure reasons consistently
Normalize provider-specific errors into categories like:
- invalid recipient
- mailbox full
- domain not found
- temporary deferral
- policy rejection
- spam complaint
- blocked by recipient server
This makes reporting much easier.
10) Measure user-level outcomes
If your emails drive actions, track whether the user:
- verified their account
- reset their password
- completed onboarding
- converted
That helps distinguish “delivered but ineffective” from “delivered and successful.”
Minimal implementation blueprint
If you want the shortest path:
- Generate
message_id - Save send attempt in DB
- Send email through provider
- Record provider response
- Receive provider webhooks
- Update status on each event
- Log everything in structured JSON
- Add metrics + alerts
Example schema
emails (
id uuid primary key,
recipient text,
template text,
provider text,
provider_msg_id text,
status text,
error_code text,
error_reason text,
queued_at timestamptz,
sent_at timestamptz,
delivered_at timestamptz,
bounced_at timestamptz,
opened_at timestamptz,
clicked_at timestamptz,
retry_count int default 0,
trace_id text
)
Key pitfalls
- Relying only on “API accepted” as “delivered”
- Not deduplicating webhook events
- Not storing raw provider payloads
- No per-domain metrics
- Missing webhook signature validation
- No alerting on delivery drops
- Measuring opens too literally due to privacy features
If you want, I can also give you:
- a reference architecture
- a Postgres schema + event model
- a Prometheus/Grafana metric list
- or an example implementation in Node/Python/Go