#How would you implement correlation id (Request id) for each request ?

1 messages · Page 1 of 1 (latest)

unkempt rover
#

I'd like to have a correlation id for each request that hit the server , but since the NestJS service acts as a BFF pattern I need to access this correlation id also at a service classes that calling other services (and send them this correlation id) ?

sly mesa
#

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

rancid crescent
#

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

rough wadi
#

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