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

expresser-metrics

v3.3.1

Published

Metrics for Expresser apps.

Downloads

24

Readme

Expresser Metrics

Plugin to gather and output simple metrics on Expresser apps.

Adding and using metrics

To measure something:

// Tag can be a label, or any string to differentiate requests of same metric,
// for example when requesting users, you can use the tag to define which user.
var tag = "myUser"
var metrics = require("expresser-metrics")

// Start metric of a getUserOrders call.
var mt = metrics.start("getUserOrders", tag)

// Do something here and there, for example get user orders,
// and set result as data on the metric object.
var orders = getUserOrders(tag)
mt.setData("orders", orders)

// Some more code, async, etc... then finally:

mt.end(optionalError)
// You can also end the metric using:
// metrics.end(mt, optionalError)

Metrics cleanup

By default the module will keep all metrics data for the past 12 hours. Older data will be purged every 20 minutes automatically. Both values are customizable on the settings.

If you wish to cleanup manually, simply call:

var metrics = require("expresser-metrics")
metrics.cleanup()

Output

To generate a summary about collected metrics, use the built-in output method:

var metrics = require("expresser-metrics")
var output = metrics.output()

// Some more code...

res.render(output)

By default it will give you the specific metrics for the last 1min, 5min and 20min, having the 99, 95 and 90 percentiles. You can change these values on the settings.

You can also generate the output with your own custom options. For example to get metrics for last 1, 5, 60 and 300 minutes, and not showing the percentiles:

var options = {
    intervals: [1, 5, 60, 300],
    percentiles: []
}

var metrics = require("expresser-metrics")
var output = metrics.output(options)

And to get metrics for a specific call only:

function myCall() {
    var mt = metrics.start("my-call")

    // Some code, then end metrics somewhere...
}

var options = {
    keys: ["my-call"]
}

var metrics = require("expresser-metrics")
var output = metrics.output(options)

Metrics HTTP server

The Metrics module can spin up a dedicated HTTP server for the metrics output, which makes it easier for you to set firewall rules for external access.

To enable the HTTP server, simply add a valid port number to settings.metrics.httpServer.port and set settings.metrics.httpServer.autoStart to true.

If you want to control the Metrics HTTP server manually, please set the port programatically and use the start and kill methods. You can also access the underlying Express server, by using the metrics.httpServer.server object. For example:

// Some code, my app starting...

var expresser = require("expresser")
var metrics = require("expresser-metrics")

expresser.settings.metrics.httpServer.port = 8080
metrics.httpServer.start()

// Server started, add a custom route to the metrics http server
metrics.httpServer.server.get("/my-route", myRouteCallback)

// More custom stuff... now to kill:

metrics.httpServer.kill()

For detailed info on specific features, check the annotated source on /docs/source/plugin.metrics.index.html