Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

traceability

almerindo2.1kMIT3.2.2TypeScript support: included

Traceability tools - middleware and winston logger

traceability, middleware, logger, winston, trackId, correlation id, winston, AsyncLocalStorage, async-hooks, context, open telemetry, express

readme

Buy Me A Coffee

TypeScript version Node.js version APLv2 Build Status

Install

NOTE This lib needs to run on version >= 14.0.0 of Nodejs After the veersion 1.7 there is a BREAKING CHANGE!

Using Yarn

yarn install traceability

Using npm

npm install traceability

Example Code

Logging the trackId in all methods

File: index.js `js import * as traceability from 'traceability'

const levelRoot = () =>{ const {cid} = traceability.ContextAsyncHooks.getTrackId({}) traceability.ContextAsyncHooks.setContext({cid}) traceability.Logger.info('levelRoot'); level1()

} const level1 = () =>{ traceability.Logger.info('level1'); level2(); }

const level2 = () =>{ traceability.Logger.info('level2'); }

levelRoot();

### Output

{"message":"levelRoot","level":"info","cid":"6b1552d0-0db5-4d7d-8551-567847703fa6","timestamp":"2021-05-03T16:21:01.523Z"} {"message":"level1","level":"info","cid":"6b1552d0-0db5-4d7d-8551-567847703fa6","timestamp":"2021-05-03T16:21:01.525Z"} {"message":"level2","level":"info","cid":"6b1552d0-0db5-4d7d-8551-567847703fa6","timestamp":"2021-05-03T16:21:01.525Z"}

> **NOTE:** We can observe the same trackId value for all output messages.

## Changing the wiston configuration
```js
import { ContextAsyncHooks, Logger, LoggerTraceability } from 'traceability';


const conf = LoggerTraceability.getLoggerOptions();
conf.silent = true;

LoggerTraceability.configure(conf);

Logger.info('levelRoot');

Using as a Express middleware

File express.js (see examples directory) `js import express from 'express'; import { ContextAsyncHooks, Logger } from 'traceability';

const app = express(); const port = 3000;

ContextAsyncHooks.trackKey= ETrackKey['cid']

app.use(ContextAsyncHooks.getExpressMiddlewareTracking()); app.get('/', (req, res) => { Logger.info('Hello World with trackId on server side!'); res.send('Hello World!'); });

app.listen(port, () => { Logger.info(Example app listening at http://localhost:${port}); });



## Using as a Express middleware with traceparent propagator
> **File** express-traceparent.ts (see examples directory)
```js
import express from 'express';
import { ContextAsyncHooks, Logger } from 'traceability';


const app = express();
const port = 3000;

ContextAsyncHooks.trackKey= ETrackKey['cid']

app.use(ContextAsyncHooks.getExpressMiddlewareTracking({
  responseHeaderPropagator: 'traceparent'
}));
app.get('/', (req, res) => {
  Logger.info('Hello World with trackId on server side!');
  res.send('Hello World!');
});

app.listen(port, () => {
  Logger.info(`Example app listening at http://localhost:${port}`);
});

When traceparent is enabled, the library will automatically generate and propagate the traceparent header in all outgoing requests, following the W3C Trace Context format: traceparent: 00-<trace-id>-<span-id>-<trace-flags>

Running the code:

yarn ts-node ./express.ts

And after that we will see the following output:

{"message":"Example app listening at http://localhost:3000","level":"info","timestamp":"2021-04-10T19:01:33.337Z"}

Testing the route GET /

We will test the route usig curl cli.

To install curl on linux: sudo apt-get install curl

Now type the following command:

curl -i http://localhost:3000

-i option will show you the header.


The output will look something like:

On ClientSide:

HTTP/1.1 200 OK
X-Powered-By: Express
cid: 79a0e7f2-3e02-49aa-9368-582b9cce6002
Content-Type: text/html; charset=utf-8
Content-Length: 12
ETag: W/"c-Lve95gjOVATpfV8EL5X4nxwjKHE"
Date: Mon, 03 May 2021 17:00:06 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Hello World!%

On ServerSide:

{"message":"Hello World with trackId on server side!","level":"info","cid":"6ecf583a-7509-4ce3-baef-1fcbe94adc5c","timestamp":"2021-05-03T16:57:32.116Z"}

NOTE: At the momennt, we can compare the cid values printed onn server side and on client side.

Using as a Nest middleware - Global Middleware

First of all, follow the First Steps accessing the link NESTJS Oficial Docs. After that, just modify the main.ts file as described bellow.

