Compare commits

..

No commits in common. "0afbcd7467ac2a7f06d73ea3c82e26f313e17d19" and "76fa8931464878273bb8eae4ec9ca50d9fc31e40" have entirely different histories.

5 changed files with 19 additions and 29 deletions

View File

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

View File

@ -4,25 +4,18 @@ import { PrometheusService } from './prometheus.service';
@Controller('metrics')
export class MetricsController {
constructor(private readonly prometheusService: PrometheusService) { }
//Получение конкретной метрики
@Get()
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);
}
async getMetrics(@Query('metric') metric: string) {
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,5 +4,4 @@ export interface PrometheusMetric {
timestamp: number;
value: number;
type: string; // Тип метрики ("gauge", "counter", и т. д.)
description?: string; // Описание метрики
}

View File

@ -8,9 +8,13 @@ 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<string>('PROMETHEUS_API', 'http://localhost:9090');
console.log('Prometheus API URL:', this.prometheusUrl);
}
async fetchMetricType(metric: string): Promise<string | null> {
try {
@ -28,8 +32,6 @@ export class PrometheusService {
}
}
//Данные конкретной метрики, включая ее тип
async fetchMetrics(metric: string): Promise<PrometheusMetric[]> {
const response = await lastValueFrom(
this.httpService.get(`${this.prometheusUrl}/query`, {
@ -37,18 +39,16 @@ 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,8 +56,6 @@ export class PrometheusService {
return response.data.data;
}
// Получаем список всех метрик
async fetchAllMetricsWithValues(): Promise<any[]> {
const metricNames = await this.fetchAllMetrics();
const promises = metricNames.map(async (metric) => {