mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Cleanup Prometheus detection (#4316)
This commit is contained in:
1 parent
8081d35f9c
commit
5d172b89db
8 files changed
+73
-85
No files matched your search
@@ -29,7 +29,7 @@ export class PrometheusHelm extends PrometheusLens {
|
||||
readonly rateAccuracy: string = "5m";
|
||||
readonly isConfigurable: boolean = true;
|
||||
|
||||
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService | undefined> {
|
||||
return this.getFirstNamespacedServer(client, "app=prometheus,component=server,heritage=Helm");
|
||||
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService> {
|
||||
return this.getFirstNamespacedService(client, "app=prometheus,component=server,heritage=Helm");
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
import { PrometheusProvider, PrometheusService } from "./provider-registry";
|
||||
import type { CoreV1Api } from "@kubernetes/client-node";
|
||||
import logger from "../logger";
|
||||
import { inspect } from "util";
|
||||
|
||||
export class PrometheusLens extends PrometheusProvider {
|
||||
@@ -30,22 +29,8 @@ export class PrometheusLens extends PrometheusProvider {
|
||||
readonly rateAccuracy: string = "1m";
|
||||
readonly isConfigurable: boolean = false;
|
||||
|
||||
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService | undefined> {
|
||||
try {
|
||||
const resp = await client.readNamespacedService("prometheus", "lens-metrics");
|
||||
const service = resp.body;
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
namespace: service.metadata.namespace,
|
||||
service: service.metadata.name,
|
||||
port: service.spec.ports[0].port,
|
||||
};
|
||||
} catch(error) {
|
||||
logger.warn(`PrometheusLens: failed to list services: ${error.response.body.message}`);
|
||||
|
||||
return undefined;
|
||||
}
|
||||
public getPrometheusService(client: CoreV1Api): Promise<PrometheusService> {
|
||||
return this.getNamespacedService(client, "prometheus", "lens-metrics");
|
||||
}
|
||||
|
||||
public getQuery(opts: Record<string, string>, queryName: string): string {
|
||||
|
||||
@@ -29,8 +29,8 @@ export class PrometheusOperator extends PrometheusProvider {
|
||||
readonly name: string = "Prometheus Operator";
|
||||
readonly isConfigurable: boolean = true;
|
||||
|
||||
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService | undefined> {
|
||||
return this.getFirstNamespacedServer(client, "operated-prometheus=true", "self-monitor=true");
|
||||
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService> {
|
||||
return this.getFirstNamespacedService(client, "operated-prometheus=true", "self-monitor=true");
|
||||
}
|
||||
|
||||
public getQuery(opts: Record<string, string>, queryName: string): string {
|
||||
|
||||
@@ -20,8 +20,8 @@
|
||||
*/
|
||||
|
||||
import type { CoreV1Api } from "@kubernetes/client-node";
|
||||
import { inspect } from "util";
|
||||
import { Singleton } from "../../common/utils";
|
||||
import logger from "../logger";
|
||||
|
||||
export type PrometheusService = {
|
||||
id: string;
|
||||
@@ -43,7 +43,7 @@ export abstract class PrometheusProvider {
|
||||
return `sum(rate(nginx_ingress_controller_bytes_sent_sum{ingress="${ingress}",namespace="${namespace}",status=~"${statuses}"}[${this.rateAccuracy}])) by (ingress, namespace)`;
|
||||
}
|
||||
|
||||
protected async getFirstNamespacedServer(client: CoreV1Api, ...selectors: string[]): Promise<PrometheusService | undefined> {
|
||||
protected async getFirstNamespacedService(client: CoreV1Api, ...selectors: string[]): Promise<PrometheusService> {
|
||||
try {
|
||||
for (const selector of selectors) {
|
||||
const { body: { items: [service] }} = await client.listServiceForAllNamespaces(null, null, null, selector);
|
||||
@@ -58,10 +58,26 @@ export abstract class PrometheusProvider {
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn(`${this.name}: failed to list services: ${error.toString()}`);
|
||||
throw new Error(`Failed to list services for Prometheus${this.name} in all namespaces: ${error.response.body.message}`);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
throw new Error(`No service found for Prometheus${this.name} from any namespace`);
|
||||
}
|
||||
|
||||
protected async getNamespacedService(client: CoreV1Api, name: string, namespace: string): Promise<PrometheusService> {
|
||||
try {
|
||||
const resp = await client.readNamespacedService(name, namespace);
|
||||
const service = resp.body;
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
namespace: service.metadata.namespace,
|
||||
service: service.metadata.name,
|
||||
port: service.spec.ports[0].port,
|
||||
};
|
||||
} catch(error) {
|
||||
throw new Error(`Failed to list services for Prometheus${this.name} in namespace=${inspect(namespace, false, undefined, false)}: ${error.response.body.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
import { PrometheusProvider, PrometheusService } from "./provider-registry";
|
||||
import type { CoreV1Api } from "@kubernetes/client-node";
|
||||
import logger from "../logger";
|
||||
import { inspect } from "util";
|
||||
|
||||
export class PrometheusStacklight extends PrometheusProvider {
|
||||
@@ -30,22 +29,8 @@ export class PrometheusStacklight extends PrometheusProvider {
|
||||
readonly rateAccuracy: string = "1m";
|
||||
readonly isConfigurable: boolean = true;
|
||||
|
||||
public async getPrometheusService(client: CoreV1Api): Promise<PrometheusService | undefined> {
|
||||
try {
|
||||
const resp = await client.readNamespacedService("prometheus-server", "stacklight");
|
||||
const service = resp.body;
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
namespace: service.metadata.namespace,
|
||||
service: service.metadata.name,
|
||||
port: service.spec.ports[0].port,
|
||||
};
|
||||
} catch(error) {
|
||||
logger.warn(`PrometheusLens: failed to list services: ${error.response.body.message}`);
|
||||
|
||||
return undefined;
|
||||
}
|
||||
public getPrometheusService(client: CoreV1Api): Promise<PrometheusService> {
|
||||
return this.getNamespacedService(client, "prometheus-server", "stacklight");
|
||||
}
|
||||
|
||||
public getQuery(opts: Record<string, string>, queryName: string): string {
|
||||
|
||||
Reference in new issue
Block a user