moleculer-correlation

0.0.1 • Public • Published

Moleculer logo

Build Status Coverage Status Known Vulnerabilities

moleculer-correlation NPM version

Log correlation for Moleculer via AsyncLocalStorage

Features

Adds the current context's requestID to any log messages made while the context is available. Takes advantage of Node 14+ AsyncStorage.

Install

npm install moleculer-correlation --save

Usage

const { ServiceBroker } = require("moleculer");
const { CorrelationMiddleware } = require("moleculer-correlation");

const broker = new ServiceBroker({
    middlewares: [CorrelationMiddleware]
});

// --- SERVICE ---
broker.createService({
    name: "greeter",
    actions: {
        hello: {
            handler(ctx) {
                this.logger.info("Creating a Greeting")
                return `Hello ${ctx.params.name}`;
            }
        }
    }
});

// --- TEST ---
broker.start()
    .then(() => broker.call("greeter.hello").then(res => broker.logger.info(res)))

// --- CONTEXT REQUEST ID IN LOGS WHEN AVAILABLE ---
// [2022-06-09T03:18:02.291Z] INFO  node/GREETER: [2e524af4-69a1-440f-9a1d-1904a6b28258]  Creating a Greeting

Test

$ npm test

In development with watching

$ npm run ci

Contribution

Please send pull requests improving the usage and fixing bugs, improving documentation and providing better examples, or providing some testing, because these things are important.

License

The project is available under the MIT license.

Contact

Copyright (c) 2022 Adam McCormick

@MoleculerJS @MoleculerJS

Dependencies (0)

    Dev Dependencies (15)

    Package Sidebar

    Install

    npm i moleculer-correlation

    Weekly Downloads

    5

    Version

    0.0.1

    License

    MIT

    Unpacked Size

    10.7 kB

    Total Files

    13

    Last publish

    Collaborators

    • adam-mccormick