UNPKG

@libp2p/prometheus-metrics

Version:

Collect libp2p metrics for scraping by Prometheus or Graphana

40 lines 1.09 kB
import { Gauge } from 'prom-client'; import { normalizeString } from './utils.js'; export class PrometheusMetric { gauge; 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.set(await opts.calculate()); }; } this.gauge = new Gauge({ name, help, labelNames: labels, registers: opts.registry !== undefined ? [opts.registry] : undefined, collect }); } update(value) { this.gauge.set(value); } increment(value = 1) { this.gauge.inc(value); } decrement(value = 1) { this.gauge.dec(value); } reset() { this.gauge.reset(); } timer() { return this.gauge.startTimer(); } } //# sourceMappingURL=metric.js.map