Distributed Tracing
import { Aside } from ‘@astrojs/starlight/components’;
What’s a trace ID?
Section titled “What’s a trace ID?”A trace ID is a string that links related log entries together. You generate one at the start of an operation and pass it to every log in that operation.
const traceId = `checkout-${orderId}-${Date.now()}`;
gun.info({ bucket: "api", message: "Checkout started", traceId, tags: { step: "start" },});
// ... latergun.info({ bucket: "payment", message: "Payment processed", traceId, tags: { step: "payment" },});
// ... latergun.info({ bucket: "api", message: "Checkout completed", traceId, tags: { step: "complete" },});In the desktop app, you can search or filter by trace ID to see the entire flow of an operation across buckets.
Generating trace IDs
Section titled “Generating trace IDs”Use whatever format makes sense for your app. Some patterns:
// UUID-basedconst traceId = crypto.randomUUID();
// Operation-basedconst traceId = `checkout:${orderId}`;
// Timestamp-basedconst traceId = `req-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;The only requirement is that it’s a string. Make it descriptive enough that you can identify the operation when you see it in the log viewer.
Cross-service tracing
Section titled “Cross-service tracing”If your frontend calls your backend, pass the trace ID in a header:
// Frontendconst traceId = crypto.randomUUID();
gun.info({ bucket: "ui", message: "Fetching order", traceId,});
const res = await fetch("/api/orders", { headers: { "x-trace-id": traceId },});// Backendapp.post("/api/orders", (req, res) => { const traceId = req.headers["x-trace-id"];
gun.info({ bucket: "api", message: "Processing order", traceId, });});Both logs — frontend and backend — share the same trace ID. Filter by it in the desktop app to see the full request lifecycle.