Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phil-r/stats
📊 Request statistics middleware that stores response times, status code counts, etc
https://github.com/phil-r/stats
benchmarking counter express express-middleware fastify koa middleware node nodejs polka rayo requests statistics stats
Last synced: about 2 months ago
JSON representation
📊 Request statistics middleware that stores response times, status code counts, etc
- Host: GitHub
- URL: https://github.com/phil-r/stats
- Owner: phil-r
- License: mit
- Created: 2019-05-15T18:28:59.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T03:44:33.000Z (almost 2 years ago)
- Last Synced: 2024-11-22T21:56:44.019Z (about 2 months ago)
- Topics: benchmarking, counter, express, express-middleware, fastify, koa, middleware, node, nodejs, polka, rayo, requests, statistics, stats
- Language: JavaScript
- Homepage:
- Size: 741 KB
- Stars: 22
- Watchers: 3
- Forks: 1
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-express - stats - Request statistics middleware that stores response times, status code counts, etc (Middleware)
README
# stats
> Request statistics middleware
[![NPM version](http://img.shields.io/npm/v/@phil-r/stats.svg?style=flat-square)](https://www.npmjs.com/package/@phil-r/stats)
[![build status](https://github.com/phil-r/stats/workflows/Testing/badge.svg)](https://github.com/phil-r/stats/actions)
[![code coverage](https://img.shields.io/codecov/c/github/phil-r/stats.svg?style=flat-square)](https://codecov.io/gh/phil-r/stats)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)## Installation
```bash
npm i @phil-r/stats
```## API
```js
const initStats = require('@phil-r/stats');
const { statsMiddleware, getStats } = initStats({
endpointStats: true,
complexEndpoints: ['/user/:id'],
customStats: true,
addHeader: true,
});
```### initStats([options])
Returns `statsMiddleware` middleware function and `getStats` function,
that returns current stats#### Options
`initStats` accepts optional `options` object that may contain any of the following keys:
##### endpointStats
Defaults to `false`
Boolean that indicates whether to track per endpoint stats.
##### complexEndpoints
Defaults to `[]`
Used in conjunction with `endpointStats`
Use it in case your application has routes with params or wildcard routes
**Recommended** for applications that have endpoints like `/user/123`
##### customStats
Defaults to `false`
Adds `startMeasurement` and `finishMeasurement` functions to the request objects
and allows measuring any parts of the app.**Usage:**
```js
function handler(req, res) {
const measurement = req.startMeasurement('measurementName');
// Some code...
req.finishMeasurement(measurement);
}
```##### addHeader
Defaults to `false`
Adds `X-Response-Time` header to all responses, can be used to replace
[`expressjs/response-time`](https://github.com/expressjs/response-time)## Example
Examples for popular node web frameworks can be found [here](./examples)
Here is the example of usage in express app:
```js
const app = require('express')();
const initStats = require('@phil-r/stats');const { statsMiddleware, getStats } = initStats({
endpointStats: true,
complexEndpoints: ['/user/:id'],
customStats: true,
addHeader: true,
});app.use(statsMiddleware);
app.get('/', (req, res) => res.end('Hello'));
app.get('/user/:id', (req, res) => res.end(`Hello ${req.params.id}`));
app.get('/long', async (req, res) => {
const measurement = req.startMeasurement('long');
await new Promise((resolve) => {
setTimeout(() => resolve(), 2000);
});
req.finishMeasurement(measurement);
res.end(`Long job finished`);
});
app.get('/stats', (req, res) => res.send(getStats()));app.listen(8080);
console.log('Server listens at http://localhost:8080');
```Visiting http://localhost:8080/stats will give following result:
```json
{
"uptime": 63881,
"uptimeHumanReadable": "1m 4s",
"statusCodes": {
"200": 5,
"404": 4
},
"uuid": "330d9cc6-7d40-4964-888c-4d2817905ee1",
"pid": 90603,
"totalTime": 4020.3912830000004,
"averageTime": 446.7101425555556,
"count": 9,
"endpointStats": {
"GET /long": {
"totalTime": 4009.410922,
"averageTime": 2004.705461,
"count": 2,
"statusCodes": {
"200": 2
}
},
"GET /favicon.ico": {
"totalTime": 4.286955,
"averageTime": 1.07173875,
"count": 4,
"statusCodes": {
"404": 4
}
},
"GET /stats": {
"totalTime": 6.227342999999999,
"averageTime": 6.227342999999999,
"count": 1,
"statusCodes": {
"200": 1
}
},
"GET /user/:id": {
"totalTime": 0.466063,
"averageTime": 0.2330315,
"count": 2,
"statusCodes": {
"200": 2
}
}
},
"customStats": {
"long": {
"totalTime": 4005.556455,
"averageTime": 2002.7782275,
"started": 2,
"count": 2
}
}
}
```All time related results are in milliseconds
# [License](LICENSE)
This is a fork of [zenmate/stats](https://github.com/zenmate/stats)
# Inspired by
[`expressjs/response-time`](https://github.com/expressjs/response-time) and [`thoas/stats`](https://github.com/thoas/stats)