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

@uswitch/koa-prometheus

v1.0.3

Published

๐ŸŒก๏ธ A configurable Prometheus data collector with Koa middleware

Downloads

6,230

Readme

Contributors License type language test style

Overview

This package is a thin wrapper around prom-client & metrics, to provide prometheus formatted metrics for Koa applications.

It provides the following types of metric;

  • ๐Ÿ“Š Histogram - Raw number counts, bucketed
  • ๐Ÿ“ˆ Summary - Percentile calculated buckets
  • โฑ Meter - an EWMA decaying gauge for counting over time
  • ๐ŸŒก Gauge - A counter that can go both up and down
  • ๐ŸŽš Counter - Count number of times something happens
  • ๐Ÿท Labelling - Labelling to enable powerful Prometheus querying

It will also provide you with a /metrics endpoint to expose these metrics to prometheus


Usage

koa-prometheus is purely config based and configurable, but you can attach it to your Koa service using default metrics with the following;

import Koa from 'koa'
import Meter from '@uswitch/koa-prometheus'

const app = new Koa()
const meters = Meter({ /* Config */ }, { loadDefaults: true })

app.use(meters.middleware)   // The middleware that makes the meters available

app.get('/metrics', async (ctx) => (ctx.body = await meters.print()))

app.on(eventAccess, (ctx) => meters.automark(ctx))
app.on(eventError, () => meters.errorRate.mark(1))

app.listen(3000, () => signal.start(Listening on port 3000))

Purely config

The main philsophy of Koa prometheus is to provide a way to configure the metrics, and how to collect them, in pure JSON.

To do this, you can create a config file that contains a list of metrics, .e.g

[
  // A manually invoked meter
  { 
    "name": "namespace_metric_name",
    "help": "A description of the metric",
    "type": "Counter"
  },
  // An `automark` meter
  {
    "name": "namespace_autocollected_metric",
    "help": "A description of the metric",
    "type": "Histogram",
    "labelNames": [
      { "key": "method", "path": ["path", "to", "method"] },
      { "key": "status", "path": ["path", "to", "status"] }
    ],
    "mark": { 
      "method": "observe",
      "path": [ "path","to","value" ]
    }
  }
]

See the schema or the defaults for a more detailed look at how they are configured.

API

You get back the configured meter when you instantiate it, and you will get

Manually marking a meter

If you have a manual meter it will be available on the meters object. If you want to add labels to the meter, you must add them in the order they are defined in the config, i.e.

// "labelName": [ "a", "b", "c" ]

meter
  .myMeter
  .labels("value for a", "value for b", "value for c")
  .inc(1)

NodeJS metrics & GC Metrics

This library also utilises prom-client's collectDefaultMetrics & node-prometheus-gc-stats to collect CPU & Garbage Collection stats

Service standard metrics

To comply with Uswitch service standard monitoring, enable service standards:

const meters = Meter({ /* Config */ }, { loadStandards: true })

These will overwrite the http_request_duration_seconds, http_requests_total and errors_total metrics. The first two are automarked by @uswitch/koa-access. Mark the errors_total metric manually:

// Directly
app.on('error', (err, ctx) => {
  meters.errorsTotal.labels(err.name).inc(1)
})
// Or with @uswitch/koa-tracer
app.on(eventError, (err, ctx) => {
  meters.errorsTotal.labels(err.name).inc(1)
})

Contributors

Thanks goes to these wonderful people (emoji key):

| Dom Charlesworth๐Ÿ“– ๐Ÿ’ป ๐Ÿค” ๐Ÿ”Œ | David Annez๐Ÿ’ป ๐Ÿค” ๐Ÿ”Œ | | :---: | :---: |

This project follows the all-contributors specification. Contributions of any kind welcome!