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-browser

v0.2.3

Published

prometheus client library for browser to interact with push gateway

Downloads

54

Readme

prometheus-browser workflow

Prometheus client implementation for browser to interact with push gateway

Metrics

Counter

import {Counter} from 'prometheus-browser'

const counter = new Counter({
  name: 'http_request_counter',
  help: 'A counter of the total number of requests',
  labels: ['status']
})
// increase by 1
counter.inc()

// increase by value
counter.inc(2)

// with label
counter.inc(2, {status: '200'})

Gauge

import {Gauge} from 'prometheus-browser'

const gauge = new Gauge({
  name: 'memory_usage',
  help: 'A gauge of the memory usage',
  labels: ['foo']
})
// increase by 1
gauge.inc()

// increase by value
gauge.inc(2)

// decrease by 1
gauge.dec()

// decrease by value
gauge.dec(2)

// set a value
gauge.set(0)

// with label
gauge.inc(2, {foo: 'bar'})

Histogram

import {Histogram} from 'prometheus-browser'

const histogram = new Histogram({
  name: 'http_request_duration',
  help: 'HTTP request duration',
  labels: ['foo']
})
// observe value
histogram.observe(0.5)
histogram.observe(2)

// with custom buckets
const histogram = new Histogram({
  name: 'http_request_duration',
  help: 'HTTP request duration',
  labels: ['foo'],
  buckets: [0.5, 1, 2, 5, 10]
})

// use a timer
const timer = histogram.startTimer()
// observe a timer
timer.observeDuration()

Default labels

import {defaultRegistry} from 'prometheus-browser'

// set default labels to a registry
// default labels will be merged with metric labels when exposing metrics from the registry
defaultRegistry.setDefaultLabels({environment: 'production'})

With Push Gateway

import {PushGateway} from 'prometheus-browser'

const gateway = new PushGateway({url: 'http://localhost:8080/metrics'})
// push metrics
// by default metrics are sent with "POST" request: https://github.com/prometheus/pushgateway#post-method
await gateway.push({job: 'pushgateway'})

// push to a group, the group is arrya of tuple: [string, string]
await gateway.push({job: 'pushgateway', group: [['client', 'foo']]})

// if you need "PUT" method: https://github.com/prometheus/pushgateway#put-method
await gateway.push({job: 'pushgateway', group: [['client', 'foo']], fetchOptions: {method: 'PUT'}})

// init gateway with fetch options, if you need apply a method or other options to every push
const gateway2 = new PushGateway({url: 'http://localhost:8080/metrics', fetchOptions: {method: 'PUT'}})
await gateway2.push({job: 'pushgateway', group: [['client', 'foo']]})