Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tafarij/micro-correlation-id
Correlate http requests across microservices built with zeit/micro
https://github.com/tafarij/micro-correlation-id
Last synced: 3 months ago
JSON representation
Correlate http requests across microservices built with zeit/micro
- Host: GitHub
- URL: https://github.com/tafarij/micro-correlation-id
- Owner: tafarij
- License: mit
- Created: 2018-03-01T17:27:50.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-10-17T22:07:37.000Z (over 1 year ago)
- Last Synced: 2024-08-11T09:52:47.878Z (6 months ago)
- Language: JavaScript
- Size: 625 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-micro - micro-correlation-id - Correlate http requests across microservices. (Modules / HTTP Requests)
README
# micro-correlation-id
> Correlate http requests across microservices built with [micro](https://github.com/zeit/micro).
This a [micro](https://github.com/zeit/micro) module for setting a [correlation id](https://blog.rapid7.com/2016/12/23/the-value-of-correlation-ids/) per HTTP request. The correlation id associated with a request remains consistent across async calls made within the scope of the request handler.
This module is inspired by [express-correlation-id](https://github.com/toboid/express-correlation-id) and makes use of [AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage).
## Installation
```bash
# npm
npm i -S micro-correlation-id# or yarn
yarn add micro-correlation-id
```## Usage
```javascript
const { correlator, getId } = require('micro-correlation-id');
const assert = require('assert');module.exports = correlator()(async (req, res) => {
// These values will always be the same within this scope
assert.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()`.
## License
[MIT](LICENCE)