@libp2p/prometheus-metrics
Version:
Collect libp2p metrics for scraping by Prometheus or Graphana
38 lines • 1.26 kB
JavaScript
import { Counter as PromCounter } from 'prom-client';
import { normalizeString } from './utils.js';
export class PrometheusCounterGroup {
counter;
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.inc({ [label]: key }, value);
});
};
}
this.counter = new PromCounter({
name,
help,
labelNames: [this.label],
registers: opts.registry !== undefined ? [opts.registry] : undefined,
collect
});
}
increment(values) {
Object.entries(values).forEach(([key, value]) => {
const inc = typeof value === 'number' ? value : 1;
this.counter.inc({ [this.label]: key }, inc);
});
}
reset() {
this.counter.reset();
}
}
//# sourceMappingURL=counter-group.js.map