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 🙏

© 2026 – Pkg Stats / Ryan Hefner

metrex

v0.5.0

Published

Express middleware that instruments your API and provides a configurable metrics dashboard.

Readme

Metrex

Express middleware that instruments your API and exposes a route with a lightweight metrics dashboard (RPS, latencies, status codes, and top routes). No runtime dependencies, only express as a peer dependency.

Installation

npm install metrex

Quick Start

const express = require('express');
const { useMetrex } = require('metrex');

const app = express();

// Globally instrument and mount the dashboard at /metrex
// Returns helper methods for custom metrics
const metrex = useMetrex(app, { routePath: '/metrex' });

app.get('/hello', (req, res) => res.json({ ok: true }));

app.post('/buy', (req, res) => {
  // Business logic...
  // Example: Increment a custom counter
  metrex.counter('items_sold', 1, 'Total items sold');

  // Example: Set a gauge value
  metrex.gauge('queue_size', 5, 'Items currently in queue');

  res.json({ ok: true });
});

app.listen(3000, () => {
  console.log('Metrex at http://localhost:3000/metrex');
});

Open http://localhost:3000/metrex to view the dashboard.

Endpoints

  • GET {routePath}: HTML dashboard.
  • GET {routePath}/data: JSON with aggregated metrics.
  • GET {routePath}/metrics: Prometheus text format exporter.

Custom Metrics

Metrex allows you to track your own application metrics alongside standard HTTP metrics.

const metrex = useMetrex(app);

// Counter: Value that only goes up (e.g. total errors, tasks completed)
metrex.counter('my_counter_name', 1, 'Description of metric');

// Gauge: Value that can go up and down (e.g. queue size, cache size)
metrex.gauge('my_gauge_name', 120, 'Description of metric');

These metrics will appear automatically in the Dashboard and the Prometheus exporter.

Options

  • routePath (string): base path for the dashboard. Default /metrex.
  • historySize (number): number of events to retain in memory (default 2000).
  • shouldTrack(req) (function): filter function; return false to not count a request.
  • excludePaths (string[] | RegExp[]): paths to exclude from instrumentation.
  • slowThreshold (number): threshold in ms to mark slow requests (default 1000ms).
  • cpuThreshold (number): percentage (0-100) to trigger CPU alerts (default 90).
  • onAlert (function): callback for alerts.
  • auth (object): Basic auth credentials for the dashboard.
    • username (string)
    • password (string)

Example with Auth

useMetrex(app, {
  routePath: '/metrex',
  auth: {
    username: 'admin',
    password: 'supersecretpassword',
  },
});

Notifications & Alerts

Metrex can trigger a callback when it detects issues, allowing you to integrate with Telegram, Slack, etc.

useMetrex(app, {
  slowThreshold: 2000, // Alert if request takes > 2s
  cpuThreshold: 80, // Alert if CPU > 80%
  onAlert: (event) => {
    // event = { type: 'error' | 'latency' | 'cpu', value: number, msg: string, timestamp: number }
    console.warn('ALERT:', event.msg);

    // Example: Send to Telegram
    // fetch(`https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=<ID>&text=${encodeURIComponent(event.msg)}`);
  },
});

Triggers:

  • Error: HTTP 5xx responses.
  • Latency: Requests taking longer than slowThreshold.
  • CPU: Usage exceeds cpuThreshold (debounced to once per minute).

Recommendations

  • Mount it before your business routes: app.use(metrex()) at the beginning to capture all requests.
  • If you have sensitive paths, exclude them with excludePaths.
  • FGET {routePath}/data`: JSON with aggregated metrics.

Local Example

npm run example
# open http://localhost:3000/metrex

Contributing

See CONTRIBUTING.md for guidelines.

Security

See SECURITY.md for security policy.

License

ISC