Метрики теперь забираются за интервал времени

pull/3/head
DmitriyA 2025-03-06 03:43:53 -05:00
parent 5ed31984f8
commit 085d48cdbf
2 changed files with 28 additions and 1 deletions

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

@ -16,6 +16,7 @@ export class PrometheusService {
console.log('Prometheus API URL:', this.prometheusUrl); console.log('Prometheus API URL:', this.prometheusUrl);
} }
// Получаем тип метрики
async fetchMetricType(metric: string): Promise<string | null> { async fetchMetricType(metric: string): Promise<string | null> {
try { try {
const response = await lastValueFrom( const response = await lastValueFrom(
@ -32,6 +33,24 @@ export class PrometheusService {
} }
} }
// Получаем описание метрики
async fetchMetricDescription(metric: string): Promise<string | undefined> {
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<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`, {
@ -40,15 +59,18 @@ 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 => ({ 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',
description: metricDescription, // Добавляем описание
})); }));
} }
// Получаем данные метрики за интервал
async fetchMetricsRange(metric: string, start: number, end: number, step: number): Promise<PrometheusMetric[]> { async fetchMetricsRange(metric: string, start: number, end: number, step: number): Promise<PrometheusMetric[]> {
const response = await lastValueFrom( const response = await lastValueFrom(
this.httpService.get(`${this.prometheusUrl}/query_range`, { this.httpService.get(`${this.prometheusUrl}/query_range`, {
@ -62,6 +84,7 @@ 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.flatMap((entry) => return response.data.data.result.flatMap((entry) =>
entry.values.map((value): PrometheusMetric => ({ entry.values.map((value): PrometheusMetric => ({
@ -69,10 +92,12 @@ export class PrometheusService {
timestamp: value[0] * 1000, timestamp: value[0] * 1000,
value: parseFloat(value[1]), value: parseFloat(value[1]),
type: metricType || 'unknown', type: metricType || 'unknown',
description: metricDescription, // Добавляем описание
})) }))
); );
} }
// Получаем список всех метрик
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`)
@ -80,6 +105,7 @@ 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) => {