import {Tags, TimerWrapper, TimerWrapperFactory} from '@btilford/ts-base-core';
import {StatsDMetric, StatsDOptions} from './metric';
class StatsDTimerWrapper<T> extends StatsDMetric implements TimerWrapper<T> {
wrap(func: (...args: unknown[]) => T): (...args: unknown[]) => T {
return this.statsd.timer(
func,
this.name,
this.options.sampleRate,
{ ...this.tags },
);
}
}
class AsyncStatsDTimerWrapper<T> extends StatsDMetric implements TimerWrapper<Promise<T>> {
wrap(func: (...args: unknown[]) => Promise<T>): (...args: unknown[]) => Promise<T> {
return this.statsd.asyncTimer(
func,
this.name,
this.options.sampleRate,
{ ...this.tags },
);
}
}
export class StatsDTimerFactory extends TimerWrapperFactory {
protected readonly options: StatsDOptions;
constructor(options: StatsDOptions = {}) {
super();
this.options = { ...options };
}
timer<T>(name: string, tags: Tags = {}): TimerWrapper<T> {
return new StatsDTimerWrapper<T>(name, this.options, tags);
}
asyncTimer<T>(name: string, tags: Tags = {}): TimerWrapper<Promise<T>> {
return new AsyncStatsDTimerWrapper<T>(name, this.options, tags);
}
}
|