npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@leisurelink/domain-correlation

v1.1.4

Published

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

Downloads

12

Readme

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