1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

fixing timeout issue

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-03-10 12:01:20 -05:00
parent 47fbf629ea
commit 0b98f59f59
2 changed files with 18 additions and 29 deletions

View File

@ -503,13 +503,22 @@ export class Cluster implements ClusterModel, ClusterState {
return url;
}
protected k8sRequest<T = any>(url: URL, options: OptionsOfJSONResponseBody = {}): Promise<T> {
protected async k8sRequest<T = any>(url: URL, options: OptionsOfJSONResponseBody = {}): Promise<T> {
options.timeout ??= 5000;
options.headers ??= {};
options.headers["content-type"] ??= "application/json";
options.headers.host ??= `${this.id}.${this.kubeProxyUrl.host}`; // required in ClusterManager.getClusterForRequest()
return got<T>(url, options).json();
try {
console.log(url, options);
const res = await got<T>(url, options);
return JSON.parse(String(res.body));
} catch (error) {
logger.error(`[REQUEST]: failed to get ${url}`, { error });
throw error;
}
}
getMetrics(prometheusPath: string, searchParams: Record<string, any>) {
@ -517,7 +526,7 @@ export class Cluster implements ClusterModel, ClusterState {
const metricsPath = `/api/v1/namespaces/${prometheusPath}/proxy${prometheusPrefix}/api/v1/query_range`;
return this.k8sRequest(this.getUrlTo(metricsPath), {
timeout: 0,
timeout: 10000,
method: "POST",
form: searchParams,
});

View File

@ -1,45 +1,23 @@
import _ from "lodash";
import { LensApiRequest } from "../router";
import { LensApi } from "../lens-api";
import { Cluster, ClusterMetadataKey } from "../cluster";
import { ClusterPrometheusMetadata } from "../../common/cluster-store";
import logger from "../logger";
import { PrometheusQueryKey } from "../prometheus/provider-registry";
import logger from "../logger";
export interface MetricsQuery {
[metricName: string]: string;
}
function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// This is used for backoff retry tracking.
const MAX_ATTEMPTS = 5;
const ATTEMPTS = [...(_.fill(Array(MAX_ATTEMPTS - 1), false)), true];
// prometheus metrics loader
async function loadMetrics(promQueries: string[], cluster: Cluster, prometheusPath: string, queryParams: Record<string, string>): Promise<any[]> {
const queries = promQueries.map(p => p.trim());
const loaders = new Map<string, Promise<any>>();
async function loadMetric(query: string): Promise<any> {
async function loadMetricHelper(): Promise<any> {
for (const [attempt, lastAttempt] of ATTEMPTS.entries()) { // retry
try {
return await cluster.getMetrics(prometheusPath, { query, ...queryParams });
} catch (error) {
if (lastAttempt || (error?.statusCode >= 400 && error?.statusCode < 500)) {
logger.error("[Metrics]: metrics not available", { error });
throw new Error("Metrics not available");
}
const searchParams = { query, ...queryParams };
await delay((attempt + 1) * 1000); // add delay before repeating request
}
}
}
return loaders.get(query) ?? loaders.set(query, loadMetricHelper()).get(query);
return loaders.get(query) ?? loaders.set(query, cluster.getMetrics(prometheusPath, { searchParams, retry: 5 })).get(query);
}
return Promise.all(queries.map(loadMetric));
@ -90,7 +68,9 @@ class MetricsRoute extends LensApi {
this.respondJson(response, data);
}
prometheusMetadata.success = true;
} catch {
} catch (error) {
logger.error("[METRICS]: failed to load metrics", { error });
prometheusMetadata.success = false;
this.respondJson(response, {});
} finally {