Prompt
How do I get logs, metrics, and tracing for every API request?
Latest observation
Oct 6, 2026
OpenAI APIWeb search: off
To get logs, metrics, and tracing for every API request, you want observability middleware in your API layer, plus an export pipeline to a backend that stores/visualizes the data.
1) What to capture on every request
For each request, collect:
- Logs: method, path, status code, latency, request ID, user/tenant ID if available, error details
- Metrics: request count, latency histogram, error rate, throughput, payload size
- Traces: a span per request, plus child spans for downstream calls (DB, cache, HTTP, queue)
2) Add request middleware/interceptors
At the API boundary, add middleware that:
- Starts a trace span
- Injects/extracts trace context from headers
- Generates or forwards a correlation/request ID
- Measures duration
- Logs structured request/response info
- Emits metrics to a metrics system
Typical headers:
traceparent/tracestatefor distributed tracing (W3C Trace Context)X-Request-Idor similar for correlation
3) Use OpenTelemetry as the standard
The easiest modern approach is OpenTelemetry (OTel):
- Tracing: automatic + manual spans
- Metrics: request counters, latency histograms
- Logs: structured logs correlated with trace/span IDs
Recommended architecture:
- Your app → OpenTelemetry SDK
- OTel SDK → OTel Collector
- Collector → backends:
- Traces: Jaeger / Tempo / Honeycomb / Datadog / New Relic
- Metrics: Prometheus / Grafana Mimir / Datadog
- Logs: Loki / Elasticsearch / Cloud logging
4) Make logs trace-aware
Include these fields in every log line:
trace_idspan_idrequest_idserviceenvironmentroutestatus_codeduration_ms
This lets you jump from a log entry to the exact trace.
5) Instrument downstream dependencies too
To get full tracing, instrument:
- database queries
- cache calls
- outbound HTTP/gRPC requests
- message queue publish/consume
Otherwise you’ll only see the top-level request span.
6) Example pattern
For each incoming request:
- start span:
HTTP GET /users/:id - record attributes:
http.methodhttp.routehttp.status_codeenduser.id(if appropriate)
- increment metric:
http_requests_total{route,method,status} - observe latency histogram
- log structured event with trace ID
7) Practical stack examples
Simple stack
- App instrumentation: OpenTelemetry
- Traces: Jaeger or Grafana Tempo
- Metrics: Prometheus + Grafana
- Logs: Loki + Grafana
Managed stack
- Datadog / New Relic / Honeycomb / Azure Monitor / AWS X-Ray + CloudWatch
8) Important best practices
- Don’t log secrets, tokens, passwords, or full PII
- Sample traces if traffic is high, but keep error traces always
- Use structured JSON logs
- Propagate context across async jobs and background workers
- Add a unique request ID at the edge (gateway/load balancer)
9) If you want “every request” specifically
You need all three of these:
- Always log a request summary
- Always count request metrics
- Always start a trace span for incoming requests
If you tell me your stack (e.g. Node/Express, Python/FastAPI, Go, Java/Spring, .NET, Kubernetes, AWS, etc.), I can give you a concrete setup and code example.