import {CounterFactory, Providers, Counter, Tags, NotImplementedError} from '@btilford/ts-base-core';
import {PromMetric, PromOptions} from './metric';
import {Counter as Delegate, Registry} from 'prom-client';
export class PromCounter extends PromMetric implements Counter<number> {
protected readonly impl: Delegate<any>;
constructor(name: string, tags: Tags, options: PromOptions) {
super(name, tags, options);
this.impl = new Delegate({
name: this.name,
help: options.help,
registers: options.registry || [Providers.provide(Registry)],
aggregator: options.aggregator,
labelNames: [...this.supportedLabels],
});
}
decrement(val: number, tags?: Tags): void {
throw NotImplementedError.methodNotImplemented('decrement', PromCounter, 'Decrement is not supported in Prometheus!')
}
increment(val: number, tags: Tags = {}): void {
const _tags = this.filterTags({ ...this.tags, ...tags });
this.impl.inc(_tags, val);
}
reset(): void {
this.impl.reset();
}
}
export class PromCounterFactory extends CounterFactory<number> {
protected readonly options: PromOptions;
constructor(options: PromOptions) {
super();
this.options = { ...options };
}
counter(name: string, tags = {}): PromCounter {
return new PromCounter(name, tags, this.options);
}
}
|