Additional metrics

This commit is contained in:
2023-10-16 20:23:16 -06:00
parent 295b217539
commit 84b6b9f385
4 changed files with 39 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ import { Response } from 'express';
import { identity, prop } from 'ramda'; import { identity, prop } from 'ramda';
import * as xml from 'xml'; import * as xml from 'xml';
import { InjectMetric } from '@willsoto/nestjs-prometheus'; import { InjectMetric } from '@willsoto/nestjs-prometheus';
import { Gauge } from 'prom-client'; import { Gauge, Histogram } from 'prom-client';
import { ApiTags } from '@nestjs/swagger'; import { ApiTags } from '@nestjs/swagger';
const episodeFiles: [number, string][] = [ const episodeFiles: [number, string][] = [
@@ -147,10 +147,12 @@ export class DinosaurwetController {
constructor( constructor(
@InjectMetric('weekly_count') public weeklyCount: Gauge<string>, @InjectMetric('weekly_count') public weeklyCount: Gauge<string>,
@InjectMetric('daily_count') public dailyCount: Gauge<string>, @InjectMetric('daily_count') public dailyCount: Gauge<string>,
@InjectMetric('rss_query_count') public queryCount: Gauge<string>,
) {} ) {}
@Get('') @Get('')
getAllAtOnce(@Res() response: Response) { getAllAtOnce(@Res() response: Response) {
this.queryCount.inc();
response.header('Content-Type', 'application/xml'); response.header('Content-Type', 'application/xml');
const feed = buildFeeds('Dinosaur Wet - All At Once', episodes); const feed = buildFeeds('Dinosaur Wet - All At Once', episodes);
return response.send(feed); return response.send(feed);
@@ -158,6 +160,7 @@ export class DinosaurwetController {
@Get('daily') @Get('daily')
getDaily(@Res() response: Response) { getDaily(@Res() response: Response) {
this.queryCount.inc();
response.header('Content-Type', 'application/xml'); response.header('Content-Type', 'application/xml');
const feed = buildFeeds( const feed = buildFeeds(
'Dinosaur Wet - Daily', 'Dinosaur Wet - Daily',
@@ -171,6 +174,7 @@ export class DinosaurwetController {
@Get('weekly') @Get('weekly')
getWeekly(@Res() response: Response) { getWeekly(@Res() response: Response) {
this.queryCount.inc();
response.header('Content-Type', 'application/xml'); response.header('Content-Type', 'application/xml');
const feed = buildFeeds( const feed = buildFeeds(
'Dinosaur Wet - Weekly', 'Dinosaur Wet - Weekly',

View File

@@ -24,6 +24,10 @@ import {
name: 'weekly_count', name: 'weekly_count',
help: 'The current weekly Dinosaur Wet episode count', help: 'The current weekly Dinosaur Wet episode count',
}), }),
makeGaugeProvider({
name: 'rss_query_count',
help: 'Total RSS endpoint queries',
}),
], ],
}) })
export class DinosaurwetModule {} export class DinosaurwetModule {}

View File

@@ -3,6 +3,8 @@ import { ApiProperty, ApiTags } from '@nestjs/swagger';
const ogs = require('open-graph-scraper'); const ogs = require('open-graph-scraper');
import { SuccessResult } from 'open-graph-scraper'; import { SuccessResult } from 'open-graph-scraper';
import { OgScraperService } from './ogscraper.service'; import { OgScraperService } from './ogscraper.service';
import { InjectMetric } from '@willsoto/nestjs-prometheus';
import { Histogram } from 'prom-client';
class ScrapeOgDto { class ScrapeOgDto {
@ApiProperty({ @ApiProperty({
@@ -16,10 +18,17 @@ class ScrapeOgDto {
@Controller('ogscraper') @Controller('ogscraper')
@ApiTags('open-graph-scraper') @ApiTags('open-graph-scraper')
export class OgScraperController { export class OgScraperController {
constructor(private readonly ogScraperService: OgScraperService) {} constructor(
private readonly ogScraperService: OgScraperService,
@InjectMetric('generation_time')
public generationTime: Histogram<string>,
) {}
@Post('') @Post('')
async scrapeOg(@Body() body: ScrapeOgDto): Promise<SuccessResult> { async scrapeOg(@Body() body: ScrapeOgDto): Promise<SuccessResult> {
return this.ogScraperService.getOg(body.url); const end = this.generationTime.startTimer();
const response = await this.ogScraperService.getOg(body.url);
end();
return response;
} }
} }

View File

@@ -1,10 +1,28 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { OgScraperController } from './ogscraper.controller'; import { OgScraperController } from './ogscraper.controller';
import { OgScraperService } from './ogscraper.service'; import { OgScraperService } from './ogscraper.service';
import {
PrometheusModule,
makeHistogramProvider,
} from '@willsoto/nestjs-prometheus';
@Module({ @Module({
imports: [
PrometheusModule.register({
customMetricPrefix: 'ogscraper',
defaultMetrics: {
enabled: false,
},
}),
],
controllers: [OgScraperController], controllers: [OgScraperController],
providers: [OgScraperService], providers: [
OgScraperService,
makeHistogramProvider({
name: 'generation_time',
help: 'Open Graph Scraping response times',
}),
],
exports: [OgScraperService], exports: [OgScraperService],
}) })
export class OgScraperModule {} export class OgScraperModule {}