@@ -4,7 +4,7 @@ import { RetryOptions } from "../schemas/index.js";
44import  {  calculateNextRetryDelay  }  from  "../utils/retries.js" ; 
55import  {  ApiConnectionError ,  ApiError ,  ApiSchemaValidationError  }  from  "./errors.js" ; 
66
7- import  {  Attributes ,  Span  }  from  "@opentelemetry/api" ; 
7+ import  {  Attributes ,  Span ,   context ,   propagation  }  from  "@opentelemetry/api" ; 
88import  {  SemanticInternalAttributes  }  from  "../semanticInternalAttributes.js" ; 
99import  {  TriggerTracer  }  from  "../tracer.js" ; 
1010import  {  accessoryAttributes  }  from  "../utils/styleAttributes.js" ; 
@@ -184,9 +184,11 @@ async function _doZodFetch<TResponseBodySchema extends z.ZodTypeAny>(
184184  requestInit ?: PromiseOrValue < RequestInit > , 
185185  options ?: ZodFetchOptions 
186186) : Promise < ZodFetchResult < z . output < TResponseBodySchema > > >  { 
187-   const  $requestInit  =  await  requestInit ; 
187+   let  $requestInit  =  await  requestInit ; 
188188
189189  return  traceZodFetch ( {  url,  requestInit : $requestInit ,  options } ,  async  ( span )  =>  { 
190+     $requestInit  =  injectPropagationHeadersIfInWorker ( $requestInit ) ; 
191+ 
190192    const  result  =  await  _doZodFetchWithRetries ( schema ,  url ,  $requestInit ,  options ) ; 
191193
192194    if  ( options ?. onResponseBody  &&  span )  { 
@@ -577,3 +579,23 @@ export function isEmptyObj(obj: Object | null | undefined): boolean {
577579export  function  hasOwn ( obj : Object ,  key : string ) : boolean  { 
578580  return  Object . prototype . hasOwnProperty . call ( obj ,  key ) ; 
579581} 
582+ 
583+ // If the requestInit has a header x-trigger-worker = true, then we will do 
584+ // propagation.inject(context.active(), headers); 
585+ // and return the new requestInit. 
586+ function  injectPropagationHeadersIfInWorker ( requestInit ?: RequestInit ) : RequestInit  |  undefined  { 
587+   const  headers  =  new  Headers ( requestInit ?. headers ) ; 
588+ 
589+   if  ( headers . get ( "x-trigger-worker" )  !==  "true" )  { 
590+     return  requestInit ; 
591+   } 
592+ 
593+   const  headersObject  =  Object . fromEntries ( headers . entries ( ) ) ; 
594+ 
595+   propagation . inject ( context . active ( ) ,  headersObject ) ; 
596+ 
597+   return  { 
598+     ...requestInit , 
599+     headers : new  Headers ( headersObject ) , 
600+   } ; 
601+ } 
0 commit comments