UNPKG

@libp2p/prometheus-metrics

Version:

Collect libp2p metrics for scraping by Prometheus or Graphana

47 lines 1.59 kB
import { Summary as PromSummary } from 'prom-client'; import { normalizeString } from './utils.js'; export class PrometheusSummaryGroup { summary; 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.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: [this.label], registers: opts.registry !== undefined ? [opts.registry] : undefined, collect }); } observe(values) { Object.entries(values).forEach(([key, value]) => { this.summary.observe({ [this.label]: key }, value); }); } reset() { this.summary.reset(); } timer(key) { return this.summary.startTimer({ [key]: 0 }); } } //# sourceMappingURL=summary-group.js.map