2024-03-25 19:15:11 -04:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { MetricOptions } from '@opentelemetry/api';
|
2024-03-24 23:02:04 -04:00
|
|
|
import { MetricService } from 'nestjs-otel';
|
2024-03-25 19:15:11 -04:00
|
|
|
import { IMetricGroupRepository, IMetricRepository, MetricGroupOptions } from 'src/interfaces/metric.interface';
|
|
|
|
import { apiMetrics, hostMetrics, jobMetrics, repoMetrics } from 'src/utils/instrumentation';
|
2024-03-24 23:02:04 -04:00
|
|
|
|
2024-03-25 19:15:11 -04:00
|
|
|
class MetricGroupRepository implements IMetricGroupRepository {
|
|
|
|
private enabled = false;
|
2024-04-17 03:00:31 +05:30
|
|
|
constructor(private metricService: MetricService) {}
|
2024-03-24 23:02:04 -04:00
|
|
|
|
2024-03-25 19:15:11 -04:00
|
|
|
addToCounter(name: string, value: number, options?: MetricOptions): void {
|
|
|
|
if (this.enabled) {
|
|
|
|
this.metricService.getCounter(name, options).add(value);
|
2024-03-24 23:02:04 -04:00
|
|
|
}
|
2024-03-25 19:15:11 -04:00
|
|
|
}
|
2024-03-24 23:02:04 -04:00
|
|
|
|
2024-03-25 19:15:11 -04:00
|
|
|
addToGauge(name: string, value: number, options?: MetricOptions): void {
|
|
|
|
if (this.enabled) {
|
|
|
|
this.metricService.getUpDownCounter(name, options).add(value);
|
|
|
|
}
|
2024-03-24 23:02:04 -04:00
|
|
|
}
|
|
|
|
|
2024-03-25 19:15:11 -04:00
|
|
|
addToHistogram(name: string, value: number, options?: MetricOptions): void {
|
|
|
|
if (this.enabled) {
|
|
|
|
this.metricService.getHistogram(name, options).record(value);
|
2024-03-24 23:02:04 -04:00
|
|
|
}
|
2024-03-25 19:15:11 -04:00
|
|
|
}
|
2024-03-24 23:02:04 -04:00
|
|
|
|
2024-03-25 19:15:11 -04:00
|
|
|
configure(options: MetricGroupOptions): this {
|
|
|
|
this.enabled = options.enabled;
|
|
|
|
return this;
|
2024-03-24 23:02:04 -04:00
|
|
|
}
|
2024-03-25 19:15:11 -04:00
|
|
|
}
|
2024-03-24 23:02:04 -04:00
|
|
|
|
2024-03-25 19:15:11 -04:00
|
|
|
@Injectable()
|
|
|
|
export class MetricRepository implements IMetricRepository {
|
|
|
|
api: MetricGroupRepository;
|
|
|
|
host: MetricGroupRepository;
|
|
|
|
jobs: MetricGroupRepository;
|
|
|
|
repo: MetricGroupRepository;
|
2024-03-24 23:02:04 -04:00
|
|
|
|
2024-03-25 19:15:11 -04:00
|
|
|
constructor(metricService: MetricService) {
|
|
|
|
this.api = new MetricGroupRepository(metricService).configure({ enabled: apiMetrics });
|
|
|
|
this.host = new MetricGroupRepository(metricService).configure({ enabled: hostMetrics });
|
|
|
|
this.jobs = new MetricGroupRepository(metricService).configure({ enabled: jobMetrics });
|
|
|
|
this.repo = new MetricGroupRepository(metricService).configure({ enabled: repoMetrics });
|
2024-03-24 23:02:04 -04:00
|
|
|
}
|
|
|
|
}
|