express-metrics
Express middleware for collecting and reporting metrics about response times.
Installation
On project path:
npm install express-metrics --save
Example
Express projects:
var express = ;var expressMetrics = ;var app = ; // start a metrics serverapp
In :8091/metrics:
global: all: type: "timer" duration: type: "histogram" min: 0 max: 109713 sum: 674927 variance: 2398825911142156 mean: 5624391666666665 std_dev: 15488143565780103 count: 1 median: 08055000000000001 p75: 1738 p95: 3157105 p99: 1071568799999999 p999: 109713 rate: type: "meter" count: 1 m1: 22284012252758894 m5: 4550172188270242 m15: 5220474962604762 mean: 13997597079168076 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;
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
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 = ;var express = ;var expressMetrics = ;var numCPUs = length; if clusterisMaster // Fork workers. for var i = 0; i < numCPUs; i++ cluster; // start a metrics server on master process expressMetrics; else var app = ; // with cluster option set to true says to the express-metrics that // it must send the measured times to master process app
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
todevelop
branch. - To fix bugs in release version,
pull request
bothmaster
anddevelop
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)