UNPKG

@libp2p/prometheus-metrics

Version:

Collect libp2p metrics for scraping by Prometheus or Graphana

43 lines 1.42 kB
import { Histogram as PromHistogram } from 'prom-client'; import { normalizeString } from './utils.js'; export class PrometheusHistogramGroup { histogram; label; constructor(name, opts) { name = normalizeString(name); const help = normalizeString(opts.help ?? name); const label = this.label = normalizeString(opts.label ?? name); let collect; // calculated metric if (opts?.calculate != null) { collect = async function () { const values = await opts.calculate(); Object.entries(values).forEach(([key, value]) => { this.observe({ [label]: key }, value); }); }; } 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: [this.label], registers: opts.registry !== undefined ? [opts.registry] : undefined, collect }); } observe(values) { Object.entries(values).forEach(([key, value]) => { this.histogram.observe({ [this.label]: key }, value); }); } reset() { this.histogram.reset(); } timer(key) { return this.histogram.startTimer({ [key]: 0 }); } } //# sourceMappingURL=histogram-group.js.map