UNPKG

@libp2p/prometheus-metrics

Version:

Collect libp2p metrics for scraping by Prometheus or Graphana

35 lines 1.12 kB
import { Histogram as PromHistogram } from 'prom-client'; import { normalizeString } from './utils.js'; export class PrometheusHistogram { histogram; constructor(name, opts) { name = normalizeString(name); const help = normalizeString(opts.help ?? name); const labels = opts.label != null ? [normalizeString(opts.label)] : []; let collect; // calculated metric if (opts?.calculate != null) { collect = async function () { this.observe(await opts.calculate()); }; } this.histogram = new PromHistogram({ name, help, buckets: opts.buckets ?? [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10], labelNames: labels, registers: opts.registry !== undefined ? [opts.registry] : undefined, collect }); } observe(value) { this.histogram.observe(value); } reset() { this.histogram.reset(); } timer() { return this.histogram.startTimer(); } } //# sourceMappingURL=histogram.js.map