Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

correlation-id

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

correlation-id

Correlation id for node.js

  • 4.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
43K
increased by1.07%
Maintainers
1
Weekly downloads
 
Created
Source

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 an API, 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

Compatibility

From v4 onwards this library requires node >=12.17.0. For older node versions use v3.x.

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 and returns any result returned from work. 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());
});
boundFunction2("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

License

MIT

Keywords

FAQs

Package last updated on 16 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc