Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting for filtering empty containers on Prometheus queries #7777

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/core/src/common/cluster-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export interface ClusterPreferences extends ClusterPrometheusPreferences {
defaultNamespace?: string;
}

interface QueryFilterOptions {
hideEmptyContainers: boolean;
}

/**
* A cluster's prometheus settings (a subset of cluster settings)
*/
Expand All @@ -125,6 +129,7 @@ export interface ClusterPrometheusPreferences {
prometheusProvider?: {
type: string;
};
prometheusQueryOptions?: QueryFilterOptions;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { getInjectable } from "@ogre-tools/injectable";

export const getOperatorLikeQueryFor = ({ rateAccuracy }: { rateAccuracy: string }): PrometheusProvider["getQuery"] => (
(opts, queryName) => {
const emptyContainerAndImageFilter = opts.hideEmptyContainers === "true" ? `container!="", image!="",` : "";
const emptyContainerFilter = opts.hideEmptyContainers === "true" ? `container!="",` : "";

switch(opts.category) {
case "cluster":
switch (queryName) {
Expand Down Expand Up @@ -71,19 +74,19 @@ export const getOperatorLikeQueryFor = ({ rateAccuracy }: { rateAccuracy: string
case "pods":
switch (queryName) {
case "cpuUsage":
return `sum(rate(container_cpu_usage_seconds_total{pod=~"${opts.pods}", namespace="${opts.namespace}"}[${rateAccuracy}])) by (${opts.selector})`;
return `sum(rate(container_cpu_usage_seconds_total{${emptyContainerAndImageFilter} pod=~"${opts.pods}", namespace="${opts.namespace}"}[${rateAccuracy}])) by (${opts.selector})`;
case "cpuRequests":
return `sum(kube_pod_container_resource_requests{pod=~"${opts.pods}", resource="cpu", namespace="${opts.namespace}"}) by (${opts.selector})`;
case "cpuLimits":
return `sum(kube_pod_container_resource_limits{pod=~"${opts.pods}", resource="cpu", namespace="${opts.namespace}"}) by (${opts.selector})`;
return `sum(kube_pod_container_resource_limits{${emptyContainerAndImageFilter} pod=~"${opts.pods}", resource="cpu", namespace="${opts.namespace}"}) by (${opts.selector})`;
case "memoryUsage":
return `sum(container_memory_working_set_bytes{pod=~"${opts.pods}", namespace="${opts.namespace}"}) by (${opts.selector})`;
case "memoryRequests":
return `sum(kube_pod_container_resource_requests{pod=~"${opts.pods}", resource="memory", namespace="${opts.namespace}"}) by (${opts.selector})`;
return `sum(kube_pod_container_resource_requests{${emptyContainerFilter} pod=~"${opts.pods}", resource="memory", namespace="${opts.namespace}"}) by (${opts.selector})`;
case "memoryLimits":
return `sum(kube_pod_container_resource_limits{pod=~"${opts.pods}", resource="memory", namespace="${opts.namespace}"}) by (${opts.selector})`;
return `sum(kube_pod_container_resource_limits{${emptyContainerFilter} pod=~"${opts.pods}", resource="memory", namespace="${opts.namespace}"}) by (${opts.selector})`;
case "fsUsage":
return `sum(container_fs_usage_bytes{pod=~"${opts.pods}", namespace="${opts.namespace}"}) by (${opts.selector})`;
return `sum(container_fs_usage_bytes{${emptyContainerFilter} pod=~"${opts.pods}", namespace="${opts.namespace}"}) by (${opts.selector})`;
case "fsWrites":
return `sum(rate(container_fs_writes_bytes_total{pod=~"${opts.pods}", namespace="${opts.namespace}"}[${rateAccuracy}])) by (${opts.selector})`;
case "fsReads":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ const addMetricsRouteInjectable = getRouteInjectable({

if (isObject(payload)) {
const data = payload as Record<string, Record<string, string>>;
const queryFilterPreferences: Record<string, string> = cluster.preferences.prometheusQueryOptions ?
Object.fromEntries(Object.entries(cluster.preferences.prometheusQueryOptions).map(([k, v]) => [k, String(v)]))
: {};

const queries = object.entries(data)
.map(([queryName, queryOpts]) => (
provider.getQuery(queryOpts, queryName)
provider.getQuery({ ...queryOpts, ...queryFilterPreferences }, queryName)
));

const result = await loadMetrics(queries, cluster, prometheusPath, queryParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import type { MetricProviderInfo, RequestMetricsProviders } from "../../../commo
import { withInjectables } from "@ogre-tools/injectable-react";
import requestMetricsProvidersInjectable from "../../../common/k8s-api/endpoints/metrics.api/request-providers.injectable";
import productNameInjectable from "../../../common/vars/product-name.injectable";
import { Checkbox } from "../checkbox";
import Gutter from "../gutter/gutter";


export interface ClusterPrometheusSettingProps {
cluster: Cluster;
Expand Down Expand Up @@ -123,6 +126,9 @@ class NonInjectedClusterPrometheusSetting extends React.Component<ClusterPrometh
};

render() {
const providerType = this.props.cluster.preferences.prometheusProvider?.type;
const showQueryFilters = providerType === "operator" || !providerType;

return (
<>
<section>
Expand All @@ -147,6 +153,28 @@ class NonInjectedClusterPrometheusSetting extends React.Component<ClusterPrometh
)
}
</section>
{
showQueryFilters && (
<>
<Gutter />
<section>
<SubTitle title="Prometheus queries" />
<Checkbox
label="Filter empty containers on pod metrics"
value={this.props.cluster.preferences.prometheusQueryOptions?.hideEmptyContainers ?? false}
onChange={v => {
this.props.cluster.preferences.prometheusQueryOptions = {
hideEmptyContainers: v,
};
}}
/>
<small className="hint">
In certain metric setups, pod metrics may be observed as double values. This filter can be helpful in ensuring accurate metric are shown.
</small>
</section>
</>
)
}
{this.canEditPrometheusPath && (
<>
<hr />
Expand Down