@leisurelink/domain-correlation

    1.1.4 • Public • Published

    Domain Correlation

    • creates/sets a correlation id against a domain-context.

    you can manually annotate the correlation where you need, but this library provides overload stratgies for:

    • log streams
    • modifying http responses
    • globally modifying http request headers (if you dare)

    a domain-context must exist for correlation to function, for example:

    var domainCorrelation = require('domain-correlation');
    var express = require('express');
     
    var app = express();
    var domainContext = domainCorrelation.domainContext;
     
    app.use(domainContext.middleware);
    app.use(function(req, res, next) {
        console.log('correlation id for this request', domainCorrelation.getId());
        next();
    });

    Correlating Logs

    if you have a structured logger, you can use the method above in your log formatter.

    if you use console.log simply to stdout/err, we have a convenience method to overload/decorate the stream.write method:

    domainCorrelation.decorateStream(process.stdout);
    domainCorrelation.decorateStream(process.stderr);
    // or simply
    domainCorrelation.decorateStream.patchStdOutErr();

    this will cause every line output to be prepended as such:

    cid:112316050941ILOQ3mg GET /v1/ping 200 106 - 15.427 ms
    

    you can customize the formatting by overloading methods provided (see source)

    Changing or Relating Correlation ID's

    the key to correlation id's is that they can possibly span multiple microservices and/or 3rd party service calls and still provide a unified "transaction log" even though they are all decoupled.

    for internal microservices, you can share the correlation id between services on each request, say setting the 'x-correlation-id' http header. however you may use the same header to allow callers of your api to convey their own correlation id. at this point the difference is the source or trust. it may be later in your middleware chain that you can verify the identify of the caller and choose to simply trust+set the correlation id, or relate the 3rd party correlation id to the one already established. in the trust scenario, it's possible that things have been logged against a generated correlation id before you were able to trust/set... in each case you want to log these associations.

    for example:

    // this adds the correlation id to every line printed to stdout/stderr
    domainCorrelation.decorateStream.patchStdOutErr();
     
    // eventing decouples logging
    domainCorrelation.onSetId(function(newId, oldId) {
        if (!oldId) { return; }
        console.log('oldCid:'+oldId);
        // this would log:
        // cid:b oldCid:a
    });
    domainCorrelation.onRelatedId(function(id) {
        console.log('relatedCid:'+id);
        // this would log:
        // cid:a relatedCid:b
    });
     
    // this should always be your first middleware
    app.use(domainContext.middleware);
     
    // this will auto-set the response correlation header
    // when res.writeHead is called
    app.use(domainCorrelation.http.middleware);
     
    app.use(function(req, res, next) {
        console.log('something before we validate our caller');
        // this would log:
        // cid:a something before we...
        next();
    });
     
    app.use(function validateUser(req, res, next) {
        if (!userValid(req)) {
            next(new UserInvalid());
        }
        var headerCorrelationId = req.headers['x-correlation-id'];
        if (isTrustedCaller(req)) {
            // if we trust the correlation id is from another
            // internal service, set/replace it. (see onSetId above)
            domainCorrelation.setId(headerCorrelationId);
        } else {
            // otherwise log it as a related id (see onRelatedId above)
            domainCorrelation.relatedId(headerCorrelationId);
        }
    });
     
    // or throughout your chain, you can log any third party id's
    app.use(function performSome3rdPartyCall(req, res, next) {
        make3rdPartyHttpRequest(function(httpResponse) {
            var thirdPartyCorrelationId = httpResponse.headers['x-transaction-id'];
            domainCorrelation.relatedId(thirdPartyCorrelationId);
        });
    });

    Keywords

    none

    Install

    npm i @leisurelink/domain-correlation

    DownloadsWeekly Downloads

    7

    Version

    1.1.4

    License

    ISC

    Last publish

    Collaborators

    • uniqname
    • blake1321
    • jouellette
    • lhirschi
    • flitbit
    • jtowner
    • twindagger
    • leisurelink-robot