Compare commits

...

4 Commits

5 changed files with 29 additions and 19 deletions

View File

@ -20,7 +20,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json" "test:e2e": "jest --config ./test/jest-e2e.json"
}, },
"dependencies": { "dependencies": {
"@nestjs/axios": "^4.0.0", "@nestjs/axios": "^4.0.0",
"@nestjs/common": "^11.0.1", "@nestjs/common": "^11.0.1",
"@nestjs/core": "^11.0.1", "@nestjs/core": "^11.0.1",
"@nestjs/config": "^4.0.0", "@nestjs/config": "^4.0.0",

View File

@ -12,4 +12,4 @@ async function bootstrap() {
}); });
await app.listen(process.env.PORT ?? 3000); await app.listen(process.env.PORT ?? 3000);
} }
bootstrap(); bootstrap();

View File

@ -4,20 +4,27 @@ import { PrometheusService } from './prometheus.service';
@Controller('metrics') @Controller('metrics')
export class MetricsController { export class MetricsController {
constructor(private readonly prometheusService: PrometheusService) { } constructor(private readonly prometheusService: PrometheusService) { }
//Получение конкретной метрики
@Get() @Get()
async getMetrics(@Query('metric') metric: string) { async getMetrics(
@Query('metric') metric: string,
@Query('start') start: number,
@Query('end') end: number,
@Query('step') step: number,
) {
if (start && end && step) {
return this.prometheusService.fetchMetricsRange(metric, start, end, step);
}
return this.prometheusService.fetchMetrics(metric); return this.prometheusService.fetchMetrics(metric);
} }
// Получить список всех метрик
@Get('/all') @Get('/all')
async getAllMetrics() { async getAllMetrics() {
return this.prometheusService.fetchAllMetrics(); return this.prometheusService.fetchAllMetrics();
} }
// Получить ВСЕ метрики со значениями
@Get('/all-values') @Get('/all-values')
async getAllMetricsWithValues() { async getAllMetricsWithValues() {
return this.prometheusService.fetchAllMetricsWithValues(); return this.prometheusService.fetchAllMetricsWithValues();
} }
} }

View File

@ -4,4 +4,5 @@ export interface PrometheusMetric {
timestamp: number; timestamp: number;
value: number; value: number;
type: string; // Тип метрики ("gauge", "counter", и т. д.) type: string; // Тип метрики ("gauge", "counter", и т. д.)
} description?: string; // Описание метрики
}

View File

@ -8,13 +8,9 @@ import { PrometheusMetric } from './prometheus-metric.interface';
export class PrometheusService { export class PrometheusService {
private readonly prometheusUrl: string; private readonly prometheusUrl: string;
constructor( constructor(private readonly httpService: HttpService) { }
private readonly httpService: HttpService,
private readonly configService: ConfigService //Получаем тип метрики
) {
this.prometheusUrl = this.configService.get<string>('PROMETHEUS_API', 'http://localhost:9090');
console.log('Prometheus API URL:', this.prometheusUrl);
}
async fetchMetricType(metric: string): Promise<string | null> { async fetchMetricType(metric: string): Promise<string | null> {
try { try {
@ -32,6 +28,8 @@ export class PrometheusService {
} }
} }
//Данные конкретной метрики, включая ее тип
async fetchMetrics(metric: string): Promise<PrometheusMetric[]> { async fetchMetrics(metric: string): Promise<PrometheusMetric[]> {
const response = await lastValueFrom( const response = await lastValueFrom(
this.httpService.get(`${this.prometheusUrl}/query`, { this.httpService.get(`${this.prometheusUrl}/query`, {
@ -39,16 +37,18 @@ export class PrometheusService {
}) })
); );
const metricType = await this.fetchMetricType(metric); const metricType = await this.fetchMetricType(metric); // Получаем тип
return response.data.data.result.map((entry): PrometheusMetric => ({ return response.data.data.result.map((entry): PrometheusMetric => ({
...entry.metric, ...entry.metric,
timestamp: entry.value[0] * 1000, timestamp: entry.value[0] * 1000, // Преобразуем в миллисекунды
value: parseFloat(entry.value[1]), value: parseFloat(entry.value[1]), // Преобразуем в число
type: metricType || 'unknown', type: metricType || 'unknown', // Добавляем тип метрики
})); }));
} }
//Получаем данные всех метрик
async fetchAllMetrics(): Promise<string[]> { async fetchAllMetrics(): Promise<string[]> {
const response = await lastValueFrom( const response = await lastValueFrom(
this.httpService.get(`${this.prometheusUrl}/label/__name__/values`) this.httpService.get(`${this.prometheusUrl}/label/__name__/values`)
@ -56,6 +56,8 @@ export class PrometheusService {
return response.data.data; return response.data.data;
} }
// Получаем список всех метрик
async fetchAllMetricsWithValues(): Promise<any[]> { async fetchAllMetricsWithValues(): Promise<any[]> {
const metricNames = await this.fetchAllMetrics(); const metricNames = await this.fetchAllMetrics();
const promises = metricNames.map(async (metric) => { const promises = metricNames.map(async (metric) => {