import {Histogram, HistogramConfiguration} from "prom-client"; import {IHistogram} from "../interface.js"; type Labels = Partial>; /** * Extends the prom-client Histogram with extra features: * - Add multiple collect functions after instantiation * - Create child histograms with fixed labels */ export class HistogramExtra extends Histogram implements IHistogram { constructor(configuration: HistogramConfiguration) { super(configuration); } child(labels: Labels): HistogramChild { return new HistogramChild(labels, this); } } export class HistogramChild implements IHistogram { histogram: HistogramExtra; labelsParent: Labels; constructor(labelsParent: Labels, histogram: HistogramExtra) { this.histogram = histogram; this.labelsParent = labelsParent; } // Sorry for this mess, `prom-client` API choices are not great // If the function signature was `observe(value: number, labels?: Labels)`, this would be simpler observe(value?: number): void; observe(labels: Labels, value?: number): void; observe(arg1?: Labels | number, arg2?: number): void { if (typeof arg1 === "object") { this.histogram.observe({...this.labelsParent, ...arg1}, arg2 ?? 0); } else { this.histogram.observe(this.labelsParent, arg1 ?? 0); } } startTimer(arg1?: Labels): (labels?: Labels) => number { if (typeof arg1 === "object") { return this.histogram.startTimer({...this.labelsParent, ...arg1}); } else { return this.histogram.startTimer(this.labelsParent); } } }