Sub topic 17
Monitoring and observability
This chapter assumes you know the pipeline from earlier chapters: producers, queues, channel workers, providers and delivery receipts. It covers how to tell whether that pipeline is healthy and where to look when it isn't.
Why notifications need extra care
A notification system fails quietly. If a worker stalls, producers still get a success response, and users simply never receive a message. Monitoring has to measure outcomes at each stage, not just whether servers are up.
Observability rests on three signals:
- Metrics are numbers sampled over time. They answer "is something wrong?".
- Logs are records of individual events. They answer "what happened to this notification?".
- Traces link the steps one event passes through. They answer "where did it get stuck?".
What to measure
Place a measurement at every hand-off.
The most useful metrics are:
| Stage | Metric | Signals |
|---|---|---|
| Queue | Age of the oldest message | Workers are behind |
| Worker | Retry rate per provider | A provider is degraded |
| Worker | Dead-letter queue size | Messages failing permanently |
| Provider | Success rate per channel | Outage or bad credentials |
| End to end | Time from event to provider acceptance | Users receive messages late |
| Receipt | Delivered ratio | Bad tokens or addresses |
Queue depth alone misleads. A deep queue of low-priority batches may be fine, while a short queue with an old critical message is an incident. Track age per priority queue.
Instrumenting a worker
Count outcomes with labels, and log in a structured form that includes the event ID.
import json
from collections import Counter
class Metrics:
def __init__(self):
self.counts = Counter()
def inc(self, name, **labels):
self.counts[(name, tuple(sorted(labels.items())))] += 1
def get(self, name, **labels):
return self.counts[(name, tuple(sorted(labels.items())))]
metrics = Metrics()
def record_send(event_id, channel, ok):
outcome = "success" if ok else "failure"
metrics.inc("send_total", channel=channel, outcome=outcome)
print(json.dumps({"event_id": event_id, "channel": channel, "outcome": outcome}))
def success_rate(channel):
ok = metrics.get("send_total", channel=channel, outcome="success")
bad = metrics.get("send_total", channel=channel, outcome="failure")
total = ok + bad
return ok / total if total else None
record_send("evt-1001", "push", True)
record_send("evt-1002", "push", False)
print(success_rate("push"))Alerting
Alert on symptoms users would feel, not on every internal fluctuation:
- The oldest critical message is older than its target, for example 30 seconds.
- Channel success rate drops below a threshold for several minutes.
- The dead-letter queue grows continuously.
- Delivered ratio falls sharply after a deploy or provider change.
A correlation ID, usually the event ID, ties these signals together. When a user reports a missing message, search logs for that ID and follow it through ingestion, dedup, routing and the provider response.
Exercise
Queue depth is low and all workers are healthy, but support reports that password-reset emails are arriving 10 minutes late. Which metric would you check first, and why?
Show answer
Check the age of the oldest message in the critical or email queue, and the time from event to provider acceptance. Depth can be low while a few messages wait behind slow retries or throttling. Message age and end-to-end latency show the delay users actually experience.