@@ -17,7 +17,9 @@ import {
1717 TopicResponseContext ,
1818 HttpResponseContext ,
1919 HeaderValue ,
20+ TraceContext ,
2021} from '@nitric/api/proto/faas/v1/faas_pb' ;
22+ import * as api from '@opentelemetry/api' ;
2123import { jsonResponse } from './json' ;
2224
2325export abstract class TriggerContext <
@@ -89,9 +91,11 @@ export abstract class TriggerContext<
8991
9092export abstract class AbstractRequest {
9193 readonly data : string | Uint8Array ;
94+ readonly traceContext : api . Context ;
9295
93- protected constructor ( data : string | Uint8Array ) {
96+ protected constructor ( data : string | Uint8Array , traceContext : api . Context ) {
9497 this . data = data ;
98+ this . traceContext = traceContext ;
9599 }
96100
97101 text ( ) : string {
@@ -122,6 +126,7 @@ interface HttpRequestArgs {
122126 params : Record < string , string > ;
123127 query : Record < string , string [ ] > ;
124128 headers : Record < string , string [ ] > ;
129+ traceContext ?: api . Context ;
125130}
126131
127132export class HttpRequest extends AbstractRequest {
@@ -131,8 +136,16 @@ export class HttpRequest extends AbstractRequest {
131136 public readonly query : Record < string , string [ ] > ;
132137 public readonly headers : Record < string , string [ ] | string > ;
133138
134- constructor ( { data, method, path, params, query, headers } : HttpRequestArgs ) {
135- super ( data ) ;
139+ constructor ( {
140+ data,
141+ method,
142+ path,
143+ params,
144+ query,
145+ headers,
146+ traceContext,
147+ } : HttpRequestArgs ) {
148+ super ( data , traceContext ) ;
136149 this . method = method ;
137150 this . path = path ;
138151 this . params = params ;
@@ -174,12 +187,28 @@ export class HttpResponse {
174187export class EventRequest extends AbstractRequest {
175188 public readonly topic : string ;
176189
177- constructor ( data : string | Uint8Array , topic : string ) {
178- super ( data ) ;
190+ constructor (
191+ data : string | Uint8Array ,
192+ topic : string ,
193+ traceContext : api . Context
194+ ) {
195+ super ( data , traceContext ) ;
179196 this . topic = topic ;
180197 }
181198}
182199
200+ // Propagate the context to the root context
201+ const getTraceContext = ( traceContext : TraceContext ) : api . Context => {
202+ const traceContextObject : Record < string , string > = traceContext
203+ ? traceContext
204+ . getValuesMap ( )
205+ . toObject ( )
206+ . reduce ( ( prev , [ k , v ] ) => ( prev [ k ] = v ) , { } )
207+ : { } ;
208+
209+ return api . propagation . extract ( api . context . active ( ) , traceContextObject ) ;
210+ } ;
211+
183212export class HttpContext extends TriggerContext < HttpRequest , HttpResponse > {
184213 public get http ( ) : HttpContext {
185214 return this ;
@@ -189,23 +218,27 @@ export class HttpContext extends TriggerContext<HttpRequest, HttpResponse> {
189218 const http = trigger . getHttp ( ) ;
190219 const ctx = new HttpContext ( ) ;
191220
192- const headers = ( ( http
193- . getHeadersMap ( )
194- // getEntryList claims to return [string, faas.HeaderValue][], but really returns [string, string[][]][]
195- // we force the type to match the real return type.
196- . getEntryList ( ) as unknown ) as [ string , string [ ] [ ] ] [ ] ) . reduce (
221+ const headers = (
222+ http
223+ . getHeadersMap ( )
224+ // getEntryList claims to return [string, faas.HeaderValue][], but really returns [string, string[][]][]
225+ // we force the type to match the real return type.
226+ . getEntryList ( ) as unknown as [ string , string [ ] [ ] ] [ ]
227+ ) . reduce (
197228 ( acc , [ key , [ val ] ] ) => ( {
198229 ...acc ,
199230 [ key . toLowerCase ( ) ] : val . length === 1 ? val [ 0 ] : val ,
200231 } ) ,
201232 { }
202233 ) ;
203234
204- const query = ( ( http
205- . getQueryParamsMap ( )
206- // getEntryList claims to return [string, faas.HeaderValue][], but really returns [string, string[][]][]
207- // we force the type to match the real return type.
208- . getEntryList ( ) as unknown ) as [ string , string [ ] [ ] ] [ ] ) . reduce (
235+ const query = (
236+ http
237+ . getQueryParamsMap ( )
238+ // getEntryList claims to return [string, faas.HeaderValue][], but really returns [string, string[][]][]
239+ // we force the type to match the real return type.
240+ . getEntryList ( ) as unknown as [ string , string [ ] [ ] ] [ ]
241+ ) . reduce (
209242 ( acc , [ key , [ val ] ] ) => ( {
210243 ...acc ,
211244 [ key ] : val . length === 1 ? val [ 0 ] : val ,
@@ -259,6 +292,7 @@ export class HttpContext extends TriggerContext<HttpRequest, HttpResponse> {
259292 // check for old headers if new headers is unpopulated. This is for backwards compatibility.
260293 headers : Object . keys ( headers ) . length ? headers : oldHeaders ,
261294 method : http . getMethod ( ) ,
295+ traceContext : getTraceContext ( trigger . getTraceContext ( ) ) ,
262296 } ) ;
263297
264298 ctx . response = new HttpResponse ( {
@@ -330,7 +364,11 @@ export class EventContext extends TriggerContext<EventRequest, EventResponse> {
330364 const topic = trigger . getTopic ( ) ;
331365 const ctx = new EventContext ( ) ;
332366
333- ctx . request = new EventRequest ( trigger . getData_asU8 ( ) , topic . getTopic ( ) ) ;
367+ ctx . request = new EventRequest (
368+ trigger . getData_asU8 ( ) ,
369+ topic . getTopic ( ) ,
370+ getTraceContext ( trigger . getTraceContext ( ) )
371+ ) ;
334372
335373 ctx . response = {
336374 success : true ,
0 commit comments