8000 feat: add prometheus metrics by jgryffindor · Pull Request #386 · liftedinit/talib · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add prometheus metrics #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"passport-local": "^1.0.0",
"pg": "^8.9.0",
"pg-query-stream": "^4.4.0",
"prom-client": "^15.1.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0",
"rxjs-stream": "^5.0.0",
Expand Down
3 changes: 3 additions & 0 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { SystemWideMetric } from "./database/entities/systemwide-metric.entity";
import { SystemWideModule } from "./metrics/systemwide/systemwide.module";
import { Migration } from "./database/entities/migration.entity";
import { MigrationWhitelist } from "./database/entities/migration-whitelist.entity";
import { MonitoringModule } from "./metrics/monitoring/monitoring.module";

@Module({
controllers: [],
Expand Down Expand Up @@ -91,6 +92,7 @@ import { MigrationWhitelist } from "./database/entities/migration-whitelist.enti
}),
}),
TypeOrmModule.forFeature([Event, Transaction, TransactionDetails, PrometheusQuery, Metric]),
MonitoringModule,
AdminConfigModule,
AppConfigModule,
SchedulerConfigModule,
Expand All @@ -107,6 +109,7 @@ import { MigrationWhitelist } from "./database/entities/migration-whitelist.enti
GeoSchedulerModule,
],
})

export class AppModule {
constructor(private dataSource: DataSource) {}
}
2 changes: 2 additions & 0 deletions server/src/metrics/metrics.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import { SystemWideService } from "./systemwide/systemwide.service";
import { SystemWideMetric } from "../database/entities/systemwide-metric.entity";
import { LocationModule } from "./location/location.module";
import { LocationService } from "./location/location.service";
import { MonitoringModule } from "./monitoring/monitoring.module";

@Module({
imports: [
TypeOrmModule.forFeature([Metric, Location, Block, Transaction, SystemWideMetric, PrometheusQuery]),
MonitoringModule,
HttpModule,
MetricsSchedulerConfigModule,
PrometheusQueryModule,
Expand Down
21 changes: 21 additions & 0 deletions server/src/metrics/monitoring/monitoring.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Controller, Get, Logger, Res } from '@nestjs/common';
import { Response } from 'express';
import { Registry, collectDefaultMetrics, register } from 'prom-client';
import { METRIC_PREFIX } from './monitoring.service';

@Controller('monitoring')
export class MonitoringController {
private readonly logger = new Logger(MonitoringController.name);

constructor() {
// Initialize the Prometheus registry
collectDefaultMetrics({ register, prefix: METRIC_PREFIX });
}

@Get('metrics')
async getMetrics(@Res() res: Response): Promise<void> {
const metrics = await register.metrics();
res.set('Content-Type', register.contentType);
res.send(metrics);
}
}
21 changes: 21 additions & 0 deletions server/src/metrics/monitoring/monitoring.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
import { MonitoringService } from './monitoring.service';

@Injectable()
export class MonitoringMiddleware implements NestMiddleware {
private readonly logger = new Logger(MonitoringMiddleware.name);

constructor(private readonly monitoringService: MonitoringService) {
this.logger.log('MonitoringMiddleware initialized');
}

async use(req: any, res: any, next: () => void) {
if (req.originalUrl.includes('/monitoring/metrics')) {

// Update all custom metrics before serving the metrics endpoint
this.monitoringService.updateMetrics();
}
next();
}
}
34 changes: 34 additions & 0 deletions server/src/metrics/monitoring/monitoring.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// src/monitoring/monitoring.module.ts
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { TypeOrmModule } from "@nestjs/typeorm";
import { MonitoringService } from './monitoring.service';
import { MonitoringController } from './monitoring.controller';
import { PrometheusQueryModule } from '../prometheus-query/query.module';
import { PrometheusQuery } from '../../database/entities/prometheus-query.entity';
import { MetricsService } from '../metrics.service';
import { Metric } from '../../database/entities/metric.entity';
import { MetricsSchedulerConfigModule } from '../../config/metrics-scheduler/configuration.module';
import { Registry } from 'prom-client';
import { MonitoringMiddleware } from './monitoring.middleware';

@Module({
imports: [
TypeOrmModule.forFeature([Metric, PrometheusQuery]),
PrometheusQueryModule,
MetricsSchedulerConfigModule,
],
controllers: [MonitoringController],
providers: [
MonitoringService,
MetricsService,
Registry,
],
exports: [MonitoringService, MetricsService],
})
export class MonitoringModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(MonitoringMiddleware)
.forRoutes('monitoring/metrics');
}
}
125 changes: 125 additions & 0 deletions server/src/metrics/monitoring/monitoring.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { Injectable, Logger } from '@nestjs/common';
import {
register,
Gauge,
Counter,
Metric,
} from 'prom-client';
import { MetricsService } from '../metrics.service';

export const METRIC_PREFIX = 'talib_';

type MetricType = 'gauge' | 'counter';

type MetricInstance =
| Gauge<string>
| Counter<string>;

type MetricValue =
| { value: number; labels?: Record<string, string> }

interface MetricDefinition {
type: MetricType;
name: string;
help: string;
labels?: string[];
getValue: () => Promise<MetricValue>;
}

function isValueMetric(val: MetricValue): val is { value: number; labels?: Record<string, string> } {
return 'value' in val;
}

@Injectable()
export class MonitoringService {
private readonly logger = new Logger(MonitoringService.name);
private readonly metrics: Record<string, MetricInstance> = {};

// Define the metrics to be monitored
// Each metric has a type, name, help text, optional labels, and a function to get its value
private readonly metricDefinitions: MetricDefinition[] = [
{
type: 'gauge',
name: 'mfx_power_conversion',
help: 'The current MFX-to-power conversion rate',
getValue: async () => {
const res = await this.metricsService.getCurrent('mfxpowerconversion');
return {
value: res ? Number(res.data) : 0,
labels: { test: 'someValue' },
};
},
},
];

constructor(private readonly metricsService: MetricsService) {
// Initialize the Prometheus registry
register.clear();
register.setDefaultLabels({ app: 'talib' });
this.registerMetrics();
this.logger.log('MonitoringService initialized');
}

// Register metrics with Prometheus
private registerMetrics(): void {
for (const def of this.metricDefinitions) {
let metric: MetricInstance;
const metricName = METRIC_PREFIX + def.name;

switch (def.type) {
case 'gauge':
metric = new Gauge({
name: metricName,
help: def.help,
labelNames: def.labels || [],
});
break;
case 'counter':
metric = new Counter({
name: metricName,
help: def.help,
labelNames: def.labels || [],
});
break;
}

register.registerMetric(metric);
this.metrics[def.name] = metric;
}

this.logger.log(`Registered ${this.metricDefinitions.length} metrics`);
}

// Update metrics
async updateMetrics(): Promise<void> {
for (const def of this.metricDefinitions) {
const metric = this.metrics[def.name];
if (!metric) continue;

try {
const result = await def.getValue();
const labels = result.labels || {};

switch (def.type) {
case 'gauge':
if (isValueMetric(result)) {
def.labels?.length
? (metric as Gauge<string>).set(labels, result.value)
: (metric as Gauge<string>).set(result.value);
}
break;

case 'counter':
if (isValueMetric(result)) {
def.labels?.length
? (metric as Counter<string>).inc(labels, result.value)
: (metric as Counter<string>).inc(result.value);
}
break;
}
} catch (err) {
this.logger.error(`Error updating metric ${def.name}:`, err);
}
}
}
}
0