@libp2p/prometheus-metrics
Version:
Collect libp2p metrics for scraping by Prometheus or Graphana
31 lines • 939 B
JavaScript
import { Counter as PromCounter } from 'prom-client';
import { normalizeString } from './utils.js';
export class PrometheusCounter {
counter;
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.inc(await opts.calculate());
};
}
this.counter = new PromCounter({
name,
help,
labelNames: labels,
registers: opts.registry !== undefined ? [opts.registry] : undefined,
collect
});
}
increment(value = 1) {
this.counter.inc(value);
}
reset() {
this.counter.reset();
}
}
//# sourceMappingURL=counter.js.map