Sub topic 18
Monitoring and observability
This chapter assumes you know the payment lifecycle, idempotent APIs, retries, and the outbox pattern from earlier chapters. It covers how to see what a payment system is doing, so you can find problems before merchants do.
What to watch
Monitoring tells you that something is wrong. Observability lets you ask why. A payment system needs both, because a failure often leaves no error. A payment can sit in authorized for days and no alert fires.
Track three kinds of signals:
- Metrics are numbers over time, such as requests per second or authorization success rate.
- Logs are records of single events, such as one state transition.
- Traces follow one request across services, from your API to the PSP and back.
Metrics that matter
Watch business outcomes, not only server health. A server can be healthy while every card is being declined.
| Metric | Why it matters |
|---|---|
| Authorization success rate, per PSP and card network | A drop usually means a PSP outage or a routing problem. |
| p99 latency of PSP calls | Slow calls cause timeouts, and timeouts cause retries. |
| Payments stuck in a non-final state | Shows lost webhooks or a crashed saga. |
| Outbox backlog (oldest unpublished event age) | Shows that events are not reaching consumers. |
| Idempotency key conflicts | A spike can mean a client bug or a retry storm. |
| Reconciliation mismatches | Shows drift between your ledger and PSP reports. |
Alert on rate changes against a baseline, not on fixed counts. Traffic varies by hour and by region.
Tracing a payment
Give every payment a correlation ID and pass it through every call, log line, and event. Include the PSP's own reference in your logs too. When support asks about one payment, you can then pull its whole history with one search.
import json
import logging
import time
logger = logging.getLogger("payments")
def log_transition(payment_id, merchant_id, old, new, psp_ref=None):
logger.info(json.dumps({
"event": "payment_transition",
"payment_id": payment_id,
"merchant_id": merchant_id,
"from": old,
"to": new,
"psp_ref": psp_ref,
"ts": time.time(),
}))Common errors
- Logging sensitive data. Never log card numbers, CVV, or full tokens. Logs fall inside PCI DSS scope if they contain card data. Mask or omit these fields.
- Alerting only on HTTP 5xx. A decline is a valid 200 or 402 response. Track decline rates by reason code.
- High-cardinality metric labels. Using
payment_idas a label creates millions of time series. Put IDs in logs and traces instead. - No alert on silence. If traffic drops to zero, no errors appear. Alert on unusually low volume.
Exercise
Your authorization success rate for one PSP falls from 95% to 60%, but your API shows no 5xx errors and normal latency. Name two things to check.
Show answer
Check whether the PSP is returning declines or soft errors (by reason code), and check whether routing sent a different card mix to that PSP. Also look at the PSP's status page and at changes in your own request format.
Exercise
Why should payment_id be a log field and not a metric label?
Show answer
Each payment ID is unique, so a label would create one time series per payment. That overloads the metrics system. Logs and traces handle unique values well.