https://github.com/strongloop/strong-express-metrics
An Express middleware for collecting HTTP statistics.
https://github.com/strongloop/strong-express-metrics
Last synced: 9 months ago
JSON representation
An Express middleware for collecting HTTP statistics.
- Host: GitHub
- URL: https://github.com/strongloop/strong-express-metrics
- Owner: strongloop
- License: other
- Created: 2015-01-29T09:44:47.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2016-07-14T00:26:57.000Z (over 9 years ago)
- Last Synced: 2024-11-09T17:02:59.832Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 42 KB
- Stars: 15
- Watchers: 21
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# strong-express-metrics
An Express middleware for collecting HTTP statistics.
## Installation
```
$ npm install strong-express-metrics
```
## Usage
```js
var express = require('express');
var metrics = require('strong-express-metrics');
var app = express();
app.use(metrics());
app.listen(3000);
```
You can extend the metrics reported by the middleware by providing
a builder function. The output of this builder function will be merged
with the default record produced by the middleware.
```js
app.use(metrics(function buildRecord(req, res) {
return {
client: {
id: req.authInfo.app.id,
username: req.authInfo.user.email
},
data: {
// put your custom metrics here
}
};
}));
```
If your application is not running inside StrongLoop's Supervisor,
you can provide a custom function to process and report the statistics.
```js
metrics.onRecord(function(data) {
// simple statsd output
console.log('url:%s|1|c', data.request.url);
console.log('status:%s|1|c', data.response.status);
console.log('response-time|%s|ms', data.duration);
});
```
## Record format
The middleware produces records in the following format.
```js
{
version: require('./package.json').version,
timestamp: Date.now(),
client: {
address: req.socket.address().address,
id: undefined, // builder should override
username: undefined // builder should override
},
request: {
method: req.method,
url: req.url
},
response: {
status: res.statusCode,
duration: res.durationInMs,
bytes: undefined // TODO
},
process: {
pid: process.pid,
workerId: cluster.worker && cluster.workerId
},
data: {
// placeholder for user-provided data
},
// extra info filled for LoopBack applications only
loopback: {
modelName: 'User',
remoteMethod: 'prototype.updateAttributes',
// instanceId is undefined for static methods
// e.g. User.find() or User.login()
instanceId: 1234
}
}
```