Observabilidad
Logs, métricas, trazas
Escrito por Darjus Kazakevic · revisado el
Metrics say something is wrong; one request id in logs and traces says why.
Monitoring tells you that a known thing broke. Observability lets you ask a question you did not plan for. The difference is what the system emits. A log line that says “error” is monitoring. A structured event with the request id, the tenant, the upstream latency and the retry count is data you can query.
Three signals do different jobs. Metrics show rates and trends cheaply. Traces show the path of one request across services. Logs hold the detail. The cost is discipline: the request id must cross every call and queue message, or the trace stops at the first hop. Metric labels must stay few, or storage costs explode.
En esencia — Emit metrics for trends, and structured logs and traces that share one request id, so you can explain a new failure without adding a log line and redeploying.
Cuándo recurrir a él
Recurre a él cuando
- Requests cross several services, queues or background jobs
- Incidents keep needing a new log line and a redeploy to diagnose
- You run more than one service and are on call for them
Reconsidéralo cuando
- A single script or batch job where a plain log and exit code suffice
- A short-lived prototype that nobody is on call for
- A single-process tool, where a debugger answers the question faster
Conceptos clave
- Metrics
- Numbers aggregated over time: request rate, error rate, latency histograms. They are cheap to store and fast to alert on, but they cannot tell you which request failed.
- Structured logs
- One event per unit of work, written as key-value data (usually JSON) instead of free text. You can filter and group by any field, such as tenant or request id.
- Distributed traces
- A tree of spans (timed steps) for one request across services. It shows where the time went and which call failed. OpenTelemetry is the standard way to emit them.
- Correlation id
- One id created at the edge and passed through every call, message and job. It joins a slow trace to its log lines and to the metric spike around it. W3C traceparent is the standard header.
- Cardinality
- The number of distinct label combinations a metric has. Each combination is a separate time series. A user-id label creates millions of series and can take down the metrics store.
Reglas de diseño
- Mint one id at the edge, carry it everywhere
- Create the request id once, then pass it through every outbound call, queue message and background job, and put it on every log event and span. Where it stops, the trace stops.
- Label metrics by route, never by user
- Every distinct label value is a new time series. Keep metric labels to small, fixed sets like route and status; per-user and per-request detail belongs in logs and traces.
- Alert on symptoms users feel
- Page on error rate and latency at the edge, against the SLO. CPU at 90% is a cause to look into, not a reason to wake someone at 3 a.m.
- Log events, not sentences
- Write one structured event per request with all its context, instead of ten free-text lines. Then “all 500s for tenant 42 slower than 300 ms” is a query, not a grep.
Errores habituales
- A user id or request id as a metric label. The metrics store grows to millions of series and becomes slow and expensive.
- Losing the id at a queue. The trace ends at the publisher, and the consumer's logs cannot be joined to the request.
- Free-text logs. Every question needs regular expressions across services, and the field you need is missing.
- Keeping a random 1% of traces only. The rare slow or failed request is the one you need, and it was dropped; keep all errors and slow traces.