@libp2p/prometheus-metrics
Version:
Collect libp2p metrics for scraping by Prometheus or Graphana
39 lines • 1.29 kB
JavaScript
import { Summary as PromSummary } from 'prom-client';
import { normalizeString } from './utils.js';
export class PrometheusSummary {
summary;
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.summary = new PromSummary({
name,
help,
percentiles: opts.percentiles ?? [0.01, 0.05, 0.5, 0.9, 0.95, 0.99, 0.999],
maxAgeSeconds: opts.maxAgeSeconds,
ageBuckets: opts.ageBuckets,
pruneAgedBuckets: opts.pruneAgedBuckets,
compressCount: opts.compressCount ?? 1000,
labelNames: labels,
registers: opts.registry !== undefined ? [opts.registry] : undefined,
collect
});
}
observe(value) {
this.summary.observe(value);
}
reset() {
this.summary.reset();
}
timer() {
return this.summary.startTimer();
}
}
//# sourceMappingURL=summary.js.map