Skip to content
This repository has been archived by the owner on Jul 21, 2022. It is now read-only.

Commit

Permalink
Prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibbp committed Feb 5, 2022
1 parent 2e542a0 commit 33a1970
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { VodsRepository } from './vods/vods.repository';
import { ChannelsRepository } from './channels/channels.repository';
import { UsersRepository } from './users/users.repository';
import { ScheduleModule } from '@nestjs/schedule';
import { MetricsModule } from './metrics/metrics.module';

@Module({
imports: [
Expand Down Expand Up @@ -81,6 +82,8 @@ import { ScheduleModule } from '@nestjs/schedule';
ExecModule,

ScheduleModule.forRoot(),

MetricsModule,
],
controllers: [AppController],
providers: [
Expand Down
20 changes: 20 additions & 0 deletions src/metrics/metrics.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MetricsController } from './metrics.controller';
import { MetricsService } from './metrics.service';

describe('MetricsController', () => {
let controller: MetricsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [MetricsController],
providers: [MetricsService],
}).compile();

controller = module.get<MetricsController>(MetricsController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
12 changes: 12 additions & 0 deletions src/metrics/metrics.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { MetricsService } from './metrics.service';

@Controller('metrics')
export class MetricsController {
constructor(private readonly metricsService: MetricsService) {}

@Get()
getMetrics(): Promise<any> {
return this.metricsService.getMetrics();
}
}
20 changes: 20 additions & 0 deletions src/metrics/metrics.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Module } from '@nestjs/common';
import { MetricsService } from './metrics.service';
import { MetricsController } from './metrics.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { VodsRepository } from 'src/vods/vods.repository';
import { QueuesRepository } from 'src/queues/queues.repository';
import { ChannelsRepository } from 'src/channels/channels.repository';

@Module({
imports: [
TypeOrmModule.forFeature([
VodsRepository,
QueuesRepository,
ChannelsRepository,
]),
],
controllers: [MetricsController],
providers: [MetricsService],
})
export class MetricsModule {}
18 changes: 18 additions & 0 deletions src/metrics/metrics.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MetricsService } from './metrics.service';

describe('MetricsService', () => {
let service: MetricsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [MetricsService],
}).compile();

service = module.get<MetricsService>(MetricsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
50 changes: 50 additions & 0 deletions src/metrics/metrics.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
Injectable,
InternalServerErrorException,
Logger,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ChannelsRepository } from 'src/channels/channels.repository';
import { QueuesRepository } from 'src/queues/queues.repository';
import { VodsRepository } from 'src/vods/vods.repository';

@Injectable()
export class MetricsService {
private logger = new Logger('MetricsService');

constructor(
@InjectRepository(ChannelsRepository)
private readonly channelsRepository: ChannelsRepository,
@InjectRepository(QueuesRepository)
private readonly queuesRepository: QueuesRepository,
@InjectRepository(VodsRepository)
private readonly vodsRepository: VodsRepository,
) {}
async getMetrics() {
try {
const vodCount = await this.vodsRepository
.createQueryBuilder('vods')
.getCount();
const channelCount = await this.channelsRepository
.createQueryBuilder('channels')
.getCount();
const channels = await this.channelsRepository
.createQueryBuilder('channel')
.loadRelationCountAndMap('channel.vodCount', 'channel.vods', 'vod')
.getMany();

let channelsString = '';

for await (const channel of channels) {
channelsString += `channel_vod_count{id="${channel.displayName}"} ${channel['vodCount']}\n`;
}

const metrics =
`vod_count{id="ceres"} ${vodCount}\nchannel_count{id="ceres"} ${channelCount}\n${channelsString}`.toString();
return metrics;
} catch (error) {
this.logger.error(error);
throw new InternalServerErrorException('Error fetching metrics');
}
}
}

0 comments on commit 33a1970

Please sign in to comment.