-
Notifications
You must be signed in to change notification settings - Fork 2
/
provider.ts
39 lines (37 loc) · 1.02 KB
/
provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {Provider} from '@nestjs/common';
import {Config} from '../config';
import {Metrics} from '../enum';
import {Metric} from './generic';
import {MetricOptions} from './options';
import {getMetric, getToken} from './utils';
/**
* Metric Provider
*
* Will create a metric provider based on the metric type you need, and it's custom options.
*
* ```typescript
* import { Module } from "@nestjs/common";
* import { MetricsModule } from "@mists/nestjs-metrics";
*
* @Module({
* controllers: [MetricsController],
* imports: [MetricsModule.register()],
* providers: [makeMetricProvider(Metrics.Counter, 'metrics_injector', {})],
* })
* export class AppModule {}
* ```
*/
export const makeMetricProvider = (type: Metrics, name: string, options?: MetricOptions): Provider => {
if (!options.statsd) {
options = {
...options,
statsd: Config.getInstance().statsd || 'dummy',
};
}
return {
provide: getToken(name),
useFactory(): Metric {
return getMetric(type, name, options);
},
};
};