68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import { Controller, Get, Res } from '@nestjs/common';
|
|
import { ApiExcludeController } from '@nestjs/swagger';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
import * as express from 'express';
|
|
|
|
@ApiExcludeController()
|
|
@Controller('metrics')
|
|
export class MetricsController {
|
|
private static requestCount = 0;
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
public static incrementRequestCount() {
|
|
this.requestCount++;
|
|
}
|
|
|
|
@Get()
|
|
async getMetrics(@Res() res: express.Response) {
|
|
const memory = process.memoryUsage();
|
|
const cpu = process.cpuUsage();
|
|
|
|
let dbStatus = 1;
|
|
try {
|
|
await this.prisma.$queryRaw`SELECT 1`;
|
|
} catch (e) {
|
|
dbStatus = 0;
|
|
}
|
|
|
|
const uptime = process.uptime();
|
|
|
|
const responseText = `# HELP node_memory_rss_bytes Resident set size in bytes.
|
|
# TYPE node_memory_rss_bytes gauge
|
|
node_memory_rss_bytes ${memory.rss}
|
|
|
|
# HELP node_memory_heap_used_bytes Heap used in bytes.
|
|
# TYPE node_memory_heap_used_bytes gauge
|
|
node_memory_heap_used_bytes ${memory.heapUsed}
|
|
|
|
# HELP node_memory_heap_total_bytes Heap total in bytes.
|
|
# TYPE node_memory_heap_total_bytes gauge
|
|
node_memory_heap_total_bytes ${memory.heapTotal}
|
|
|
|
# HELP node_cpu_user_time_microseconds CPU user time in microseconds.
|
|
# TYPE node_cpu_user_time_microseconds counter
|
|
node_cpu_user_time_microseconds ${cpu.user}
|
|
|
|
# HELP node_cpu_system_time_microseconds CPU system time in microseconds.
|
|
# TYPE node_cpu_system_time_microseconds counter
|
|
node_cpu_system_time_microseconds ${cpu.system}
|
|
|
|
# HELP node_app_uptime_seconds Application uptime in seconds.
|
|
# TYPE node_app_uptime_seconds gauge
|
|
node_app_uptime_seconds ${uptime}
|
|
|
|
# HELP canino_db_connected Database connection status (1 = connected, 0 = disconnected).
|
|
# TYPE canino_db_connected gauge
|
|
canino_db_connected ${dbStatus}
|
|
|
|
# HELP canino_http_requests_total Total HTTP requests processed.
|
|
# TYPE canino_http_requests_total counter
|
|
canino_http_requests_total ${MetricsController.requestCount}
|
|
`;
|
|
|
|
res.set('Content-Type', 'text/plain; version=0.0.4; charset=utf-8');
|
|
res.end(responseText);
|
|
}
|
|
}
|