Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

http-metrics-middleware

Package Overview
Dependencies
Maintainers
0
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-metrics-middleware

Express middleware for adding common prometheus metrics

  • 2.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Build Status Test Coverage npm downloads

http-metrics-middleware

Express middleware with useful prometheus metrics.

This wraps prom-client, and adds some default metrics.

Note: As of v1.2.0, this module requires Node.js v10 or above.

Contributing

Contributions are welcome and encouraged! Please follow the instructions in CONTRIBUTING.md.

Usage

Simplest usage is:

const MetricsMiddleware = require('http-metrics-middleware')
const express = require('express')

var metrics = new MetricsMiddleware()
app.use(metrics.initRoutes())

With koa using koa-connect:

const MetricsMiddleware = require('http-metrics-middleware')
const c2k = require('koa-connect')

var metrics = new MetricsMiddleware()
app.use(c2k(metrics.initRoutes()))

Options

The middleware can be configured by providing an options object to the constructor.

optiondefaultinfo
metricsPath/metricsthe metrics exposed path
timeBuckets[ 0.01, 0.1, 0.5, 1, 5 ]the buckets to assign to duration histogram (in seconds)
quantileBuckets[ 0.1, 0.5, 0.95, 0.99 ]the quantiles to assign to duration summary (0.0 - 1.0)
quantileMaxAge600configures sliding time window for summary (in seconds)
quantileAgeBuckets5configures number of sliding time window buckets for summary
includeErrorfalsewhether or not to include presence of an unhandled error as a label
includePathtruewhether or not to include normalized URL path as a metric label - see about includePath below
normalizePatha function(req) - generates path values from the express req object
paramIgnores[]array of path parameters not to replace. Use with caution as this may cause high label cardinality.
formatStatusCode(res) => res.status_code || res.statusCodea function(res) - generates path values from the express res object
enableDurationHistogramtruewhether to enable the request duration histogram
enableDurationSummarytruewhether to enable the request duration summary
durationHistogramNamehttp_request_duration_secondsthe name of the duration histogram metric - must be unique
durationSummaryNamehttp_request_duration_quantile_secondsthe name of duration summary metric - must be unique
about includePath

While it can be useful to know which endpoints are being exercised, including the path label can cause an explosion in tracked metrics from your service when the malicious or poorly-configured clients send strange URLs.

For this reason, it is recommended that you set includePath to false, unless your route parameters are restricted to include only desired values.

Paths are never included on requests which were not handled by a route with an explicit path (i.e. app.use where the first argument is a callback).

For example:

// here, the path label will be tracked if `includePath` is enabled
// BUT don't do this - restrict the param with a regex like the next example
app.get('/api/v1/:resource/*', (req, res) => {
  res.send('foo')
})

// this is better, as the resource param only matches a certain pattern
app.get('/api/v1/:resource([a-z]+)/*', (req, res) => {
  res.send('foo')
})

// here, the path label will never be tracked 
app.use((req, res) => {
  res.send('foo')
})

Defining custom metrics

The underlying prom-client module is available for specifying your own custom metrics:

const promClient = require('http-metrics-middleware').promClient

var myHistogram = new promClient.Histogram({
  name: 'foo_duration_seconds',
  help: 'track the duration of foo',
  labelNames: [ 'bar', 'baz' ],
  buckets: [1, 2, 3, 4, 5]
})

Metrics

In additional to the default metrics provided by prom-client, this module adds:

  • http_request_duration_seconds - (optional, enabled by default) http latency histogram labeled with status_code, method, path, and error (disabled by default - enable with includeError option)
    • use the enableDurationHistogram boolean property to control whether or not this is enabled
    • use the durationHistogramName property to give this metric a different name (required if you want both the histogram and summary)
  • http_request_duration_seconds - (optional, disabled by default) http latency summary labeled with status_code, method, path, and error (disabled by default - enable with includeError option)
    • use the enableDurationSummary boolean property to control whether or not this is enabled
    • use the durationSummaryName property to give this metric a different name (required if you want both the histogram and summary)
  • *_build_info - build information about the service (initialized with initBuildInfo function)
    const MetricsMiddleware = require('http-metrics-middleware')
    var metrics = new MetricsMiddleware()
    
    var ns = 'myservice'
    var version = '1.2.3'
    var revision = 'abcd1234'
    var buildTime = '2017-07-07T07:07:07.007Z'
    metrics.initBuildInfo(ns, version, revision, buildTime)
    

Sample output

http_request_duration_seconds_bucket{le="0.05",status_code="200",path="/",method="GET"} 5
http_request_duration_seconds_bucket{le="0.1",status_code="200",path="/",method="GET"} 7
http_request_duration_seconds_bucket{le="0.5",status_code="200",path="/",method="GET"} 10
http_request_duration_seconds_bucket{le="1",status_code="200",path="/",method="GET"} 13
http_request_duration_seconds_bucket{le="+Inf",status_code="200",path="/",method="GET"} 15
http_request_duration_seconds_count{status_code="200",path="/",method="GET"} 15
http_request_duration_seconds_sum{status_code="200",path="/",method="GET"} 18.534

Keywords

FAQs

Package last updated on 17 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc