GithubHelp home page GithubHelp logo

correlation-id's Introduction

Build Status Coverage Status Dependencies npm version Greenkeeper badge

Correlation id

Correlation id maintains a consistent id across asynchronous calls in node.js applications. This is extremely useful for logging purposes. For example within a web application, each incoming request can be assigned an id that will be available in all function calls made processing that request, so we can see which requests caused errors.

Installation

npm i correlation-id --save

Simple example

As demonstrated by this example, all calls to getId() within the same withId() block will return the same id. The id can be supplied, otherwise a v4 uuid will be generated.

const correlator = require('correlation-id');

function printCurrentId (name) {
  console.log('%s id: %s', name, correlator.getId());
}

correlator.withId(() => {
  setTimeout(() => {
    printCurrentId('withId block 1, call 1');
  });
  setTimeout(() => {
    printCurrentId('withId block 1, call 2');
  }, 1000);
});

correlator.withId('my-custom-id', () => {
  setTimeout(() => {
    printCurrentId('withId block 2, call 1');
  }, 500);
});

// Output:
// withId block 1, call 1 id: 5816e2d3-6b90-43be-8738-f6e1b2654f39
// withId block 2, call 1 id: my-custom-id
// withId block 1, call 2 id: 5816e2d3-6b90-43be-8738-f6e1b2654f39

API

withId([id,] work)

Executes function work within a correlation scope. Within work and any other function executions (sync or async) calls to getId() will return the same id. The id for the context may be set explicitly with the optional id parameter, otherwise it will be a v4 uuid. Calls to withId() may be nested.

correlator.withId(() => {
  console.log(correlator.getId()); // Writes a uuid to stdout
});
correlator.withId('my-custom-id', () => {
  console.log(correlator.getId()); // Writes 'my-custom-id' to stdout
});

bindId([id,] work)

Returns function work bound with a correlation scope. When work is executed all calls to getId() will return the same id. The id for the context may be set explicitly with the optional id parameter, otherwise it will be a v4 uuid. Arguments passed to the bound function will be applied to work.

const boundFunction = correlator.bindId((p1) => {
  console.log('p1 is', p1);
  console.log(correlator.getId());
});
boundFunction('foo'); // Writes 'p1 is foo' and then a uuid to stdout

const boundFunction2 = correlator.bindId('my-custom-id', (p1) => {
  console.log('p1 is', p1);
  console.log(correlator.getId());
});
boundFunction('foo'); // Writes 'p1 is foo' and then 'my-custom-id' to stdout

getId()

Returns the id for the current correlation scope (created via withId or bindId). If called outside of a correlation scope returns undefined.

correlator.getId(); // Returns the current id or undefined

How does it work?

Currently this module a slim wrapper over continuation-local-storage. I intend to move to async-hook when it's generally available.

License

MIT

correlation-id's People

Contributors

toboid avatar greenkeeper[bot] avatar greenkeeperio-bot avatar felixheck avatar

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.