File main.ts (see examples directory) `js import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import traceability from 'traceability';

const middlewareTracking = traceability.ContextAsyncHooks.getExpressMiddlewareTracking();

async function bootstrap() { const app = await NestFactory.create(AppModule); app.use(middlewareTracking); await app.listen(3000); } bootstrap();


## Using methods from winston

Just destructure the necessary methods directly from traceability

```js
import {format, addColors} from 'traceability';

format and addColors comes from winston

Known issues

TypeError: async_hooks_1.AsyncLocalStorage is not a constructor:

 this.asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();

 TypeError: async_hooks_1.AsyncLocalStorage is not a constructor
    at new ContextAsyncHooks

This lib uses LocalStorageAsyncHooks as a feature that is only available on Node >= 14.0.0

Then, to solve it, you must migrates your node to 14.0.0. Type: nvm use 14.16.1

License

See the LICENSE file for details.

changelog

3.2.2 (2024-10-31)

Bug Fixes

3.2.1 (2024-10-31)

Bug Fixes

3.2.0 (2024-10-31)

Bug Fixes

Features

  • enable clients to getTraceParent (b823657)
  • enable traceparent as a context propagator option (fc338ff)

3.1.1 (2022-10-19)

Bug Fixes

  • turn param option on getTrackId (aa50627)

3.1.0 (2022-10-18)

Features

  • add get default context on get trackId (de1a3b5)

3.0.6 (2022-09-12)

Bug Fixes

  • Merge pull request #25 from almerindo/dependabot/npm_and_yarn/ansi-regex-4.1.1 (f677b1f)

3.0.5 (2022-09-10)

Bug Fixes

  • removing colorize from log format (12c1d9a)

3.0.4 (2022-09-10)

Bug Fixes

  • returning to json format (3d2180e)

3.0.3 (2022-09-10)

Bug Fixes

3.0.2 (2022-09-10)

Bug Fixes

3.0.1 (2022-09-10)

Bug Fixes

3.0.0 (2021-11-04)

Features

BREAKING CHANGES

  • this cocmmit is a braking change

2.0.0 (2021-11-04)

  • Merge pull request #17 from almerindo/main-backup (4b84cfd), closes #17

Features

BREAKING CHANGES

  • add barreel import
  • this commit is a breaking change. it changes the imports to barree.

1.8.0 (2021-11-04)

Bug Fixes

  • fix example levels to export barrel (269354c)

Features

  • changing export modules to barrel (d8095da)

1.7.0 (2021-10-10)

Features

  • added all modules from winston exported by traceability (6152971)

1.6.0 (2021-09-11)

Bug Fixes

  • logger: fix a method name in class LoggerTraceability. (0e0f5ad)

Features

  • added configuration logger wiston (887601d)

1.5.0 (2021-05-04)

Features

  • add export enum ETrackKey (1149dbf)

1.4.0 (2021-05-04)

Bug Fixes

  • upgrade jest and fixes eslint (bcef4d3)

Features

  • add request header configurable to accept the use of a standard like X-Correlation-ID or X-Request-ID (0c950c4), closes #2

1.3.1 (2021-05-03)

Bug Fixes

  • fixes the visibility of constructor from public to private (32ef2ef), closes #1

1.3.0 (2021-04-14)

Features

  • change asyncLocalStorage to public (a754473)

1.2.0 (2021-04-12)

Features

  • add example and middleware to NestJs (bc0a5ee)

1.1.9 (2021-04-10)

Bug Fixes

  • fixes express middleware and improves the readme.md (eb9ec4c)

1.1.8 (2021-04-10)

Bug Fixes

  • some erros on ci and package npm (e24795a)

1.1.7 (2021-04-10)

Bug Fixes

  • some erros on ci and package npm (48f17d0)

1.1.6 (2021-04-10)

Bug Fixes

  • some erros on ci and package npm (0d27289)
  • some erros on ci and package npm (2dd6122)

1.1.5 (2021-04-10)

Bug Fixes

  • some erros on ci and package npm (73ff367)

1.1.4 (2021-04-10)

Bug Fixes

  • some erros on ci and package npm (6e33991)

1.1.3 (2021-04-10)

Bug Fixes

  • some erros on ci and package npm (d4716ba)

1.1.2 (2021-04-10)

Bug Fixes

  • exports and refactor (415847d)
  • some erros on ci and package npm (f763168)

1.1.1 (2021-04-10)

Bug Fixes

1.1.0 (2021-04-10)

Bug Fixes

  • fixes droneCI an removes some missing imports (2af252c)
  • fixes tests and imports (cd735f9)

Features

  • add level and silent (3271db7)
  • creates context with asyncHooks localStore and creates tests (5df0f3b)
  • refactor (6492690)

1.0.0 (2021-04-09)

Features