practica
practica copied to clipboard
Add unique request-id (correlation-id) to every request and log statement
🎯 Goal: - The ops/dev should be able to filter out logs and see all the entries of the same request. To achieve this, a unique correlation-id should be added to every request's log statement
🤔 Things to consider:
- The implementation should minimally impact the majority of the code: Probably just an initialization when a request starts, then the logger should try to identify whether some store/context/request-id exists and append to logs
- The standard mechanism in Node.js to achieve this is AsyncLocalStorage, we may use it directly or choose a library that abstracts it. If choosing a library, we may prefer those who use AsyncLocalStorage under the hood and follow our vendor pick guidelines
- Outgoing HTTP requests should contain the correlation id so other Microservice can reuse the same id
- This might have impact on performance - Could be useful to benchmark before and after
🏎 Quick iteration: There are many optimization and testing that can be done here. Let's start with a quick and simple iteration that contains the basics - Initialize a unique id on request start, ensure it's included in all the log entries of the request
I want to take it.
@marcosmol204 Welcome aboard!
That could be awesome to have you participating in this important improvement. Note that it's already work in progress, @rluvaton is handling but I definitely see here an opportunity for two to collaborate on this
@rluvaton I would like to collaborate also. I have an implementation in my head that is not a breaking change, use AsyncLocalStorage
@rluvaton Maybe you can share with @marcosmol204 what was done thus far
Hey @marcosmol204 thanks for joining in
- [x] I made the logger be able to log objects #228
- [x] Adding Async Local Storage package #232
- [ ] (Todo) And then I'll create a
request-id
package that will have the request ID generator and express middleware for using/generating the request id and set it on the response headers
So I want to take request-id package!
@rluvaton @marcosmol204
The plan in overall sounds great, I'm having thoughts about whether we should wrap it all in a single package - request-scoped-store
Why - The proposed package request-id is dependent by nature on the library async-local-storage library, they are both highly dependent on each other (to provide decent functionality)- It makes sense to encapsulate this in a single package with two objects: requestStoreWrapper, request-id-middleware
Just my thoughts...
Hey @marcosmol204 I had some free time now so I created a PR, I would appreciate if you review it though, also if you want there are some other issues that need your help