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:
parent
c1becf6953
commit
e98ef3673a
@ -2,11 +2,9 @@ import { PrometheusLens } from "../main/prometheus/lens";
|
|||||||
import { PrometheusHelm } from "../main/prometheus/helm";
|
import { PrometheusHelm } from "../main/prometheus/helm";
|
||||||
import { PrometheusOperator } from "../main/prometheus/operator";
|
import { PrometheusOperator } from "../main/prometheus/operator";
|
||||||
import { PrometheusProviderRegistry } from "../main/prometheus/provider-registry";
|
import { PrometheusProviderRegistry } from "../main/prometheus/provider-registry";
|
||||||
import logger from "../main/logger";
|
|
||||||
|
|
||||||
[PrometheusLens, PrometheusHelm, PrometheusOperator].forEach(providerClass => {
|
[PrometheusLens, PrometheusHelm, PrometheusOperator].forEach(providerClass => {
|
||||||
const provider = new providerClass()
|
const provider = new providerClass()
|
||||||
logger.info(provider.id)
|
|
||||||
PrometheusProviderRegistry.registerProvider(provider.id, provider)
|
PrometheusProviderRegistry.registerProvider(provider.id, provider)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -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 {
|
export class PrometheusLens implements PrometheusProvider {
|
||||||
id = "lens"
|
id = "lens"
|
||||||
name = "Lens"
|
name = "Lens"
|
||||||
rateAccuracy = "1m"
|
rateAccuracy = "1m"
|
||||||
|
|
||||||
public getQueries(opts: PrometheusQueryOpts): PrometheusQuery {
|
public getQueries(opts: PrometheusQueryOpts): PrometheusNodeQuery | PrometheusClusterQuery | PrometheusPodQuery | PrometheusPvcQuery | PrometheusIngressQuery {
|
||||||
switch(opts.category) {
|
switch(opts.category) {
|
||||||
case 'cluster':
|
case 'cluster':
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -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 {
|
export class PrometheusOperator implements PrometheusProvider {
|
||||||
rateAccuracy = "1m"
|
rateAccuracy = "1m"
|
||||||
id = "operator"
|
id = "operator"
|
||||||
name = "Prometheus Operator"
|
name = "Prometheus Operator"
|
||||||
|
|
||||||
public getQueries(opts: PrometheusQueryOpts): PrometheusQuery {
|
public getQueries(opts: PrometheusQueryOpts): PrometheusNodeQuery | PrometheusClusterQuery | PrometheusPodQuery | PrometheusPvcQuery | PrometheusIngressQuery {
|
||||||
switch(opts.category) {
|
switch(opts.category) {
|
||||||
case 'cluster':
|
case 'cluster':
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -1,5 +1,47 @@
|
|||||||
export type PrometheusQuery = {
|
export type PrometheusClusterQuery = {
|
||||||
[key: string]: string;
|
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 = {
|
export type PrometheusQueryOpts = {
|
||||||
@ -7,7 +49,7 @@ export type PrometheusQueryOpts = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export interface PrometheusProvider {
|
export interface PrometheusProvider {
|
||||||
getQueries(opts: PrometheusQueryOpts): PrometheusQuery;
|
getQueries(opts: PrometheusQueryOpts): PrometheusNodeQuery | PrometheusClusterQuery | PrometheusPodQuery | PrometheusPvcQuery | PrometheusIngressQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PrometheusProviderList = {
|
export type PrometheusProviderList = {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { LensApiRequest } from "../router"
|
import { LensApiRequest } from "../router"
|
||||||
import { LensApi } from "../lens-api"
|
import { LensApi } from "../lens-api"
|
||||||
import * as requestPromise from "request-promise-native"
|
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[] | {
|
type MetricsQuery = string | string[] | {
|
||||||
[metricName: string]: string;
|
[metricName: string]: string;
|
||||||
@ -71,9 +71,10 @@ class MetricsRoute extends LensApi {
|
|||||||
data = {};
|
data = {};
|
||||||
const result = await Promise.all(
|
const result = await Promise.all(
|
||||||
Object.entries(query).map((queryEntry: any) => {
|
Object.entries(query).map((queryEntry: any) => {
|
||||||
const queryName = queryEntry[0]
|
const queryName: string = queryEntry[0]
|
||||||
const queryOpts = queryEntry[1]
|
const queryOpts: PrometheusQueryOpts = queryEntry[1]
|
||||||
const q = prometheusProvider.getQueries(queryOpts)[queryName]
|
const queries = prometheusProvider.getQueries(queryOpts)
|
||||||
|
const q = queries[queryName as keyof (PrometheusNodeQuery | PrometheusClusterQuery | PrometheusPodQuery | PrometheusPvcQuery | PrometheusIngressQuery)]
|
||||||
return loadMetrics(q)
|
return loadMetrics(q)
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user