What a trace id is, and why every request should have one
A trace id ties every log line, query and metric of one request together. Here is how it works and how to add one to your app in minutes.
A user writes in: "checkout failed at about 3 o'clock". You open your logs and find four hundred lines from that minute, from forty different requests, interleaved. Which ones belong to their checkout?
A trace id answers that question. It is a random string created when a request starts and carried by everything that request does: every log line, every SQL query, every outgoing call, every metric. Filter by it and the noise disappears — you are looking at one request, start to finish.
What it looks like
A trace id is usually 16 or 32 hexadecimal characters, like 4bf92f3577b34da6a3ce929d0e0e4736. It is created once, at the edge of your app, and stored somewhere every part of the request can reach — a request attribute in Laravel, an async context in Node.
Inside one trace there are spans: one for the request itself, and one for each step worth timing inside it. Each span has its own id, a parent, a start time and a duration. Put them on a timeline and you get a waterfall:
GET /checkout ████████████████████ 842 ms
select * from carts … ██ 61 ms
POST payment-gateway/charge ███████████████ 701 ms
insert into orders … █ 12 ms
You can see at a glance that the payment call, not your database, is where the time went.
Why it matters even for a small app
- Support tickets become one click. Return the trace id in a response header (
X-Trace-Id) and show it on error pages. When someone reports a problem, they can quote it, and you jump straight to that request. - Logs stop being a haystack. "Payment gateway timed out" is useful; "Payment gateway timed out in the request where the cart had 14 items and the user was on the Pro plan" is a fix.
- Slowness gets a location. Averages tell you a route is slow. A trace tells you which line is slow.
Adding one
In Grafatsuki, the drop-in clients do it for you: the Laravel middleware and the Node client create a trace id per request, attach it to every log line and metric, record SQL queries as spans in Laravel, and add the X-Trace-Id header to the response. Anything else worth timing, you wrap:
$receipt = Grafatsuki::span('payment-gateway charge', fn () => $gateway->charge($order), 'http');
If you are sending data by hand, put the same trace_id on the logs, metrics and spans of one request — that is all the joining needs.
Reading a trace
Open any log line that has a trace id and follow its trace link. You get the waterfall, the logs written during the request in order, and the metrics it recorded. From the traces feature you can also rank routes by p95 and open their slowest requests.
The habit worth building is simple: every request gets an id, and every error message shows it. The rest — finding, timing, explaining — follows from that.