(
/* istanbul ignore next */
options: LambdaRequestTrackerOptions = {},
)
| 11 | * @param options The request options |
| 12 | */ |
| 13 | export const lambdaRequestTracker = ( |
| 14 | /* istanbul ignore next */ |
| 15 | options: LambdaRequestTrackerOptions = {}, |
| 16 | ) => (event: LambdaEvent, context: LambdaContext): void => { |
| 17 | const ctx: ContextMap = { |
| 18 | awsRequestId: context.awsRequestId, |
| 19 | }; |
| 20 | |
| 21 | // capture api gateway request ID |
| 22 | const apiRequestId = event.requestContext?.requestId; |
| 23 | if (apiRequestId) { |
| 24 | ctx.apiRequestId = apiRequestId; |
| 25 | } |
| 26 | |
| 27 | // capture any correlation headers sent from upstream callers |
| 28 | if (event.headers) { |
| 29 | for (const [header, value] of Object.entries(event.headers)) { |
| 30 | if (header.toLowerCase().startsWith(CORRELATION_HEADER)) { |
| 31 | ctx[header] = value; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // capture the xray trace id if its enabled |
| 37 | if (process.env[AMAZON_TRACE_ID]) { |
| 38 | ctx[CORRELATION_TRACE_ID] = process.env[AMAZON_TRACE_ID]; |
| 39 | } |
| 40 | |
| 41 | // set the correlation id if not already set by upstream callers |
| 42 | /* istanbul ignore next */ |
| 43 | if (!ctx[CORRELATION_ID]) { |
| 44 | ctx[CORRELATION_ID] = context.awsRequestId; |
| 45 | } |
| 46 | |
| 47 | // handle custom request level mixins |
| 48 | if (options.requestMixin) { |
| 49 | const result = options.requestMixin(event, context); |
| 50 | for (const key in result) { |
| 51 | // when the JSON serializer runs, by default it omits undefined properties |
| 52 | ctx[key] = result[key]; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | const storageProvider = options.storageProvider || GlobalContextStorageProvider; |
| 57 | /* istanbul ignore next */ |
| 58 | if (storageProvider) { |
| 59 | storageProvider.setContext(ctx); |
| 60 | } |
| 61 | }; |
no outgoing calls
searching dependent graphs…