Negligible Psychological Misery

    traceability

    1.6.0 • Public • Published

    TypeScript version Node.js version APLv2 Build Status

    Install

    NOTE This lib needs to run on version >= 14.0.0 of Nodejs

    Using Yarn

    yarn install traceability
    

    Using npm

    npm install traceability
    

    Example Code

    Logging the trackId in all methods

    File: index.js

    import traceability from 'traceability'
    
    
    const levelRoot = () =>{
      traceability.ContextAsyncHooks.trackKey = ETrackKey['X-Correlation-ID']
      const {trackId} = traceability.ContextAsyncHooks.getTrackId({})
      traceability.ContextAsyncHooks.setContext({trackId})
      traceability.Logger.info('levelRoot');
      level1()
    
    }
    const level1 =  () =>{
      traceability.Logger.info('level1');
      level2();
    }
    
    const level2 =  () =>{
      traceability.Logger.info('level2');
    }
    
    levelRoot();

    Output

    {"message":"levelRoot","level":"info","X-Correlation-ID":"6b1552d0-0db5-4d7d-8551-567847703fa6","timestamp":"2021-05-03T16:21:01.523Z"}
    {"message":"level1","level":"info","X-Correlation-ID":"6b1552d0-0db5-4d7d-8551-567847703fa6","timestamp":"2021-05-03T16:21:01.525Z"}
    {"message":"level2","level":"info","X-Correlation-ID":"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

    import traceability from 'traceability';
    
    const { ContextAsyncHooks, Logger, LoggerTraceability } = 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)

    import express from 'express';
    import traceability from 'traceability';
    
    const { ContextAsyncHooks, Logger } = traceability;
    
    const app = express();
    const port = 3000;
    
    ContextAsyncHooks.trackKey= ETrackKey['X-Correlation-ID']
    
    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}`);
    });

    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
    X-Correlation-ID: 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","X-Correlation-ID":"6ecf583a-7509-4ce3-baef-1fcbe94adc5c","timestamp":"2021-05-03T16:57:32.116Z"}
    

    NOTE: At the momennt, we can compare the X-Correlation-ID 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)

    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();

    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.

    Install

    npm i traceability

    DownloadsWeekly Downloads

    59

    Version

    1.6.0

    License

    MIT

    Unpacked Size

    30 kB

    Total Files

    28

    Last publish

    Collaborators

    • almerindo.rehem