Compare commits

..

4 Commits

5 changed files with 29 additions and 19 deletions

View File

@ -4,18 +4,25 @@ import { PrometheusService } from './prometheus.service';
@Controller('metrics')
export class MetricsController {
constructor(private readonly prometheusService: PrometheusService) { }
//Получение конкретной метрики
@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);
}
// Получить список всех метрик
@Get('/all')
async getAllMetrics() {
return this.prometheusService.fetchAllMetrics();
}
// Получить ВСЕ метрики со значениями
@Get('/all-values')
async getAllMetricsWithValues() {
return this.prometheusService.fetchAllMetricsWithValues();

View File

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

View File

@ -8,13 +8,9 @@ import { PrometheusMetric } from './prometheus-metric.interface';
export class PrometheusService {
private readonly prometheusUrl: string;
constructor(
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);
}
constructor(private readonly httpService: HttpService) { }
//Получаем тип метрики
async fetchMetricType(metric: string): Promise<string | null> {
try {
@ -32,6 +28,8 @@ export class PrometheusService {
}
}
//Данные конкретной метрики, включая ее тип
async fetchMetrics(metric: string): Promise<PrometheusMetric[]> {
const response = await lastValueFrom(
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 => ({
...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', // Добавляем тип метрики
}));
}
//Получаем данные всех метрик
async fetchAllMetrics(): Promise<string[]> {
const response = await lastValueFrom(
this.httpService.get(`${this.prometheusUrl}/label/__name__/values`)
@ -56,6 +56,8 @@ export class PrometheusService {
return response.data.data;
}
// Получаем список всех метрик
async fetchAllMetricsWithValues(): Promise<any[]> {
const metricNames = await this.fetchAllMetrics();
const promises = metricNames.map(async (metric) => {