correlation-id
Advanced tools
Comparing version 3.1.2 to 4.0.0
@@ -0,3 +1,10 @@ | ||
# Changelog | ||
## 4.0.0 (16th November 2020) | ||
Change implementation to AsyncLocalStorage, no change to public API however minimum supported node version is not v12.17.0 | ||
## 3.0.0 (8th July 2018) | ||
`bindId` and `withId` now return the return value of the `work` function passed | ||
to them as a parameter. | ||
to them as a parameter. |
@@ -1,5 +0,5 @@ | ||
'use strict'; | ||
"use strict"; | ||
const http = require('http'); | ||
const correlator = require('../index.js'); | ||
const http = require("http"); | ||
const correlator = require("../index.js"); | ||
@@ -16,3 +16,3 @@ const server = http.createServer((req, res) => { | ||
function getRandomNumber (callback) { | ||
function getRandomNumber(callback) { | ||
setTimeout(() => { | ||
@@ -29,3 +29,3 @@ console.log(`${correlator.getId()} getting random number`); | ||
} | ||
console.log('Example app listening on port 3000'); | ||
console.log("Example app listening on port 3000"); | ||
}); |
39
index.js
@@ -1,7 +0,7 @@ | ||
'use strict'; | ||
"use strict"; | ||
const uuid = require('uuid'); | ||
const cls = require('cls-hooked'); | ||
const { AsyncLocalStorage } = require("async_hooks"); | ||
const uuid = require("uuid"); | ||
const store = cls.createNamespace('1d0e0c48-3375-46bc-b9ae-95c63b58938e'); | ||
const asyncLocalStorage = new AsyncLocalStorage(); | ||
@@ -11,22 +11,14 @@ module.exports = { | ||
bindId: configureArgs(bindId), | ||
getId | ||
getId, | ||
}; | ||
function withId (id, work) { | ||
return store.runAndReturn(() => { | ||
store.set('correlator', id); | ||
return work(); | ||
}); | ||
function withId(id, work) { | ||
return asyncLocalStorage.run({ id }, () => work()); | ||
} | ||
function bindId (id, work) { | ||
return function () { | ||
return store.runAndReturn(() => { | ||
store.set('correlator', id); | ||
return work.apply(null, [].slice.call(arguments)); | ||
}); | ||
}; | ||
function bindId(id, work) { | ||
return (...args) => asyncLocalStorage.run({ id }, () => work(...args)); | ||
} | ||
function configureArgs (func) { | ||
function configureArgs(func) { | ||
return (id, work) => { | ||
@@ -37,3 +29,3 @@ if (!work && isFunction(id)) { | ||
} | ||
if (!work) throw new Error('Missing work parameter'); | ||
if (!work) throw new Error("Missing work parameter"); | ||
@@ -44,8 +36,9 @@ return func(id, work); | ||
function isFunction (object) { | ||
return typeof object === 'function'; | ||
function isFunction(object) { | ||
return typeof object === "function"; | ||
} | ||
function getId () { | ||
return store.get('correlator'); | ||
function getId() { | ||
const store = asyncLocalStorage.getStore(); | ||
return store && store.id; | ||
} |
{ | ||
"name": "correlation-id", | ||
"version": "3.1.2", | ||
"version": "4.0.0", | ||
"description": "Correlation id for node.js", | ||
@@ -11,7 +11,11 @@ "main": "index.js", | ||
], | ||
"engines": { | ||
"node": ">=12.17.0" | ||
}, | ||
"scripts": { | ||
"test": "ava", | ||
"coverage": "nyc --check-coverage --lines 100 npm test", | ||
"lint": "eslint index.js test/*.js examples/*.js", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
"lint": "prettier --write . && eslint .", | ||
"fmt": "prettier --write .", | ||
"test": "jest", | ||
"coverage": "jest --coverage", | ||
"coveralls": "coveralls < coverage/lcov.info" | ||
}, | ||
@@ -34,12 +38,11 @@ "repository": { | ||
"dependencies": { | ||
"cls-hooked": "^4.2.2", | ||
"uuid": "^3.3.2" | ||
"uuid": "^8.3.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.13.0", | ||
"coveralls": "^3.0.3", | ||
"eslint": "^6.6.0", | ||
"eslint-config-toboid": "^2.0.0", | ||
"nyc": "^15.0.0" | ||
"coveralls": "^3.1.0", | ||
"eslint": "^7.13.0", | ||
"eslint-config-prettier": "^6.15.0", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.1.2" | ||
} | ||
} |
# 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. | ||
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 | ||
```shell | ||
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. | ||
``` javascript | ||
const correlator = require('correlation-id'); | ||
function printCurrentId (name) { | ||
console.log('%s id: %s', name, correlator.getId()); | ||
```javascript | ||
const correlator = require("correlation-id"); | ||
function printCurrentId(name) { | ||
console.log("%s id: %s", name, correlator.getId()); | ||
} | ||
@@ -20,12 +29,12 @@ | ||
setTimeout(() => { | ||
printCurrentId('withId block 1, call 1'); | ||
printCurrentId("withId block 1, call 1"); | ||
}); | ||
setTimeout(() => { | ||
printCurrentId('withId block 1, call 2'); | ||
printCurrentId("withId block 1, call 2"); | ||
}, 1000); | ||
}); | ||
correlator.withId('my-custom-id', () => { | ||
correlator.withId("my-custom-id", () => { | ||
setTimeout(() => { | ||
printCurrentId('withId block 2, call 1'); | ||
printCurrentId("withId block 2, call 1"); | ||
}, 500); | ||
@@ -41,3 +50,5 @@ }); | ||
## 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. | ||
@@ -49,3 +60,3 @@ | ||
}); | ||
correlator.withId('my-custom-id', () => { | ||
correlator.withId("my-custom-id", () => { | ||
console.log(correlator.getId()); // Writes 'my-custom-id' to stdout | ||
@@ -56,2 +67,3 @@ }); | ||
### `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`. | ||
@@ -61,15 +73,16 @@ | ||
const boundFunction = correlator.bindId((p1) => { | ||
console.log('p1 is', p1); | ||
console.log("p1 is", p1); | ||
console.log(correlator.getId()); | ||
}); | ||
boundFunction('foo'); // Writes 'p1 is foo' and then a uuid to stdout | ||
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); | ||
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 | ||
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`. | ||
@@ -81,6 +94,4 @@ | ||
## How does it work? | ||
Currently this module is a slim wrapper over [cls-hooked](https://github.com/jeff-lewis/cls-hooked). I intend to move to async-hook in future. | ||
## License | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
1
92
6851
6
55
1
2
+ Addeduuid@8.3.2(transitive)
- Removedcls-hooked@^4.2.2
- Removedasync-hook-jl@1.7.6(transitive)
- Removedcls-hooked@4.2.2(transitive)
- Removedemitter-listener@1.1.2(transitive)
- Removedsemver@5.7.2(transitive)
- Removedshimmer@1.2.1(transitive)
- Removedstack-chain@1.3.7(transitive)
- Removeduuid@3.4.0(transitive)
Updateduuid@^8.3.1