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:
parent
47fbf629ea
commit
0b98f59f59
@ -503,13 +503,22 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
return url;
|
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.timeout ??= 5000;
|
||||||
options.headers ??= {};
|
options.headers ??= {};
|
||||||
options.headers["content-type"] ??= "application/json";
|
options.headers["content-type"] ??= "application/json";
|
||||||
options.headers.host ??= `${this.id}.${this.kubeProxyUrl.host}`; // required in ClusterManager.getClusterForRequest()
|
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>) {
|
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`;
|
const metricsPath = `/api/v1/namespaces/${prometheusPath}/proxy${prometheusPrefix}/api/v1/query_range`;
|
||||||
|
|
||||||
return this.k8sRequest(this.getUrlTo(metricsPath), {
|
return this.k8sRequest(this.getUrlTo(metricsPath), {
|
||||||
timeout: 0,
|
timeout: 10000,
|
||||||
method: "POST",
|
method: "POST",
|
||||||
form: searchParams,
|
form: searchParams,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,45 +1,23 @@
|
|||||||
import _ from "lodash";
|
|
||||||
import { LensApiRequest } from "../router";
|
import { LensApiRequest } from "../router";
|
||||||
import { LensApi } from "../lens-api";
|
import { LensApi } from "../lens-api";
|
||||||
import { Cluster, ClusterMetadataKey } from "../cluster";
|
import { Cluster, ClusterMetadataKey } from "../cluster";
|
||||||
import { ClusterPrometheusMetadata } from "../../common/cluster-store";
|
import { ClusterPrometheusMetadata } from "../../common/cluster-store";
|
||||||
import logger from "../logger";
|
|
||||||
import { PrometheusQueryKey } from "../prometheus/provider-registry";
|
import { PrometheusQueryKey } from "../prometheus/provider-registry";
|
||||||
|
import logger from "../logger";
|
||||||
|
|
||||||
export interface MetricsQuery {
|
export interface MetricsQuery {
|
||||||
[metricName: string]: string;
|
[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
|
// prometheus metrics loader
|
||||||
async function loadMetrics(promQueries: string[], cluster: Cluster, prometheusPath: string, queryParams: Record<string, string>): Promise<any[]> {
|
async function loadMetrics(promQueries: string[], cluster: Cluster, prometheusPath: string, queryParams: Record<string, string>): Promise<any[]> {
|
||||||
const queries = promQueries.map(p => p.trim());
|
const queries = promQueries.map(p => p.trim());
|
||||||
const loaders = new Map<string, Promise<any>>();
|
const loaders = new Map<string, Promise<any>>();
|
||||||
|
|
||||||
async function loadMetric(query: string): Promise<any> {
|
async function loadMetric(query: string): Promise<any> {
|
||||||
async function loadMetricHelper(): Promise<any> {
|
const searchParams = { query, ...queryParams };
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
await delay((attempt + 1) * 1000); // add delay before repeating request
|
return loaders.get(query) ?? loaders.set(query, cluster.getMetrics(prometheusPath, { searchParams, retry: 5 })).get(query);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return loaders.get(query) ?? loaders.set(query, loadMetricHelper()).get(query);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all(queries.map(loadMetric));
|
return Promise.all(queries.map(loadMetric));
|
||||||
@ -90,7 +68,9 @@ class MetricsRoute extends LensApi {
|
|||||||
this.respondJson(response, data);
|
this.respondJson(response, data);
|
||||||
}
|
}
|
||||||
prometheusMetadata.success = true;
|
prometheusMetadata.success = true;
|
||||||
} catch {
|
} catch (error) {
|
||||||
|
logger.error("[METRICS]: failed to load metrics", { error });
|
||||||
|
|
||||||
prometheusMetadata.success = false;
|
prometheusMetadata.success = false;
|
||||||
this.respondJson(response, {});
|
this.respondJson(response, {});
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user