The portfolio screen took nine seconds to load for one collector, and I would have sworn the phone was the slow part. One database query was eating 8.3 of those 9 seconds.
I only know that because earlier in the week I turned tracing up to 100% on every API route of Binderdex, a Pokémon card collection and price app. The portfolio screen shows a collector's cards and sealed products and their total value over time. Before that, the web server traced one request in ten and recorded zero database spans, so the slow part of any request was a blank. Getting to a trace I could read took four fixes before I touched the query, and that order is the lesson.
Sample every API route and drop the noise
A trace is a timed tree: the request at the top, and each piece of work it caused as a child span underneath. Full sampling is affordable when the sampler refuses the requests that teach nothing.
function sampleRate(path: string, parentDecision: number): number {
if (path === "/api/health") return 0; // probes are not insights
if (path.startsWith("/api/")) return 1.0; // full fidelity where it counts
if (path.startsWith("/_next/")) return 0; // static assets teach nothing
return parentDecision || 0.1; // pages follow their parent
}
Health probes and static assets are sampled at zero, API routes at one, and page requests follow their parent's decision or fall back to 10%.
Full sampling alone gave me request traces with no database children and no error explaining why. The tracing hooks can only wrap the Postgres driver if it loads from node_modules, which means listing pg in serverExternalPackages. Skip that and the driver runs unpatched.
The phone that lost 6.8 days
Mobile tracing produced the strangest bug of the week. Transactions arrived with durations measured in days, and every one of them was rejected as invalid.
On React Native 0.86, performance.timeOrigin drifts behind Date.now() by however long the device has slept. One test device had accumulated 6.8 days. The Sentry SDK, the library that reports errors and traces to my tracking service, reads that clock once, lazily, on first use, so every span was anchored to a timestamp from last week.
The guard runs before Sentry.init() and pins the clock when it's off by more than a second.
const drift = Date.now() - performance.timeOrigin;
if (Math.abs(drift) > 1000) {
performance.timeOrigin = Date.now();
}
Newer versions of the mobile SDK do this alignment themselves. I'm pinned to an older major until a native build bump, so I carry the guard for now.
Production shipped no spans at all
The web deploy went out and the dashboard stayed empty, while everything worked locally. Production runs Next's standalone output, which builds its folder by tracing which files the server imports. That tracer left out instrumentation.js and the chunk holding the server config. The Postgres instrumentation is loaded dynamically, and file tracers never follow dynamic loads. The Dockerfile now copies those files in explicitly. If a standalone container loses tracing, check this class of bug first.
Paying the noise tax
Once everything reported, I could see how much of it was junk. 65% of my error volume came from development builds, and one device had emitted 69,000 network-error events in 28 days. Tracing everything only works if you silence what isn't signal. Events now fire from production builds only, mobile events are rate-limited per key, and a handful of benign browser errors are ignored outright.
8.3 seconds of the same join
With clean traces flowing, the portfolio screen named its culprit within a day. The collector had about 1,800 holdings, meaning individual cards and sealed products they own, and one query joined a 30-million-row prices table once per holding.
Many of those joins fetched identical data. Two copies of the same card in the same condition share one price series. The query now groups holdings by price key, the identifier for one card in one condition, joins once per unique key, and multiplies by the count on the way out.
const byKey = groupBy(holdings, (h) => priceKey(h));
const rows = await joinPrices(Object.keys(byKey)); // ~900 joins instead of ~1,800
for (const row of rows) {
const count = byKey[row.key].length;
series[i] += row.prices[i] * count;
}
Rows returned dropped by about half, and warm timings roughly halved with them. The 30-day series went from 326ms to 170ms, and the 1-year series from 2.6s to 1.3s.
Cache the points that can't change
Long-period series now cache for 24 hours per user, period and collection revision, a version that changes when the collection does. Points before today are immutable once the overnight pipeline writes them, so only the newest point is overwritten with the live total. That keeps the chart from disagreeing with the headline number. The 1-day and 7-day series stay live.
Instrument first, guess second
You can't tune what you can't see, and intuition is a terrible profiler. I would have sworn the slowness was the phone, and it was one query joining the same price series 1,800 times. Before I trust a hunch about a slow screen now, I check that the request shows its database spans.