import {StatsD} from 'hot-shots';
import {Tags, Providers} from '@btilford/ts-base-core';
import * as os from 'os';
export type StatsDOptions = {
statsd?: StatsD;
sampleRate?: number;
tags?: Tags;
prefix?: string;
suffix?: string;
}
export class StatsDMetric {
readonly tags: Tags;
protected readonly options: StatsDOptions;
protected readonly statsd: StatsD;
constructor(
readonly name: string,
options: StatsDOptions,
tags: Tags = {}
) {
this.options = { ...options };
this.tags = {
pid: `${process.pid}`,
NODE_ENV: process.env.NODE_ENV || 'not set',
host: os.hostname(),
os: os.type(),
platform: os.platform(),
...options.tags,
...tags
};
const statsd = options.statsd || Providers.provide(StatsD);
if (this.options.prefix || this.options.suffix) {
this.statsd = statsd?.childClient(this.options);
} else {
this.statsd = statsd;
}
}
}
|