Monitoring answers questions you thought of in advance. Observability is being able to ask new questions about a running system without shipping new code. It rests on three kinds of data.
Logs
A log is a record of an event: a request failed, a job started. Write structured logs, meaning JSON with fields, rather than free text, so you can filter and aggregate by user, request id or status.
{"level":"error","msg":"payment failed","request_id":"abc123","user_id":42,"provider":"card","duration_ms":812}Never log passwords, tokens or full card numbers, and be careful with personal data.
Metrics
Metrics are numbers over time: requests per second, error rate, queue depth, memory. They are cheap to store and perfect for dashboards and alerts. A useful starting set for any service is the RED method: Rate, Errors and Duration of requests.
For duration, look at percentiles instead of the average. An average of 200 ms can hide a slowest one percent of requests that take ten seconds. Track p50, p95 and p99.
Traces
A trace follows one request across services as a tree of spans, each with a start time and duration. When a page is slow, the trace shows whether the time went to the database, a downstream API or your own code. A trace id passed along each call, and included in logs, ties everything together.
How they work together
- An alert fires on a metric: error rate is up.
- A trace of a failing request shows which dependency is slow.
- Logs with the same request id explain the exact error.
Alerting
- Alert on symptoms users feel, such as errors and latency, not on every internal cause such as CPU.
- Every alert needs an owner and a documented first step.
- Remove or tune alerts that fire without action. Noisy alerts teach people to ignore them.
Takeaway
Emit structured logs with a request id, track rate, errors and duration, and add tracing once requests cross several services. Debug with evidence, not guesses.
Comments