Logs and traces
What you need to know
Logs describe individual events; traces connect related work across boundaries; metrics aggregate observations. Together they help explain why a particular request was slow or failed.
By the end, you can: correlate synthetic request events; redact private fields.
Project a minimal synthetic log event
type LogEvent={route:'catalog'|'health';status:number;durationMs:number;cookie?:string};function logEvent(e:LogEvent){return {route:e.route,status:e.status,durationMs:e.durationMs};}console.log(logEvent({route:'catalog',status:200,durationMs:12,cookie:'synthetic-secret'}));Expected behavior: Only the approved fields are returned. This typed example assumes validated inputs; the fixture tests runtime validation.
How it works
A trace groups spans, each describing an operation with timing, status and a parent relationship when present. A server-generated correlation identifier can connect application logs for one request; use validated trace context across trusted services with an actual tracing library. Never treat a correlation header as proof of identity. Use structured records with a low-cardinality route label such as /catalog/[slug] instead of raw URLs, query strings or user identifiers. Whitelist approved fields and validate their values; deleting only one obvious password field leaves other sensitive content. An error digest can help connect framework errors to server diagnostics, but is not a stable business identifier or user-safe explanation by itself. Sampling reduces collected volume and affects interpretation. Never use per-request identifiers as metric label dimensions.
Connect two synthetic spans
{ "trace": "demo-trace", "spans": [ { "id": "s1", "parent": null, "operation": "catalog request", "durationMs": 40 }, { "id": "s2", "parent": "s1", "operation": "read books", "durationMs": 25 } ]}Expected behavior: A child operation occupies part of a parent span; do not sum overlapping durations as if they were sequential.
Common mistake
Do not log raw headers, form data, full errors or RSC payloads. Do not use an arbitrary URL or a unique request ID as a metrics grouping label.
Practice and verification
Run the allowlist tests and inspect rejected invalid status, duration and route inputs. Design a three-span trace with one failure and describe what can be inferred from partial sampled data.
Scope: Proposed Next.js 16.x / React 19, App Router with Cache Components disabled unless noted. Framework and browser examples are staged, not executed. Synthetic numbers are not measured performance or actual provider rates.
Further reading: Instrumentation · Production · Otel