---
title: Observability
summary: Logs, metrics, traces and alerts, so you know something broke before your users tell you.
section: shipping
phase: operate
order: 3
last_reviewed: 2026-11-20
---

# Observability

You can't fix what you can't see. Observability is also how you hand a production bug to an agent with enough context for it to actually fix it.

## You'll be able to
- Know the three pillars and when to use each
- Set up error tracking and uptime alerts in an afternoon
- Write logs that are useful for both humans and agents

## The mental model

Observability is how you ask a running system questions you didn't plan for, using the evidence it gives off. That evidence comes as **logs** (a record of events), **metrics** (measurements over time) and **traces** (the path of one request through the system). OpenTelemetry calls these signals. Logs tell you what happened, metrics tell you how much and how often, and traces tell you where the time went.

Alongside the evidence, you need two things: someone watching it for problems users would notice (alerts), and a way to hand the evidence to whoever, or whatever, fixes the bug.

## Key concepts
- **Logs:** Timestamped events. **Structured logs** (JSON with fields like `userId`, `requestId`) are searchable. Use levels: debug, info, warn, error.
- **Metrics:** Numbers over time, such as request rate, error rate, latency (p50/p95/p99), CPU and DB connections.
- **Traces:** The path of a single request through services and DB calls, with timings. **OpenTelemetry** is the vendor-neutral standard.
- **Error tracking:** Captures exceptions with stack traces and context (Sentry, Highlight, PostHog). This gives you the most for the least effort.
- **Uptime monitoring:** External pings to your health endpoint (Better Stack, UptimeRobot, Checkly).
- **Alerts:** Notifications when something crosses a threshold. Alert on symptoms (error rate, latency), not every blip.
- **Request ID / correlation ID:** One ID attached to every log line for a request.
- **Product analytics:** What users do (PostHog, Plausible). This is separate from system health.

## Decisions you make (not the agent)
| Decision | Sensible default | Choose otherwise when… |
|---|---|---|
| Errors | Sentry (or equivalent) on frontend + backend | — |
| Logs | Platform logs + structured JSON | Scale → Axiom, Better Stack, Datadog, Grafana Loki |
| Uptime | External check on `/health` every 1–5 min | — |
| Alerts | Error-rate spike + downtime → phone/Slack | — |
| PII in logs | Never log passwords, tokens, full card numbers; minimize personal data | — |

## What good output looks like

- Logs are structured, with a request ID on every line, so one request can be followed from start to finish
- Errors are captured with a stack trace and context, and none are silently swallowed
- The things you watch include latency (not only the average), traffic, errors and how full the system is
- Alerts fire on symptoms users would notice, and each one needs a person to act
- No passwords, tokens, keys or personal data appear in logs

## Red flags
- `console.log("here")` as the logging strategy
- Errors swallowed with an empty `catch {}`
- Logs that contain tokens or passwords
- Alerts so noisy that everyone ignores them
- **Common agent mistakes:** logging a whole request object, which includes the `Authorization` header and cookies; an alert on every `error` log line; reporting only the average latency; catching an error just to log `error.message`, dropping the stack trace and context

## Worked example

**Task:** Users say checkout "sometimes fails." You hand it to an agent.

The app logs `console.log("here")` in a few places and has no error tracking and no request IDs. The agent reads the code, picks the most likely suspect, adds a retry around the payment call, and reports it fixed.

**What a good result contains:** first, evidence. Error tracking shows the exception and its context, structured logs let you follow one failing request by its ID, and a chart of the error rate shows when it began and whether the change helped. Then a fix with a test that reproduces the failure.

**The wrong turn:** nothing was observed, so the "fix" is a guess. The retry may hide the problem, or double-charge. And in trying to add logging, the agent logs the entire request object on every checkout, including the `Authorization` header, so tokens now sit in your logs. OWASP's logging guidance lists access tokens, session IDs, passwords, connection strings and payment data as things not to log.

**How review catches it:** ask *what did you observe that showed this was the cause?* Then add the missing signals: error tracking, a request ID on every log line, and a chart of error rate and latency. Search the logs for tokens. Re-run the investigation with real evidence, and only then fix it, with a failing test first.

## Sample prompts

### Weak vs strong
```compare
## Weak
Add logging to the app.
## Why it fails
It doesn't say what to capture, where errors go, how to find one request, or what must never be logged, so the agent adds `console.log` calls and maybe logs whole requests.
## Strong
Add observability to this app: Sentry for client and server errors with source
maps and release tracking; a structured logger (pino) with a requestId on every
log line; a /health endpoint that checks DB connectivity; and remove any logging
of secrets or PII. Show me where errors were previously being swallowed.
## Why it works
It names the error tracker, a structured logger with a request ID on every line, a health check that tests the database, and tells the agent to remove secrets and personal data from logs.
## What changed
- **Error tracking** covers client and server, with source maps and release tracking
- **Structured logs** carry a `requestId` on every line
- **A health endpoint** checks the database, not just that the process is up
- **Secrets and PII** are explicitly removed
- **Swallowed errors** are found and reported
```

```text
Here's a Sentry error with stack trace and breadcrumbs: <paste>. Find the root
cause in this codebase, explain it, write a failing test that reproduces it, then
fix it.
```
*Why it works:* It hands over the evidence and requires a reproducing test before the fix, which stops the agent from changing code on a guess.

### Audit logging and error handling
```text
Audit logging and error handling in this codebase. Find: places that log whole request or response objects, headers, tokens, passwords, connection strings or personal data; empty catch blocks and errors that are caught and dropped; logs without a request ID; and error handlers that log only the message and lose the stack trace. List each finding with file and line, and the fix. Don't change anything yet.
```
*Why it works:* It targets the specific mistakes agents make with logging, and each finding is easy to check.

### Decide what should page a human
```text
Propose an alerting setup for this service. For every alert, answer: is it urgent, is it visible to users, does it need a person's judgement, and could it wait until morning? Alert on symptoms (error rate, latency, downtime), not on individual causes. Anything that doesn't need a person right now goes on a dashboard instead. List what we should watch for latency, traffic, errors and saturation.
```
*Why it works:* The questions come from Google's SRE guidance: a page should be urgent and actionable, and noisy pages get ignored.

### Explain how to trace one request
```text
Using only what the app currently logs and reports, explain how I would follow one failing checkout from the browser to the database. List each step, what evidence exists at each one, and where there is a gap. Then propose the smallest change that closes the biggest gap.
```
*Why it works:* It makes missing observability visible before the next incident does.

## Check your understanding

```quiz
Q: Which of these should page a person in the middle of the night?
- [ ] Every error log line
- [x] A sustained, user-visible symptom such as a high error rate or the site being down
- [ ] CPU at 70% for one minute
> Why: Google's SRE guidance says a page should be urgent and actionable. Noisy alerts get ignored, and slower issues belong on a dashboard.

---

Q: Which of these should not be written to logs?
- [ ] Request IDs
- [x] Access tokens, passwords and connection strings
- [ ] HTTP status codes
> Why: Logs are widely accessible and long-lived. OWASP lists tokens, passwords, connection strings, keys and payment data as data to keep out or mask.

---

Q: Why look at 95th or 99th percentile latency and not just the average?
- [ ] Averages are harder to compute
- [x] A good average can hide a slow tail that many users still experience
- [ ] Percentiles are always lower
> Why: The SRE guidance warns against designing around the mean and recommends watching high percentiles.

---

Q: Which are the four golden signals?
- [ ] Logs, metrics, traces and events
- [x] Latency, traffic, errors and saturation
- [ ] CPU, memory, disk and network
> Why: These four describe how a service looks to its users and how full it is. Logs, metrics and traces are the kinds of data you collect.
```

## Checklist

- [ ] Errors are tracked on the client and the server, with context
- [ ] Every log line carries a request ID, and logs are structured
- [ ] Nothing secret or personal is logged
- [ ] I watch latency (including percentiles), traffic, errors and saturation
- [ ] Alerts are symptoms that need a person, and everything else is on a dashboard
- [ ] A health check tests the database, not just that the process is running

## Go deeper
- [OpenTelemetry docs](https://opentelemetry.io/docs/)
- [OpenTelemetry: Signals](https://opentelemetry.io/docs/concepts/signals/): traces, metrics and logs
- [OWASP Logging Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html): what not to log, and which events to log
- [Google SRE Book: Monitoring Distributed Systems](https://sre.google/sre-book/monitoring-distributed-systems/): the golden signals and what deserves a page
- [Deployment](/docs/shipping/deployment.md) and [Environments & secrets](/docs/shipping/environments-and-secrets.md)
