Skip to content

Commit b6f4846

Browse files
feat: Add traceparent for all requests to API (#180)
1 parent 493c5d5 commit b6f4846

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/utils/api.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ async function enhanceFetch(
2525
}`.trim()
2626
);
2727

28+
// Add trace parent header for distributed tracing
29+
headers.set("traceparent", generateTraceParent());
30+
2831
// Create new request with updated headers and optionally add instrumentation
2932
return instrumentation
3033
? instrumentation(
@@ -169,3 +172,18 @@ export function handleResponse<D, E>(
169172

170173
return result.data.data;
171174
}
175+
176+
export function generateTraceParent(): string {
177+
// Generate W3C Trace Context traceparent header
178+
// Format: version-trace-id-span-id-trace-flags
179+
const version = "00"; // Current version is 00
180+
const traceId = Array.from({ length: 32 }, () =>
181+
Math.floor(Math.random() * 16).toString(16)
182+
).join(""); // 128-bit (32 hex chars)
183+
const spanId = Array.from({ length: 16 }, () =>
184+
Math.floor(Math.random() * 16).toString(16)
185+
).join(""); // 64-bit (16 hex chars)
186+
const traceFlags = "01"; // Sampled flag set to 1
187+
188+
return `${version}-${traceId}-${spanId}-${traceFlags}`;
189+
}

0 commit comments

Comments
 (0)