Skip to content

Commit

Permalink
refactor(server): metric repo (#8278)
Browse files Browse the repository at this point in the history
* refactor

* redundant `implements`

* simplify

* remove `enabled`
  • Loading branch information
mertalev committed Mar 25, 2024
1 parent c56c04a commit c45e28a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 31 deletions.
20 changes: 14 additions & 6 deletions server/src/interfaces/metric.interface.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { MetricOptions } from '@opentelemetry/api';

export interface CustomMetricOptions extends MetricOptions {
enabled?: boolean;
export const IMetricRepository = 'IMetricRepository';

export interface MetricGroupOptions {
enabled: boolean;
}

export const IMetricRepository = 'IMetricRepository';
export interface IMetricGroupRepository {
addToCounter(name: string, value: number, options?: MetricOptions): void;
addToGauge(name: string, value: number, options?: MetricOptions): void;
addToHistogram(name: string, value: number, options?: MetricOptions): void;
configure(options: MetricGroupOptions): this;
}

export interface IMetricRepository {
addToCounter(name: string, value: number, options?: CustomMetricOptions): void;
updateGauge(name: string, value: number, options?: CustomMetricOptions): void;
updateHistogram(name: string, value: number, options?: CustomMetricOptions): void;
api: IMetricGroupRepository;
host: IMetricGroupRepository;
jobs: IMetricGroupRepository;
repo: IMetricGroupRepository;
}
51 changes: 34 additions & 17 deletions server/src/repositories/metric.repository.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
import { Inject } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { MetricOptions } from '@opentelemetry/api';
import { MetricService } from 'nestjs-otel';
import { CustomMetricOptions, IMetricRepository } from 'src/interfaces/metric.interface';
import { IMetricGroupRepository, IMetricRepository, MetricGroupOptions } from 'src/interfaces/metric.interface';
import { apiMetrics, hostMetrics, jobMetrics, repoMetrics } from 'src/utils/instrumentation';

export class MetricRepository implements IMetricRepository {
constructor(@Inject(MetricService) private readonly metricService: MetricService) {}
class MetricGroupRepository implements IMetricGroupRepository {
private enabled = false;
constructor(private readonly metricService: MetricService) {}

addToCounter(name: string, value: number, options?: CustomMetricOptions): void {
if (options?.enabled === false) {
return;
addToCounter(name: string, value: number, options?: MetricOptions): void {
if (this.enabled) {
this.metricService.getCounter(name, options).add(value);
}
}

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);
}
}

updateGauge(name: string, value: number, options?: CustomMetricOptions): void {
if (options?.enabled === false) {
return;
addToHistogram(name: string, value: number, options?: MetricOptions): void {
if (this.enabled) {
this.metricService.getHistogram(name, options).record(value);
}
}

this.metricService.getUpDownCounter(name, options).add(value);
configure(options: MetricGroupOptions): this {
this.enabled = options.enabled;
return this;
}
}

updateHistogram(name: string, value: number, options?: CustomMetricOptions): void {
if (options?.enabled === false) {
return;
}
@Injectable()
export class MetricRepository implements IMetricRepository {
api: MetricGroupRepository;
host: MetricGroupRepository;
jobs: MetricGroupRepository;
repo: MetricGroupRepository;

this.metricService.getHistogram(name, options).record(value);
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 });
}
}
9 changes: 4 additions & 5 deletions server/src/services/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
import { IMetricRepository } from 'src/interfaces/metric.interface';
import { IPersonRepository } from 'src/interfaces/person.interface';
import { ISystemConfigRepository } from 'src/interfaces/system-config.interface';
import { jobMetrics } from 'src/utils/instrumentation';
import { ImmichLogger } from 'src/utils/logger';

@Injectable()
Expand Down Expand Up @@ -96,7 +95,7 @@ export class JobService {
throw new BadRequestException(`Job is already running`);
}

this.metricRepository.addToCounter(`immich.queues.${snakeCase(name)}.started`, 1), { enabled: jobMetrics };
this.metricRepository.jobs.addToCounter(`immich.queues.${snakeCase(name)}.started`, 1);

switch (name) {
case QueueName.VIDEO_CONVERSION: {
Expand Down Expand Up @@ -163,20 +162,20 @@ export class JobService {
const { name, data } = item;

const queueMetric = `immich.queues.${snakeCase(queueName)}.active`;
this.metricRepository.updateGauge(queueMetric, 1, { enabled: jobMetrics });
this.metricRepository.jobs.addToGauge(queueMetric, 1);

try {
const handler = jobHandlers[name];
const status = await handler(data);
const jobMetric = `immich.jobs.${name.replaceAll('-', '_')}.${status}`;
this.metricRepository.addToCounter(jobMetric, 1, { enabled: jobMetrics });
this.metricRepository.jobs.addToCounter(jobMetric, 1);
if (status === JobStatus.SUCCESS || status == JobStatus.SKIPPED) {
await this.onDone(item);
}
} catch (error: Error | any) {
this.logger.error(`Unable to run job handler (${queueName}/${name}): ${error}`, error?.stack, data);
} finally {
this.metricRepository.updateGauge(queueMetric, -1, { enabled: jobMetrics });
this.metricRepository.jobs.addToGauge(queueMetric, -1);
}
});
}
Expand Down
27 changes: 24 additions & 3 deletions server/test/repositories/metric.repository.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@ import { IMetricRepository } from 'src/interfaces/metric.interface';

export const newMetricRepositoryMock = (): jest.Mocked<IMetricRepository> => {
return {
addToCounter: jest.fn(),
updateGauge: jest.fn(),
updateHistogram: jest.fn(),
api: {
addToCounter: jest.fn(),
addToGauge: jest.fn(),
addToHistogram: jest.fn(),
configure: jest.fn(),
},
host: {
addToCounter: jest.fn(),
addToGauge: jest.fn(),
addToHistogram: jest.fn(),
configure: jest.fn(),
},
jobs: {
addToCounter: jest.fn(),
addToGauge: jest.fn(),
addToHistogram: jest.fn(),
configure: jest.fn(),
},
repo: {
addToCounter: jest.fn(),
addToGauge: jest.fn(),
addToHistogram: jest.fn(),
configure: jest.fn(),
},
};
};

0 comments on commit c45e28a

Please sign in to comment.