0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-21 00:52:43 -05:00
immich/server/src/repositories/metric.repository.ts
AmAn Sharma 6e6deec40c
feat: use ILoggerRepository (#8855)
* Migrate ImmichLogger over to injected ILoggerRepository

* chore: cleanup and tests

---------

Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
2024-04-16 17:30:31 -04:00

48 lines
1.7 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { MetricOptions } from '@opentelemetry/api';
import { MetricService } from 'nestjs-otel';
import { IMetricGroupRepository, IMetricRepository, MetricGroupOptions } from 'src/interfaces/metric.interface';
import { apiMetrics, hostMetrics, jobMetrics, repoMetrics } from 'src/utils/instrumentation';
class MetricGroupRepository implements IMetricGroupRepository {
private enabled = false;
constructor(private metricService: MetricService) {}
addToCounter(name: string, value: number, options?: MetricOptions): void {
if (this.enabled) {
this.metricService.getCounter(name, options).add(value);
}
}
addToGauge(name: string, value: number, options?: MetricOptions): void {
if (this.enabled) {
this.metricService.getUpDownCounter(name, options).add(value);
}
}
addToHistogram(name: string, value: number, options?: MetricOptions): void {
if (this.enabled) {
this.metricService.getHistogram(name, options).record(value);
}
}
configure(options: MetricGroupOptions): this {
this.enabled = options.enabled;
return this;
}
}
@Injectable()
export class MetricRepository implements IMetricRepository {
api: MetricGroupRepository;
host: MetricGroupRepository;
jobs: MetricGroupRepository;
repo: MetricGroupRepository;
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 });
}
}