Skip to content

Commit

Permalink
feat: Add alerts to deamonsets table
Browse files Browse the repository at this point in the history
  • Loading branch information
tiithansen committed Jul 6, 2024
1 parent 1d6f1e7 commit 39fe8b5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
27 changes: 26 additions & 1 deletion src/pages/Workloads/tabs/DaemonSets/DaemonSets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@grafana/scenes';
import { buildExpandedRowScene } from './ExpandedRow';
import { ReplicasCell } from 'pages/Workloads/components/ReplicasCell';
import { getSeriesValue } from 'common/seriesHelpers';
import { getAllSeries, getSeriesValue } from 'common/seriesHelpers';
import { createNamespaceVariable } from 'common/variableHelpers';
import { createRowQueries } from './Queries';
import { Metrics } from 'metrics/metrics';
Expand All @@ -19,6 +19,7 @@ import { SortingState } from 'common/sortingHelpers';
import { prefixRoute } from 'utils/utils.routing';
import { ROUTES } from '../../../../constants';
import { TableRow } from "./types";
import { TextColor } from 'common/types';

const namespaceVariable = createNamespaceVariable();

Expand All @@ -30,7 +31,18 @@ const searchVariable = new TextBoxVariable({

const serieMatcherPredicate = (row: TableRow) => (value: any) => value.daemonset === row.daemonset;

function determineAlertsColor(row: TableRow): TextColor {
let color: TextColor = 'primary';
if (row.alerts && row.alerts.length > 0) {
color = 'error'
}

return color
}

function asyncDataRowMapper(row: TableRow, asyncRowData: Map<string, number[]>) {

row.alerts = getAllSeries(asyncRowData, 'alerts', serieMatcherPredicate(row))

const total = getSeriesValue(asyncRowData, 'replicas', serieMatcherPredicate(row))
const ready = getSeriesValue(asyncRowData, 'replicas_ready', serieMatcherPredicate(row))
Expand Down Expand Up @@ -74,6 +86,19 @@ const columns: Array<Column<TableRow>> = [
},
}
},
{
id: 'alerts',
header: 'ALERTS',
sortingConfig: {
enabled: true,
local: false,
type: 'value'
},
accessor: (row: TableRow) => row.alerts ? row.alerts.length : 0,
cellProps: {
color: determineAlertsColor
}
},
{
id: 'replicas',
header: 'REPLICAS',
Expand Down
70 changes: 55 additions & 15 deletions src/pages/Workloads/tabs/DaemonSets/Queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,76 @@ import { SceneVariables } from "@grafana/scenes";
import { resolveVariable } from "common/variableHelpers";
import { Metrics } from "metrics/metrics";
import { TableRow } from "./types";
import { Labels, MatchOperators, PromQL } from "common/promql";

function createReplicasQuery(cluster: string, additionalLabels: Labels) {

return PromQL.max(
PromQL.metric(Metrics.kubeDaemonsetStatusDesiredNumberScheduled.name)
.withLabels(additionalLabels)
.withLabelEquals('cluster', cluster)
).by([
Metrics.kubeDaemonsetStatusDesiredNumberScheduled.labels.daemonset
])
}

function createReplicasReadyQuery(cluster: string, additionalLabels: Labels) {

return PromQL.max(
PromQL.metric(Metrics.kubeDaemonsetStatusNumberReady.name)
.withLabels(additionalLabels)
.withLabelEquals('cluster', cluster)
).by([
Metrics.kubeDaemonsetStatusNumberReady.labels.daemonset
])
}

function createAlertsQuery(cluster: string, additionalLabels: Labels) {

return PromQL.metric('ALERTS')
.withLabelEquals('alertstate', 'firing')
.withLabels(additionalLabels)
.withLabelEquals('cluster', cluster)
.multiply()
.ignoring(['alertstate'])
.groupRight(
['alertstate'],
PromQL.metric('ALERTS_FOR_STATE')
.withLabels(additionalLabels)
.withLabelEquals('cluster', cluster)
)
}

export function createRowQueries(rows: TableRow[], sceneVariables: SceneVariables) {

const daemonSet = rows.map(row => row.daemonset).join('|');
const daemonSets = rows.map(row => row.daemonset).join('|');
const cluster = resolveVariable(sceneVariables, 'cluster');

const additionalLabels: Labels = {
daemonset: {
operator: MatchOperators.MATCHES,
value: daemonSets
}
}

return [
{
refId: 'replicas',
expr: `
max(
${Metrics.kubeDaemonsetStatusDesiredNumberScheduled.name}{
${Metrics.kubeDaemonsetStatusDesiredNumberScheduled.labels.daemonset}=~"${daemonSet}",
cluster="${cluster}"
}
) by (${Metrics.kubeDaemonsetStatusDesiredNumberScheduled.labels.daemonset})`,
expr: createReplicasQuery(cluster?.toString()!, additionalLabels).stringify(),
instant: true,
format: 'table'
},
{
refId: 'replicas_ready',
expr: `
max(
${Metrics.kubeDaemonsetStatusNumberReady.name}{
${Metrics.kubeDaemonsetStatusNumberReady.labels.daemonset}=~"${daemonSet}",
cluster="${cluster}"
}
) by (${Metrics.kubeDaemonsetStatusNumberReady.labels.daemonset})`,
expr: createReplicasReadyQuery(cluster?.toString()!, additionalLabels).stringify(),
instant: true,
format: 'table'
},
{
refId: 'alerts',
expr: createAlertsQuery(cluster?.toString()!, additionalLabels).stringify(),
instant: true,
format: 'table'
}
];
}
7 changes: 7 additions & 0 deletions src/pages/Workloads/tabs/DaemonSets/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export interface DaemonSetAlert {
alertname: string;
severity: string;
daemonset: string;
}

export interface TableRow {
cluster: string;
daemonset: string;
namespace: string;
alerts: DaemonSetAlert[];
replicas: {
total: number;
ready: number;
Expand Down

0 comments on commit 39fe8b5

Please sign in to comment.