Blog

Trace a React click all the way to the SQL query

The Network tab says the request took a second. The Spring logs say something else. Here is how to get both on one timeline.

The gap between the browser and the server

A click in the browser and the SQL it triggers are usually debugged in separate tools. The Network tab knows when the request left and returned. The server knows what it did in between. Nothing joins them, so you line up timestamps by hand and hope the clocks agree.

One header joins them

Distributed tracing solves this with a single standard header, W3C traceparent. It carries a trace ID and the ID of the span that sent the request. If the browser sends it and the server reads it, both sides write their spans into the same trace.

POST /api/orders
traceparent: 00-8969ad7e690b50a8721f9fd8e0e5c925-00f067aa0ba902b7-01

Causeline uses only this standard header. There is nothing proprietary on the wire, so it also works with any OpenTelemetry setup you already have.

Name the click

On the React side, @causeline/react wraps fetch and XHR and adds the header to calls to your own API. Wrap a handler in trace() and the click becomes the root of the trace, with every request and state update inside it.

Checkout.tsx
const onCheckout = () =>
  trace('Checkout', async () => {
    const res = await fetch('/api/orders', { method: 'POST', body });
    setStatus(res.ok ? 'paid' : 'failed');
  });

The SDK does nothing in production builds, so it is safe to leave in.

Spring Boot builds on Micrometer

The Spring Boot starter reads the header and builds on Micrometer Observation. Controllers, @Observed services, repositories, transactions, JDBC or R2DBC queries, outbound HTTP calls and messages all become spans in the same trace. Add causeline.enabled: true in a dev profile and open /causeline.

Order by the tree, not by clocks

The browser and the server have different clocks. Causeline orders spans by the parent-child tree rather than by timestamps across sources, and places the server’s spans inside the browser’s request span. That is why the timeline reads correctly even when the two clocks are a few hundred milliseconds apart.

Then it names the culprit

With everything on one tree, the slow part is a calculation, not a guess. Causeline ranks spans by self time, a span’s duration minus the time its children ran, so a slow parent never hides the real culprit. In the demo checkout, one outbound payment call takes 844 ms of a 960 ms request.

BottleneckPOST /fake-payment/charge, 844 ms, 88% of the checkout.

The how it works page walks through each step, and the install page gets the first trace on screen in about five minutes.