diff --git a/src/prometheus.service.ts b/src/prometheus.service.ts index 407cdb0..333292c 100644 --- a/src/prometheus.service.ts +++ b/src/prometheus.service.ts @@ -8,10 +8,15 @@ import { PrometheusMetric } from './prometheus-metric.interface'; export class PrometheusService { private readonly prometheusUrl: string; - constructor(private readonly httpService: HttpService) { } - - //Получаем тип метрики + constructor( + private readonly httpService: HttpService, + private readonly configService: ConfigService + ) { + this.prometheusUrl = this.configService.get('PROMETHEUS_API', 'http://localhost:9090'); + console.log('Prometheus API URL:', this.prometheusUrl); + } + // Получаем тип метрики async fetchMetricType(metric: string): Promise { try { const response = await lastValueFrom( @@ -28,8 +33,24 @@ export class PrometheusService { } } - //Данные конкретной метрики, включая ее тип + // Получаем описание метрики + async fetchMetricDescription(metric: string): Promise { + try { + const response = await lastValueFrom( + this.httpService.get(`${this.prometheusUrl}/metadata`, { + params: { metric }, + }) + ); + const metadata = response.data.data[metric]; + return metadata?.length ? metadata[0].help : undefined; + } catch (error) { + console.error(`Ошибка при получении описания метрики ${metric}:`, error); + return undefined; + } + } + + // Получаем данные метрики (текущие значения) async fetchMetrics(metric: string): Promise { const response = await lastValueFrom( this.httpService.get(`${this.prometheusUrl}/query`, { @@ -37,18 +58,46 @@ export class PrometheusService { }) ); - const metricType = await this.fetchMetricType(metric); // Получаем тип + const metricType = await this.fetchMetricType(metric); + const metricDescription = await this.fetchMetricDescription(metric); return response.data.data.result.map((entry): PrometheusMetric => ({ ...entry.metric, - timestamp: entry.value[0] * 1000, // Преобразуем в миллисекунды - value: parseFloat(entry.value[1]), // Преобразуем в число - type: metricType || 'unknown', // Добавляем тип метрики + timestamp: entry.value[0] * 1000, + value: parseFloat(entry.value[1]), + type: metricType || 'unknown', + description: metricDescription, // Добавляем описание })); } - //Получаем данные всех метрик + // Получаем данные метрики за интервал + async fetchMetricsRange(metric: string, start: number, end: number, step: number): Promise { + const response = await lastValueFrom( + this.httpService.get(`${this.prometheusUrl}/query_range`, { + params: { + query: metric, + start, + end, + step, + }, + }) + ); + const metricType = await this.fetchMetricType(metric); + const metricDescription = await this.fetchMetricDescription(metric); + + return response.data.data.result.flatMap((entry) => + entry.values.map((value): PrometheusMetric => ({ + ...entry.metric, + timestamp: value[0] * 1000, + value: parseFloat(value[1]), + type: metricType || 'unknown', + description: metricDescription, // Добавляем описание + })) + ); + } + + // Получаем список всех метрик async fetchAllMetrics(): Promise { const response = await lastValueFrom( this.httpService.get(`${this.prometheusUrl}/label/__name__/values`) @@ -56,8 +105,7 @@ export class PrometheusService { return response.data.data; } - // Получаем список всех метрик - + // Получаем все метрики с их значениями async fetchAllMetricsWithValues(): Promise { const metricNames = await this.fetchAllMetrics(); const promises = metricNames.map(async (metric) => { @@ -66,4 +114,4 @@ export class PrometheusService { }); return Promise.all(promises); } -} +} \ No newline at end of file