npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@matteodisabatino/express-prometheus-middleware

v4.2.1

Published

Exposes Prometheus metrics for express applications

Downloads

1,675

Readme

express-prometheus-middleware

Exposes Prometheus metrics for express applications. Based on @trussle/tricorder.

By default, the module exposes information about Prometheus default metrics, garbage collection metrics and the duration and the throughput of each HTTP route that has been called at least once via endpoint /metrics. However, you can customize this behaviour via options.

Since version 1.0.0 the module supports all existing Node.js versions from 6.0.0. express and prom-client are required as peer dependencies.

Since version 3.0.0 the module has got its own official Grafana dashboard. Please note that to have information about usage of CPU and RAM, node_exporter must be used in conjunction.

The module is write in TypeScript following the Google TypeScript Style Guide. If you find something not compliant with, please provide a pull request.

In general every pull request that will:

  • Let the code be compliant to Google TypeScript Style Guide
  • Improve performances
  • Add features

are well accepted.

Available options

| Options | Type | Meaning | Default value | | - | - | - | - | | collectDefaultMetrics | boolean | Prometheus.DefaultMetricsCollectorConfiguration | Whether or not to collect Prometheus default metrics or configuration for prom-client | true | | collectGCMetrics | boolean | Whether or not to collect garbage collection metrics | true | | exclude | (req: express.Request): boolean | Avoid all matching routes to expose duration and throughput information | (req) => false | | excludePaths | string[] | Avoid all matching paths to expose duration and throughput information | [] | | url | string | The path to which expose metrics | /metrics |

Usage

Require the module, instance an object and use the handler as an express middleware.

Basic

const express = require('express')
const { ExpressPrometheusMiddleware } = require('@matteodisabatino/express-prometheus-middleware')

const app = express()
const epm = new ExpressPrometheusMiddleware()

app.use(epm.handler)

app.listen(process.env.PORT, () => {
  console.log('Server has been started')
})

Example of advanced usage

You can easily configure your Express Prometheus Middleware instance to ignore specific endpoints in your application.

const express = require('express')
const { ExpressPrometheusMiddleware } = require('@matteodisabatino/express-prometheus-middleware')

const app = express()
const epm = new ExpressPrometheusMiddleware({
  exclude: (req) => req.method === 'POST' && req.path === '/accounts'
  // This setting will prevent to generate the duration and the throughput
  // metrics for route POST /accounts
})

app.use(epm.handler)

app.listen(process.env.PORT, () => {
  console.log('Server has been started')
})

Example of usage with custom metrics reporting

It's likely that you will want to provide additional data to your Prometheus scraper and Express Prometheus Middleware does not get in your way with this:

const express = require('express')
const { ExpressPrometheusMiddleware } = require('@matteodisabatino/express-prometheus-middleware')
// npm install --save prom-client
const Prometheus = require('prom-client')

const app = express()

const gauge = new Prometheus.Gauge({
  name: `myamazingapp_interesting_datapoint`,
  help: `A very helpful but terse explanation of this metric`,
  collect () {
  // Add your own custom logic here
    this.inc();
  },
  // Or more likely
  async collect () {
    const data = SomeRepository.getSomeRecordCounts(...);
    this.set(data.total);
  }
})

app.use(epm.handler)

app.listen(process.env.PORT, () => {
  console.log('Server has been started')
})

Viewing /metrics will then display your data alongside the information provided by this library. See the Prometheus Client documentation for better examples.