Observability: logs, metrics and traces

Structured logs, the RED and USE metrics, distributed tracing with OpenTelemetry, and alerts on what users feel.

L2 · Mid-level engineer≈ 5 min read
Key points · the two-minute version
  1. Logs say what happened, metrics say how the system behaves, traces say where the time went.
  2. Use RED for services (Rate, Errors, Duration) and USE for resources.
  3. Write structured JSON logs with a propagated request ID, and no secrets or personal data.
  4. Alert on user-facing symptoms and SLO burn. Every alert links a runbook.
  5. AI adds fields per call: model, prompt version, tokens, cost, TTFT, outcome.
You should be able to
  • Emit structured logs with request IDs and no secrets
  • Choose metrics and alert thresholds that matter
  • Trace a request across services to find where time goes

Why this matters

You can't fix what you can't see. Observability answers "is it working, and if not, where?" in minutes instead of hours. AI systems need it more than most: quality can degrade without a single error, and cost can spike without a traffic change.

The three signals

Signal Answers Example
Logs What happened in this specific event? {"event":"llm.retry","provider":"x","attempt":2,"request_id":"..."}
Metrics How is the system behaving overall? p95 latency, error rate, queue depth, cost per hour
Traces Where did time go in this request, across services? Span tree: API → retrieval → model → validation
  • RED for services: Rate, Errors, Duration.
  • USE for resources: Utilisation, Saturation, Errors.
  • OpenTelemetry is the vendor-neutral standard for emitting all three.

How to do it

logger.info("evaluation.completed", extra={
    "request_id": request_id, "tenant_id": tenant_id,
    "model": model, "prompt_version": PROMPT_VERSION,
    "tokens_in": usage.input_tokens, "tokens_out": usage.output_tokens,
    "latency_ms": elapsed_ms, "outcome": "pass",
})   # never the prompt text, document content, email or API key
  • Structured (JSON) logs with consistent field names, searchable by request_id.
  • Propagate a request ID from the edge through every service and job.
  • Log field names, not values, for sensitive operations: an audit trail can record "updated fields: diagnosis, address" without the diagnosis (P12).

Alerting

Alert on symptoms users feel (error rate above X% for 5 minutes, p95 above the SLO, queue age growing, cost per hour above budget). Every alert must be actionable and link to a runbook. Define SLOs (for example "99.5% of requests succeed in under 2 s over 30 days") and alert on burning the error budget too fast.

What changes for AI

Add per-call fields: model and version, prompt version, tokens in/out, cache hits, cost, latency split into time-to-first-token and total, outcome (success, schema violation, refusal, fallback). Then dashboards for cost per successful task and quality signals. Part 10 covers this in depth (Q36).

Common mistakes

  • Unstructured print statements.
  • Logging full prompts and documents containing personal data.
  • Alert fatigue from noisy, non-actionable alerts.
  • Only averages on dashboards.

Interview follow-ups

  • "Logs, metrics or traces: which first?" Metrics to know something's wrong, traces to find where, logs to see exactly what happened. Start with RED metrics and request IDs in logs.
  • "What's an SLO and an error budget?" A target (99.5% under 2 s over 30 days); the budget is the allowed failure (0.5%). Burning it fast pauses risky releases.
  • "High-cardinality labels?" Don't put user IDs or request IDs in metric labels (it explodes storage); put them in logs and traces.

Check yourself

What are RED metrics?

Rate (requests per second), Errors (failed requests), Duration (latency distribution, as percentiles). The minimum dashboard for any service.

Why propagate a request ID?

So you can find every log line and span for one user's request across the API, workers and external calls, which turns a vague bug report into a precise timeline.

Build it

Add JSON logging with request IDs to your service, a /metrics endpoint (Prometheus format) with RED metrics, and OpenTelemetry tracing for one outbound call; view a trace locally (Jaeger in Docker).

What comes next

5.5 Application security.

Build it to remember

Two hands-on projects that exercise this lesson. Pick one, build it end to end, and keep it in your portfolio.

Project 1

Observability kit for your API

Instrument a FastAPI service with traces, RED metrics and JSON logs, build a dashboard, and set an SLO alert.

  • OpenTelemetry
  • Prometheus
  • Grafana
  • Loki
  • Jaeger
  • Docker Compose

Practises The three signals working together.

Project 2

LLM call tracer

Trace every model call with prompt version, tokens, cost and outcome, and build a cost-per-task view.

  • Langfuse (self-hosted)
  • Python
  • an LLM API

Practises AI-specific observability.

Can you answer every check-yourself question out loud without opening the answer?