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

express-tracking

v1.0.1

Published

Express middleware to track request/response with a correlator

Downloads

62

Readme

express-tracking

Express middleware to track the request and response storing in the domain the operation, transactionId and correlator.

npm version Build Status Coverage Status

These values can be used by a logging system, e.g. logops, to track all the log entries corresponding to a transaction (a request/response) by matching the transactionId or to track a flow (from one to multiple transactions) by matching the correlator.

The middleware follows this process:

  • In the request:

    • Get or initialize the tracking object in the domain. This object stores all the variables for tracking the request and response.
    • Set op (operation) in the tracking object. Its value is obtained from the middleware options, if available. Otherwise, it is set to null.
    • Set trans (transactionId) in the tracking object. Its value is a generated UUID that makes unique the request/response.
    • Set corr (correlator) in the tracking object. The middleware tries to get the correlator from the request using a correlator handler (based on HTTP header or JWT header depending on middleware options). If it is not possible to get a valid correlator, it reuses the transactionId as correlator.
  • In the response:

    • It uses the correlator handler to set the correlator in the response. The correlator handler based on HTTP headers adds a HTTP header (Unica-Correlator by default) with the correlator.

Installation

npm install express-tracking

Basic usage

var express = require('express'),
    expressLogging = require('express-tracking');

var app = express();
app.use(expressTracking());

app.listen(3000);

Options

The express middleware may receive an object with optional settings:

| Option | Type | Default | Description | |--------|------|---------|-------------| | op | String | null | Name of the operation (for logging purposes) | | corrHeader | String | Unica-Correlator | Name of the HTTP header with the correlator | | isJwt | Boolean | false | If the handler to get the correlator from the request and set the correlator in the response is based on JWT (obtained from the corr field in the JWT header) or on a HTTP header. Note that this option is ignored if corrHandler option is set. | | corrHandler | Function(corrHeader) | null | Function to get the correlator from the request and set the correlator in the response when the default implementations (header and JWT-based) are not enough. |

Advanced usage

Enabling JWT-based correlator handler

The default correlator handler is based on the HTTP header: Unica-Correlator (unless modified with the option corrHeader). To use the other correlator header, based on the corr field of the JWT header:

app.use(expressTracking({isJwt: true}));

Using a custom correlator handler

The following sample implements a custom handler to get the correlator from the query parameter corr instead of an HTTP header.

var customCorrHandler = function(header) {
  return {
    getCorrelator: function(req, cb) {
      return cb(null, req.query.corr);
    },
    setCorrelator: function(res, correlator) {
      res.set(header, correlator);
    }
  };
}
app.use(expressTracking({corrHandler: customCorrHandler}));

Full example

It is recommended to use this express middleware in combination with:

var express = require('express'),
    expressDomain = require('express-domaining'),
    expressTracking = require('express-tracking'),
    expressLogging = require('express-logging'),
    logger = require('logops');

logger.getContext = function() {
  return process.domain && process.domain.tracking;
};

var app = express();
app.use(expressDomain(logger));
app.use(expressTracking({op: 'test'}));
app.use(expressLogging(logger));

app.get('/test', function(req, res) {
  res.status(200).send();
});

app.listen(3000);

After launching the previous server, each HTTP request generates 2 log entries to trace the request and response:

time=2015-06-25T14:34:55.847Z | lvl=INFO | corr=0ad79e44-95e9-48fe-aa17-19773ebe9056 | trans=0ad79e44-95e9-48fe-aa17-19773ebe9056 | op=test | msg=Request from ::1: GET /test
time=2015-06-25T14:34:55.848Z | lvl=INFO | corr=0ad79e44-95e9-48fe-aa17-19773ebe9056 | trans=0ad79e44-95e9-48fe-aa17-19773ebe9056 | op=test | msg=Response with status 200 in 1 ms.

Changelog:

v1.0.1 (11-02-2016)

  • Update Jwt Utils dependency

v1.0.0 (02-07-2015)

  • First version

License

Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.