Compare commits

..

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

5 changed files with 19 additions and 29 deletions

View File

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

View File

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

View File

@ -8,9 +8,13 @@ import { PrometheusMetric } from './prometheus-metric.interface';
export class PrometheusService { export class PrometheusService {
private readonly prometheusUrl: string; 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> { async fetchMetricType(metric: string): Promise<string | null> {
try { try {
@ -28,8 +32,6 @@ export class PrometheusService {
} }
} }
//Данные конкретной метрики, включая ее тип
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`, {
@ -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 => ({ 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',
})); }));
} }
//Получаем данные всех метрик
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`)
@ -56,8 +56,6 @@ 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) => {