File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments