Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 1x 1x 1x 1x 11x 11x 11x | import { NextFunction, Request, Response } from 'express';
import { AsyncLocalStorage } from 'async_hooks';
import { extractHeader } from '@Infrastructure/Http/Utils/Headers';
/**
* Interface for tracing context storage
*/
export interface TracingStore {
correlationId: string;
transactionId: string;
}
/**
* AsyncLocalStorage instance for storing tracing context
*/
export const asyncLocalStorage = new AsyncLocalStorage<TracingStore>();
/**
* Middleware for caching/generating tracing headers
* @param req The Express request object
* @param _ The Express response object (unused)
* @param next The next middleware function
*/
export const TracingMiddleware = (req: Request, _: Response, next: NextFunction) => {
const store: TracingStore = {
correlationId: extractHeader(req, 'x-correlation-id') ?? 'default-correlation-id',
transactionId: crypto.randomUUID(),
};
asyncLocalStorage.run(store, () => {
next();
});
}
|