From 085d48cdbf268e42f940143d6fb9ec0c7fc40c12 Mon Sep 17 00:00:00 2001 From: DmitriyA Date: Thu, 6 Mar 2025 03:43:53 -0500 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B5=D1=82=D1=80=D0=B8=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B7=D0=B0=D0=B1=D0=B8?= =?UTF-8?q?=D1=80=D0=B0=D1=8E=D1=82=D1=81=D1=8F=20=D0=B7=D0=B0=20=D0=B8?= =?UTF-8?q?=D0=BD=D1=82=D0=B5=D1=80=D0=B2=D0=B0=D0=BB=20=D0=B2=D1=80=D0=B5?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/prometheus-metric.interface.ts | 3 ++- src/prometheus.service.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/prometheus-metric.interface.ts b/src/prometheus-metric.interface.ts index 646cbab..2f30262 100644 --- a/src/prometheus-metric.interface.ts +++ b/src/prometheus-metric.interface.ts @@ -4,4 +4,5 @@ export interface PrometheusMetric { timestamp: number; value: number; type: string; // Тип метрики ("gauge", "counter", и т. д.) -} + description?: string; // Описание метрики +} \ No newline at end of file diff --git a/src/prometheus.service.ts b/src/prometheus.service.ts index b829278..333292c 100644 --- a/src/prometheus.service.ts +++ b/src/prometheus.service.ts @@ -16,6 +16,7 @@ export class PrometheusService { console.log('Prometheus API URL:', this.prometheusUrl); } + // Получаем тип метрики async fetchMetricType(metric: string): Promise { try { const response = await lastValueFrom( @@ -32,6 +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`, { @@ -40,15 +59,18 @@ export class PrometheusService { ); 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', + description: metricDescription, // Добавляем описание })); } + // Получаем данные метрики за интервал async fetchMetricsRange(metric: string, start: number, end: number, step: number): Promise { const response = await lastValueFrom( this.httpService.get(`${this.prometheusUrl}/query_range`, { @@ -62,6 +84,7 @@ export class PrometheusService { ); const metricType = await this.fetchMetricType(metric); + const metricDescription = await this.fetchMetricDescription(metric); return response.data.data.result.flatMap((entry) => entry.values.map((value): PrometheusMetric => ({ @@ -69,10 +92,12 @@ export class PrometheusService { 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`) @@ -80,6 +105,7 @@ export class PrometheusService { return response.data.data; } + // Получаем все метрики с их значениями async fetchAllMetricsWithValues(): Promise { const metricNames = await this.fetchAllMetrics(); const promises = metricNames.map(async (metric) => {