debian-mirror-gitlab/app/assets/javascripts/monitoring/requests/index.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-10-24 23:57:45 +05:30
import axios from '~/lib/utils/axios_utils';
import { backOff } from '~/lib/utils/common_utils';
2023-03-17 16:20:25 +05:30
import {
HTTP_STATUS_BAD_REQUEST,
2023-03-04 22:38:38 +05:30
HTTP_STATUS_NO_CONTENT,
2023-03-17 16:20:25 +05:30
HTTP_STATUS_SERVICE_UNAVAILABLE,
2023-03-04 22:38:38 +05:30
HTTP_STATUS_UNPROCESSABLE_ENTITY,
} from '~/lib/utils/http_status';
2020-10-24 23:57:45 +05:30
import { PROMETHEUS_TIMEOUT } from '../constants';
2021-03-08 18:12:59 +05:30
const cancellableBackOffRequest = (makeRequestCallback) =>
2020-10-24 23:57:45 +05:30
backOff((next, stop) => {
makeRequestCallback()
2021-03-08 18:12:59 +05:30
.then((resp) => {
2023-03-04 22:38:38 +05:30
if (resp.status === HTTP_STATUS_NO_CONTENT) {
2020-10-24 23:57:45 +05:30
next();
} else {
stop(resp);
}
})
// If the request is cancelled by axios
// then consider it as noop so that its not
// caught by subsequent catches
2021-03-08 18:12:59 +05:30
.catch((thrown) => (axios.isCancel(thrown) ? undefined : stop(thrown)));
2020-10-24 23:57:45 +05:30
}, PROMETHEUS_TIMEOUT);
export const getDashboard = (dashboardEndpoint, params) =>
cancellableBackOffRequest(() => axios.get(dashboardEndpoint, { params })).then(
2021-03-08 18:12:59 +05:30
(axiosResponse) => axiosResponse.data,
2020-10-24 23:57:45 +05:30
);
export const getPrometheusQueryData = (prometheusEndpoint, params, opts) =>
cancellableBackOffRequest(() => axios.get(prometheusEndpoint, { params, ...opts }))
2021-03-08 18:12:59 +05:30
.then((axiosResponse) => axiosResponse.data)
.then((prometheusResponse) => prometheusResponse.data)
.catch((error) => {
2020-10-24 23:57:45 +05:30
// Prometheus returns errors in specific cases
// https://prometheus.io/docs/prometheus/latest/querying/api/#format-overview
const { response = {} } = error;
if (
2023-03-17 16:20:25 +05:30
response.status === HTTP_STATUS_BAD_REQUEST ||
2023-03-04 22:38:38 +05:30
response.status === HTTP_STATUS_UNPROCESSABLE_ENTITY ||
2023-03-17 16:20:25 +05:30
response.status === HTTP_STATUS_SERVICE_UNAVAILABLE
2020-10-24 23:57:45 +05:30
) {
const { data } = response;
if (data?.status === 'error' && data?.error) {
throw new Error(data.error);
}
}
throw error;
});