moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.11k stars 580 forks source link

Inject custom tags dynamically in Datadog logger. #821

Closed edud69 closed 1 year ago

edud69 commented 3 years ago

Is your feature request related to a problem? Please describe. Moleculer offers a way to have static tags, but for a context-bound tag nothing is possible. Currently, to track user logs with Datadog, we inject the request context (current connected user email, request ID) in the arguments of logged lines behind a wrapper that we have created:

export class LoggerWrapper implements Logger {

    constructor(context : Context<any>, user : User) {
        this._user = user;
        this._internalLogger = context.service.logger;
    }

    debug(...args: any[]): void {
        this._internalLogger.debug(...this._appendContext(args));
    }

    private _appendContext(args: any[]) {
        if(this._user?.email) {
            return args.concat(["\n\nContext:", { user: this._user.email, domain: this._user.domain, requestID: this._requestID }]);
        }
        return args;
    }
}

Describe the solution you'd like It would be nice if the Moleculer config exposes some kind of callback based on the context.

{
    type: "Datadog",
    options: {
        level: "info",
        ...
        customTags: (context? : Context<any>) => {
            return { "A_TAG_KEY_01" : context?.meta?.someDynamicValue }
        }
    }

Something really close to what was done in: PR : https://github.com/moleculerjs/moleculer/pull/789 Issue: https://github.com/moleculerjs/moleculer/issues/784

Describe alternatives you've considered Logging the context directly in the message is a workaround, but having it as a tag would be much easier and remove the noise of some messages that are not related.

icebob commented 1 year ago

I'm closing this issue because we will remove the built-in trace/metrics features in the future versions.