backend-trust-module/src/prometheus/prometheus.service.ts

33 lines
950 B
TypeScript

import { Injectable, InternalServerErrorException } from '@nestjs/common';
import axios from 'axios';
import {
PrometheusQueryResult,
PrometheusApiResponse,
} from './interface/prometheus.interface';
@Injectable()
export class PrometheusService {
private readonly prometheusUrl = 'http://prometheus:9090/api/v1/query';
async getMetric(metric: string): Promise<PrometheusQueryResult> {
try {
const response = await axios.get<
PrometheusApiResponse<PrometheusQueryResult>
>(this.prometheusUrl, {
params: { query: metric },
});
return response.data.data;
} catch (error: unknown) {
if (error instanceof Error) {
throw new InternalServerErrorException(
`Ошибка запроса: ${error.message}`,
);
}
throw new InternalServerErrorException(
'Неизвестная ошибка при запросе к Prometheus',
);
}
}
}