#How would you implement correlation id (Request id) for each request ?
1 messages · Page 1 of 1 (latest)
I kinda have an answer. First, you need to create a middleware that sets the request ID somewhere – easiest and convenient is to put it on request header(usually X-Request-Id). Then to access it, you'd need a request scoped provider, that way you can inject the request to the provider and get access to the request ID(in header). Downside is that those request scoped providers won't be singleton, and will be initialised every time you get a request. I am now looking for a better solution, but for now don't have anything in mind
I think using a middleware is the correct answer here and passing down.
However, there is using a Reflector seen here: https://docs.nestjs.com/fundamentals/execution-context
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
If you're looking for fully featured tracing solution, look into OpenTelemetry
If you just want to propagate request id without turning all providers into request-scoped ones, I would recommend AsyncLocalStorage
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).