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

Make PrometheusQuery more type safe

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
Lauri Nevala 2020-04-30 19:24:01 +03:00
parent c1becf6953
commit e98ef3673a
5 changed files with 54 additions and 13 deletions

View File

@ -2,11 +2,9 @@ import { PrometheusLens } from "../main/prometheus/lens";
import { PrometheusHelm } from "../main/prometheus/helm";
import { PrometheusOperator } from "../main/prometheus/operator";
import { PrometheusProviderRegistry } from "../main/prometheus/provider-registry";
import logger from "../main/logger";
[PrometheusLens, PrometheusHelm, PrometheusOperator].forEach(providerClass => {
const provider = new providerClass()
logger.info(provider.id)
PrometheusProviderRegistry.registerProvider(provider.id, provider)
});

View File

@ -1,11 +1,11 @@
import { PrometheusProvider, PrometheusQuery, PrometheusQueryOpts } from "./provider-registry";
import { PrometheusProvider, PrometheusQueryOpts, PrometheusClusterQuery, PrometheusNodeQuery, PrometheusPodQuery, PrometheusPvcQuery, PrometheusIngressQuery } from "./provider-registry";
export class PrometheusLens implements PrometheusProvider {
id = "lens"
name = "Lens"
rateAccuracy = "1m"
public getQueries(opts: PrometheusQueryOpts): PrometheusQuery {
public getQueries(opts: PrometheusQueryOpts): PrometheusNodeQuery | PrometheusClusterQuery | PrometheusPodQuery | PrometheusPvcQuery | PrometheusIngressQuery {
switch(opts.category) {
case 'cluster':
return {

View File

@ -1,11 +1,11 @@
import { PrometheusProvider, PrometheusQuery, PrometheusQueryOpts } from "./provider-registry";
import { PrometheusProvider, PrometheusQueryOpts, PrometheusClusterQuery, PrometheusNodeQuery, PrometheusPodQuery, PrometheusPvcQuery, PrometheusIngressQuery } from "./provider-registry";
export class PrometheusOperator implements PrometheusProvider {
rateAccuracy = "1m"
id = "operator"
name = "Prometheus Operator"
public getQueries(opts: PrometheusQueryOpts): PrometheusQuery {
public getQueries(opts: PrometheusQueryOpts): PrometheusNodeQuery | PrometheusClusterQuery | PrometheusPodQuery | PrometheusPvcQuery | PrometheusIngressQuery {
switch(opts.category) {
case 'cluster':
return {

View File

@ -1,5 +1,47 @@
export type PrometheusQuery = {
[key: string]: string;
export type PrometheusClusterQuery = {
memoryUsage: string;
memoryRequests: string;
memoryLimits: string;
memoryCapacity: string;
cpuUsage: string;
cpuRequests: string;
cpuLimits: string;
cpuCapacity: string;
podUsage: string;
podCapacity: string;
}
export type PrometheusNodeQuery = {
memoryUsage: string;
memoryCapacity: string;
cpuUsage: string;
cpuCapacity: string;
fsSize: string;
fsUsage: string;
}
export type PrometheusPodQuery = {
memoryUsage: string;
memoryRequests: string;
memoryLimits: string;
cpuUsage: string;
cpuRequests: string;
cpuLimits: string;
fsUsage: string;
networkReceive: string;
networkTransit: string;
}
export type PrometheusPvcQuery = {
diskUsage: string;
diskCapacity: string;
}
export type PrometheusIngressQuery = {
bytesSentSuccess: string;
bytesSentFailure: string;
requestDurationSeconds: string;
responseDurationSeconds: string;
}
export type PrometheusQueryOpts = {
@ -7,7 +49,7 @@ export type PrometheusQueryOpts = {
};
export interface PrometheusProvider {
getQueries(opts: PrometheusQueryOpts): PrometheusQuery;
getQueries(opts: PrometheusQueryOpts): PrometheusNodeQuery | PrometheusClusterQuery | PrometheusPodQuery | PrometheusPvcQuery | PrometheusIngressQuery;
}
export type PrometheusProviderList = {

View File

@ -1,7 +1,7 @@
import { LensApiRequest } from "../router"
import { LensApi } from "../lens-api"
import * as requestPromise from "request-promise-native"
import { PrometheusProviderRegistry, PrometheusProvider} from "../prometheus/provider-registry"
import { PrometheusProviderRegistry, PrometheusProvider, PrometheusNodeQuery, PrometheusClusterQuery, PrometheusPodQuery, PrometheusPvcQuery, PrometheusIngressQuery, PrometheusQueryOpts} from "../prometheus/provider-registry"
type MetricsQuery = string | string[] | {
[metricName: string]: string;
@ -71,9 +71,10 @@ class MetricsRoute extends LensApi {
data = {};
const result = await Promise.all(
Object.entries(query).map((queryEntry: any) => {
const queryName = queryEntry[0]
const queryOpts = queryEntry[1]
const q = prometheusProvider.getQueries(queryOpts)[queryName]
const queryName: string = queryEntry[0]
const queryOpts: PrometheusQueryOpts = queryEntry[1]
const queries = prometheusProvider.getQueries(queryOpts)
const q = queries[queryName as keyof (PrometheusNodeQuery | PrometheusClusterQuery | PrometheusPodQuery | PrometheusPvcQuery | PrometheusIngressQuery)]
return loadMetrics(q)
})
);