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

prometheus-wrapper

v0.1.3

Published

Makes prometheus easier to use in a nodejs app

Downloads

50

Readme

NodeJS Prometheus Wrapper

Wrapper to official NodeJS Prometheus exporter (prom-client)

WHY

The official NodeJS client for prometheus requires to transporter the metrics variables in your code (counters, gauges, histograms and summaries).

This small library allow to control variables from the whole code, in a singleton, by getting the metric by name, just like that: require('prometheus-wrapper').get("<counter-name>").inc()

EXAMPLES

Execute this command to launch an express server exposing some metrics. Browse localhost:8080/metrics to monitor the metrics (data is changing after 10s).

$ node examples/index.js

See the source files in /examples to see suggestions of implementation.

HOW TO

init.js

var prometheus = require("prometheus-wrapper");

var express = require('express');
var app = express();

prometheus.setNamespace("myapp");

app.get('/metrics', function(req, res) {
  res.end(prometheus.getMetrics());
});

// Counter
prometheus.createCounter("mycounter", "A number we occasionally increment.");

// Gauge
prometheus.createGauge("mygauge", "A random number we occasionally set.");

// Histogram
prometheus.createHistogram("myhistogram", "A chat duration histogram.", {
	buckets: [ 10, 30, 60, 300, 600, 1800, 3600 ]
});

// Summary
prometheus.createSummary("mysummary", "Compute quantiles and median of a random list of numbers.", {
	percentiles: [ 0.01, 0.1, 0.5, 0.9, 0.99 ]
});

app.listen(8080);

wherever-in-your-code.js

var prometheus		= require("prometheus-wrapper");

setInterval(function () {
	prometheus.get("mycounter").inc(42);
}, 10000);

prometheus.get("mygauge").set(42);

var end = prometheus.get("myhistogram").startTimer();
setTimeout(function () {
	end();
}, 15000);

for (var i = 0; i < 100000; ++i) {
	prometheus.get("mysummary").observe(Math.random());
}

Exposed /metrics :

$ curl 127.0.0.1:8080

# HELP myapp_mycounter A number we occasionally increment.
# TYPE myapp_mycounter counter
myapp_mycounter 84

# HELP myapp_mygauge A random number we occasionally set.
# TYPE myapp_mygauge gauge
myapp_mygauge 42

# HELP examples_myhistogram A chat duration histogram.
# TYPE examples_myhistogram histogram
myapp_myhistogram_bucket{le="10"} 0
myapp_myhistogram_bucket{le="30"} 1
myapp_myhistogram_bucket{le="60"} 1
myapp_myhistogram_bucket{le="300"} 1
myapp_myhistogram_bucket{le="600"} 1
myapp_myhistogram_bucket{le="1800"} 1
myapp_myhistogram_bucket{le="3600"} 1
myapp_myhistogram_bucket{le="+Inf"} 1
myapp_myhistogram_sum 15.002
myapp_myhistogram_count 1

# HELP myapp_mysummary Compute quantiles and median of a random list of numbers.
# TYPE myapp_mysummary summary
myapp_mysummary{quantile="0.01"} 0.009997170550270069
myapp_mysummary{quantile="0.1"} 0.09957970759409267
myapp_mysummary{quantile="0.5"} 0.5016970195079504
myapp_mysummary{quantile="0.9"} 0.8993228241841542
myapp_mysummary{quantile="0.99"} 0.9901868947762174
myapp_mysummary_sum 4999.807495853165
myapp_mysummary_count 10000

Labels

Example:

var prometheus		= require("prometheus-wrapper");

// Init Counter
prometheus.createCounter("mycounter", "A number we occasionally increment.", ['foo']);

setInterval(function () {
	prometheus.get("mycounter").inc({foo: 'bar'}, 42);
	prometheus.get("mycounter").inc({foo: 'baz'}, 21);
}, 10000);

prometheus.get("mygauge").set(42);
$ curl 127.0.0.1:8080

# HELP myapp_mycounter A number we occasionally increment.
# TYPE myapp_mycounter counter
myapp_mycounter{foo="bar"} 42
myapp_mycounter{foo="baz"} 42

Full list of metrics types

Official documentation

Counter

A counter is a cumulative metric that represents a single numerical value that only ever goes up. A counter is typically used to count requests served, tasks completed, errors occurred, etc. Counters should not be used to expose current counts of items whose number can also go down, e.g. the number of currently running goroutines. Use gauges for this use case.

Gauge

A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.

Gauges are typically used for measured values like temperatures or current memory usage, but also "counts" that can go up and down, like the number of running goroutines.

Histogram

A histogram samples observations (usually things like request durations or response sizes) and counts them in configurable buckets. It also provides a sum of all observed values.

A histogram with a base metric name of <basename> exposes multiple time series during a scrape:

- cumulative counters for the observation buckets, exposed as <basename>_bucket{le="<upper inclusive bound>"}
- the total sum of all observed values, exposed as <basename>_sum
- the count of events that have been observed, exposed as <basename>_count (identical to <basename>_bucket{le="+Inf"} above)

Summary

Similar to a histogram, a summary samples observations (usually things like request durations and response sizes). While it also provides a total count of observations and a sum of all observed values, it calculates configurable quantiles over a sliding time window.

A summary with a base metric name of <basename> exposes multiple time series during a scrape:

- streaming φ-quantiles (0 ≤ φ ≤ 1) of observed events, exposed as <basename>{quantile="<φ>"}
- the total sum of all observed values, exposed as <basename>_sum
- the count of events that have been observed, exposed as <basename>_count

Methods available

setNamespace

  • client.setNamespace(<namespace>) => prefixes every metric name

getMetrics

  • client.getMetrics() => what to expose in your server under /metrics

Counter

  • client.createCounter(<name>, <help>, [ <label-list> ])
  • client.get(<name>).get()
  • client.get(<name>).inc()
  • client.get(<name>).inc(<delta>)

Gauge

  • client.createGauge(<name>, <help>, [ <label-list> ])
  • client.get(<name>).get()
  • client.get(<name>).set(<value>)
  • client.get(<name>).setToCurrentTime() => expose a timestamp in ms
  • var end = client.get(<name>).startTimer() => call end() to stop the timer, expose a timestamp in seconds

Histogram

  • client.createHistogram(<name>, <help>, buckets: [ <categories> ], [ <label-list> ])
  • client.get(<name>).get()
  • client.get(<name>).observe(<value>)
  • client.get(<name>).reset()
  • var end = client.get(<name>).startTimer() => call end() to stop the timer, expose a timestamp in seconds

Summary

  • client.createSummary(<name>, <help>, buckets: [ <categories> ], [ <label-list> ])
  • client.get(<name>).get()
  • client.get(<name>).observe(<value>)
  • client.get(<name>).reset()
  • var end = client.get(<name>).startTimer() => call end() to stop the timer, expose a timestamp in seconds