Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prometheus: Handle Non JSON errors in response #32

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion prometheus/src/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export function Chart(props: ChartProps) {
step: 2,
});
} catch (e) {
setError(e);
fetchedMetrics[plot.name] = { data: [], state: ChartState.ERROR };
setError(e.message);
setState(ChartState.ERROR);
break;
}
Expand Down
24 changes: 21 additions & 3 deletions prometheus/src/request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@ export async function isPrometheusInstalled() {
return [false, null, null];
}

export function fetchMetrics(data: {
/**
* Fetches metrics data from Prometheus using the provided parameters.
* @param {object} data - The parameters for fetching metrics.
* @param {string} data.prefix - The namespace prefix.
* @param {string} data.query - The Prometheus query string.
* @param {number} data.from - The start time for the query (Unix timestamp).
* @param {number} data.to - The end time for the query (Unix timestamp).
* @param {number} data.step - The step size for the query (in seconds).
* @returns {Promise<object>} - A promise that resolves to the fetched metrics data.
* @throws {Error} - Throws an error if the request fails.
*/
export async function fetchMetrics(data: {
yolossn marked this conversation as resolved.
Show resolved Hide resolved
prefix: string;
query: string;
from: number;
to: number;
step: number;
}) {
}): Promise<object> {
const params = new URLSearchParams();
if (data.from) {
params.append('start', data.from.toString());
Expand All @@ -37,10 +48,17 @@ export function fetchMetrics(data: {
params.append('query', data.query);
}

return request(
const response = await request(
`/api/v1/namespaces/${data.prefix}/proxy/api/v1/query_range?${params.toString()}`,
{
method: 'GET',
isJSON: false,
}
);
if (response.status === 200) {
return response.json();
} else {
const error = new Error(response.statusText);
yolossn marked this conversation as resolved.
Show resolved Hide resolved
return Promise.reject(error);
}
}
Loading