Coder Social home page Coder Social logo

micro-correlation-id's Introduction

micro-correlation-id

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.

This module is inspired by express-correlation-id and makes use of AsyncLocalStorage.

Installation

# npm
npm i -S micro-correlation-id

# or yarn
yarn add micro-correlation-id

Usage

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.