GithubHelp home page GithubHelp logo

phil-r / stats Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 1.0 759 KB

๐Ÿ“Š Request statistics middleware that stores response times, status code counts, etc

License: MIT License

JavaScript 99.20% Shell 0.80%
nodejs node middleware statistics benchmarking express express-middleware stats koa rayo

stats's Introduction

stats

Request statistics middleware

NPM version build status code coverage code style: prettier

Installation

npm i @phil-r/stats

API

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:

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

Example

Examples for popular node web frameworks can be found here

Here is the example of usage in express app:

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:

{
  "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

This is a fork of zenmate/stats

Inspired by

expressjs/response-time and thoas/stats

stats's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar phil-r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

xyztlp

stats's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.