Compare commits
4 Commits
76fa893146
...
0afbcd7467
| Author | SHA1 | Date |
|---|---|---|
|
|
0afbcd7467 | |
|
|
085d48cdbf | |
|
|
5ed31984f8 | |
|
|
f4feb6349f |
|
|
@ -4,18 +4,25 @@ 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(@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);
|
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();
|
||||||
|
|
|
||||||
|
|
@ -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; // Описание метрики
|
||||||
}
|
}
|
||||||
|
|
@ -8,13 +8,9 @@ import { PrometheusMetric } from './prometheus-metric.interface';
|
||||||
export class PrometheusService {
|
export class PrometheusService {
|
||||||
private readonly prometheusUrl: string;
|
private readonly prometheusUrl: string;
|
||||||
|
|
||||||
constructor(
|
constructor(private readonly httpService: HttpService) { }
|
||||||
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 {
|
||||||
|
|
@ -32,6 +28,8 @@ 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`, {
|
||||||
|
|
@ -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 => ({
|
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,6 +56,8 @@ 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) => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue