You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Correlate http requests across microservices built with micro.
This a micro module for setting a correlation id per HTTP request. The correlation id associated with a request remains consistent across async calls made within the scope of the request handler.
# npm
npm i -S micro-correlation-id
# or yarn
yarn add micro-correlation-id
Usage
const{ correlator, getId }=require('micro-correlation-id');constassert=require('assert');module.exports=correlator()(async(req,res)=>{// These values will always be the same within this scopeassert.equal(req.correlationId(),getId());res.end();});
API
correlator(idHeader='x-correlation-id')
This creates and/or sets the correlation id on each incomming request. If the incoming request has a correlation id then that id is preserved, a new uuid is assigned otherwise. The correlation id may be accessed using req.correlationId() or getId().
correlator takes and optional argument idHeader which defaults to x-correlation-id. This is HTTP header key used for passing a correlation id. You may change this to your liking.
getId()
Use this to get the current correlation id from any where within a request handler. It will always be equivalent to req.correlationId().