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

inspector-prometheus

v2.8.0

Published

Prometheus / Pushgateway metric reporter for nodejs

Downloads

11

Readme

inspector-prometheus

Typescript metric reporter for prometheus.

This library is made for inspector-metrics node module and is meant to be used with nodejs.

All metrics from the inspector-metrics library can be pushed to a pushgateway or be exposed with a custom /metrics endpoint in your application.

Take a look at the Documentation.

install

npm install --save inspector-prometheus

basic usage

example.ts

import {
    MetricRegistry,
} from "inspector-metrics";

import {
    PrometheusMetricReporter,
} from "inspector-prometheus";

// contains all metrics
const registry = new MetricRegistry();
// exposes the metrics
const reporter = new PrometheusMetricReporter({});

// register the registry within the reporter
reporter.addMetricRegistry(registry);

// common tags for all metrics
const tags = new Map();
tags.set("app_version", "1.0.0");
reporter.setTags(tags);

// a simple request timer used to report response latencies
const requests: Timer = registry.newTimer("requests");
// custom metric tag
requests.setTag("host", "127.0.0.3");

// some server implementation - could be anything KOA, Express, HAPI ...
const server = new Hapi.Server({ host: "0.0.0.0", port: 8080 });

// '/metrics' is the standard route used by prometheus ...
server.route({
    method: "GET",
    path: "/metrics",
    handler(request, h) {
        console.log("reporting metrics");
        return h.response(reporter.getMetricsString())
            .code(200)
            .type("text/plain");
    },
});

// starts the server
server.start();

/etc/prometheus/config.yml

global:
  scrape_interval:     15s
  evaluation_interval: 30s

scrape_configs:
- job_name: test-app
  metrics_path: /metrics
  static_configs:
    - targets:
      - localhost:8080

example metrics report

# HELP requests request durations for some endpoint
# TYPE requests summary
requests{app_version="1.0.0",host="127.0.0.3",quantile="0.01"} 0
requests{app_version="1.0.0",host="127.0.0.3",quantile="0.05"} 0
requests{app_version="1.0.0",host="127.0.0.3",quantile="0.5"} 999936
requests{app_version="1.0.0",host="127.0.0.3",quantile="0.75"} 999936
requests{app_version="1.0.0",host="127.0.0.3",quantile="0.9"} 1000192
requests{app_version="1.0.0",host="127.0.0.3",quantile="0.95"} 1000192
requests{app_version="1.0.0",host="127.0.0.3",quantile="0.98"} 1999872
requests{app_version="1.0.0",host="127.0.0.3",quantile="0.99"} 2000128
requests{app_version="1.0.0",host="127.0.0.3",quantile="0.999"} 4000000
requests_count{app_version="1.0.0",host="127.0.0.3"} 362
requests_sum{app_version="1.0.0",host="127.0.0.3"} 283998208

reporting options for PrometheusMetricReporter

import {
    PrometheusMetricReporter,
} from "inspector-prometheus";

const reporter = new PrometheusMetricReporter({
    includeTimestamp: true,
    emitComments: true,
    useUntyped: false,
});

multi process support (nodejs cluster)

Due to the nature of prometheus scraping multiple processes need to collect
metrics in order report all metrics of every process.

Therefore the PrometheusMetricReporter implements an internal
request/response mechanism to gather all metrics from all forked processes
and wait for the response before serving all metrics data.

You should set the pid as reporter tag to be able to determine
between the multiple metrics sources.

Only the master process should serve the metrics to the prometheus server.

import * as cluster from "cluster";

import {
    tagsToMap,
} from "inspector-metrics";

import {
    PrometheusMetricReporter,
} from "inspector-prometheus";

const reporter = new PrometheusMetricReporter({});

// set "pid" to process id
reporter.setTags(tagsToMap({
    pid: `${process.pid}`,
}));

if (cluster.isMaster) {
    // some server implementation - could be anything KOA, Express, HAPI ...
    const server = new Hapi.Server({ host: "0.0.0.0", port: 8080 });

    // '/metrics' is the standard route used by prometheus ...
    server.route({
        method: "GET",
        path: "/metrics",
        handler(request, h) {
            console.log("reporting metrics");
            return h.response(reporter.getMetricsString())
                .code(200)
                .type("text/plain");
        },
    });

    // starts the server
    server.start();
}

report metrics with pushgateway

import ...; // like in the example above

import {
    PrometheusMetricReporter,
    PushgatewayMetricReporter,
} from "inspector-prometheus";

// contains all metrics
const registry = new MetricRegistry();
// exposes the metrics
const reporter = new PrometheusMetricReporter({});

// register the registry within the reporter
reporter.addMetricRegistry(registry);

const pushReporter = new PushgatewayMetricReporter({
    reporter,

    host: "localhost",
    port: 9091,
    job: "pushgateway",
    instance: "127.0.0.4",
});

// start reporting
await pushReporter.start();

multi process support (nodejs cluster)

By default cluster support is disabled for PushgatewayMetricReporter.
You should set the pid as reporter tag.

import {
    tagsToMap,
} from "inspector-metrics";

import {
    PrometheusMetricReporter,
    PushgatewayMetricReporter,
} from "inspector-prometheus";

const reporter = new PrometheusMetricReporter({});
const pushReporter = new PushgatewayMetricReporter({
    reporter,
    ...
});

// set "pid" to process id
reporter.setTags(tagsToMap({
    pid: `${process.pid}`,
}));

// start reporting
await pushReporter.start();

License

MIT