Observability: Logs, Metrics & Traces
Use logs, metrics, and traces with RED/USE, symptom-based alerts, SLO error budgets, and OpenTelemetry—and know what a static site needs vs a backend.
TL;DR
Metrics tell you something is wrong (RED for services, USE for resources), traces show where a slow request spent time across services, and logs explain why a specific request failed. Page only on user-facing symptoms (SLO burn), and standardize instrumentation with OpenTelemetry.
Intent
Understand how logs, metrics, and traces complement each other, how SLOs and error budgets stop alert fatigue, and how much observability a static site needs compared to a distributed backend.
Problem
Without structured observability, teams either fly blind ("users say it's slow") or drown in noisy alerts ("CPU > 80% on node 4") while real checkout failures go unnoticed. Dumping unstructured console.log lines without trace_id or latency histograms makes debugging multi-service incidents guesswork.
Solution
1. Logs vs Metrics vs Traces
| Signal | What it is | Best question it answers |
|---|---|---|
| Metrics | Numeric time-series (rate, histogram) | Is error rate or p99 latency spiking right now? |
| Traces | Distributed call tree (trace_id + spans) | Which downstream service or SQL query took 800 ms? |
| Logs | Structured JSON events with trace_id | What exact payload or exception caused this request to fail? |
2. RED, USE, and symptom-based alerting
- RED (services): Rate, Errors, Duration.
- USE (resources — CPU, RAM, disk, pools): Utilization, Saturation, Errors.
- Alert on symptoms, debug with causes: Page an engineer when an SLO (e.g., 99.9% of requests
< 500 msand non-5xx) burns its error budget too fast—not every time a single Pod hits 85% CPU.
3. Static site vs backend needs
- A static site like Mengty Learns: Pre-rendered HTML/CSS/JS has no database, no queue, and no microservices. When deployed to Cloudflare Workers (
docs/deploy/CLOUDFLARE-WORKERS-PLAN.md), built-in Workers Logs & Analytics (status codes, edge latency, 4xx/5xx counts) plus synthetic uptime checks are plenty. Running Prometheus + Tempo + Loki for static files would be pure overhead. - A multi-service backend: Needs OpenTelemetry SDKs emitting OTLP metrics, traces, and structured logs through an OTel Collector so you can correlate a 500 spike across API, Redis, Postgres, and async workers.
Real-world analogy
▶ Flow: watch it work
OpenTelemetry Pipeline: App → Collector → Metrics & Traces
Press Start or tap any step dot to trace the path.
▸All steps (5)
Code
Emit structured JSON logs with trace_id and duration_ms—never console.log("error") strings or PII:
// Structured JSON log helper (not run)
export function logRequest(entry: {
traceId: string;
route: string;
status: number;
durationMs: number;
}) {
// Never log passwords, Authorization headers, or raw session tokens
console.log(JSON.stringify({ ts: new Date().toISOString(), ...entry }));
}When you DON'T need this
- Static sites and small personal apps (like this one): Platform analytics (Cloudflare Workers Observability / HTTP status logs) + CI checks cover 99% of needs without self-hosting Grafana, Prometheus, or Jaeger.
- High-cardinality metrics traps: Putting
user_idor full URLs with query strings as Prometheus metric labels explodes time-series cardinality and can crash your metrics server—keep high-cardinality IDs in logs and traces, not metric labels. - Cost / complexity trade-off: Full observability stacks can easily cost 10–25% of your compute bill (ingestion + retention for verbose debug logs and 100% trace sampling). Control cost with structured
info/errorlog levels, tail/head trace sampling, and short raw-log retention.
When to use / when to avoid
- Any production backend where you need to prove SLO compliance and debug latency across DBs/queues.
- Standardizing instrumentation with OpenTelemetry so you can swap backends without rewriting app code.
- Putting unbounded user IDs or request IDs into metric labels (cardinality explosion).
- Logging secrets, tokens, or raw PII into centralized log stores.
How to implement
Start with structured JSON logs and RED metrics
Log JSON with traceId, status, and durationMs, and track request rate, errors, and p95/p99 latency.
Define one simple SLO and error budget
For example: 99.9% of /api/* requests return non-5xx within 500 ms over a rolling 30-day window.
Add OpenTelemetry tracing when calls cross services
Propagate W3C traceparent headers across HTTP and queues so one trace_id ties every hop together.
Pros / Cons
- OpenTelemetry prevents vendor lock-in across Grafana, Honeycomb, Datadog, or cloud native tools.
- Symptom-based SLO alerts cut midnight false alarms dramatically.
- High ingestion and storage costs if log levels and trace sampling are left unbounded.
- Operating a self-hosted telemetry stack adds its own maintenance burden.
Relations
Check yourself
Check Yourself
1. Why should paging alerts trigger on user-facing symptoms (such as 5xx error-budget burn) rather than CPU > 80%?
2. Why is adding user_id as a label on a Prometheus HTTP request counter dangerous?