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

expressjs-metrics

v0.0.16

Published

Express middleware for collecting and reporting metrics about response times

Downloads

8

Readme

expressjs-metrics

Express middleware for collecting and reporting metrics about response times. base in express-metrics

Installation

On project path:

npm install expressjs-metrics --save

Example

Express projects:

var express = require('express');
var expressMetrics = require('expressjs-metrics');
var app = express();

// start a metrics server
app.use(expressMetrics({
  port: 8091
});

// every time this handler returns the greet, express-metrics
// will update the metrics with the calculated response time
app.get('/', function (req, res, next) {
  res.json({ greet: 'Hello world!' });
});

In :8091/metrics:

{
  global: {
    all: {
      type: "timer",
      duration: {
        type: "histogram",
        min: 0,
        max: 109.713,
        sum: 674.927,
        variance: 239.8825911142156,
        mean: 5.624391666666665,
        std_dev: 15.488143565780103,
        count: 1,
        median: 0.8055000000000001,
        p75: 1.738,
        p95: 31.57105,
        p99: 107.1568799999999,
        p999: 109.713
      },
      rate: {
        type: "meter",
        count: 1,
        m1: 2.2284012252758894,
        m5: 4.550172188270242,
        m15: 5.220474962604762,
        mean: 1.3997597079168076,
        unit: "seconds"
      }
    },
    static: {
      type: "timer",
      duration: {
        ...
      },
      rate: {
        ...
      }
    }
  },
  status: {
    200: {
      ...
    },
    302: {
      ...
    },
  },
  method: {
    get: {
      ...
    },
    post: {
      ...
    },
    ...
  },
  '/blog': {
    get: {
      ...
    }
  },
  '/blog/:slug': {
    post: {
      ...
    }
  }
}

Metrics are grouped by:

  • global, all and statics (i.e. global: { all: {...}, static: {...} })
  • code status (i.e. status: { 200: {...} })
  • method (i.e. method: { get: {...} })
  • path and method (i.e. '/blog': { get: {...} })

Options

Example using all options with its default values:

app.use(expressMetrics({
  port: 8091,
  cluster: false,
  decimals: false,
  header: false
}));

port: Number (default: undefined)

Only used when cluster option is false, start a metrics servers on the same process that the application is running.

decimals: Boolean (default: false)

If decimals is true, times are measured in millisecond with three decimals. Otherwise, times are rounded to milliseconds.

header: Boolean (default: false)

If header is true, "X-Response-Time" is added as HTTP header in the response.

statsd: Object (default: undefined)

Optionally you can send the metrics to statsd. In order to do that you just need to provide the statsd config in the options. Thanks to metrics you are able to explore at any time if there is something wierd in your application. And with statsd you are able to collect stats for you more representative resources.

Example:



  app.use(expressMetrics({
    statsd: {
      'host': 'localhost',
      'port': 8125,
      'prefix': require('os').hostname() + '.myService'
      'routes': {
        'showUserCampaigns': [{ path: '/campaigns/:userId/lite', methods: ['get']}],
        'showCampaign':  [{ path: '/campaign/:campaignId', methods: ['get']}],
        'showUserShops': { path: '/shop/:userId', method: 'get'}
      }
    }
  });

use in strongloop

Example:

app.all("*",expressMetrics({
port: 8091,
// cluster: true,
decimals: false,
header: true,
// statsd: {
//     'host': 'localhost',
//     'port': 8125,
//     'prefix': require('os').hostname() + '.myService',
//     'routes': {
//         'showUserCampaigns': [{ path: '/campaigns/:userId/lite', methods: ['get']}],
//         'showCampaign':  [{ path: '/campaign/:campaignId', methods: ['get']}],
//         'showUserShops': { path: '/shop/:userId', method: 'get'}
//     }
}))

Just the routes that you indicate in the 'routes' option will be sent to statsd.

cluster: Boolean (default: false)

If cluster is true, delegate the start of the metrics server to master process. Due to this, express-metrics provides one way to run a metrics server in master, i.e:

var cluster = require('cluster');
var express = require('express');
var expressMetrics = require('expressjs-metrics');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  // start a metrics server on master process
  expressMetrics.listen(8091);
} else {
  var app = express();

  // with cluster option set to true says to the express-metrics that
  // it must send the measured times to master process
  app.use(expressMetrics({
    cluster: true
  });

  app.get('/', function (req, res, next) {
    res.json({ greet: 'Hello world!' });
  });

  app.listen(8090);
}

When one request is handled by one worker, express-metrics measures the response time and send it to the master. Then, master receives the data and updates the corresponding metrics. Furthermore, master exposes the metrics on port previously configured.

Logging

Logs are sent to 'express-metrics' log4js logger.

Contributions

Do you want to contribute?. Please, follow the below suggestions:

  • To add features, pull requests to develop branch.
  • To fix bugs in release version, pull request both master and develop branches.
  • Be consistent with style and design decisions.
  • Cover your implementation with tests, add it under test/*.spec.js.

Change history

To view change history, please visit: HISTORY.md

Versioning strategy:

  • The major version will increase for any backward-incompatible changes.
  • The minor version will increase for added features.
  • The patch version will increase for bug-fixes.

License

To view the MIT license, please visit: The MIT License (MIT)