Skip to content

Commit

Permalink
feat: Make it possible to filter alerts based on state
Browse files Browse the repository at this point in the history
  • Loading branch information
tiithansen committed Nov 10, 2024
1 parent e2df175 commit 3281252
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/common/variableHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ export function createNamespaceVariable() {
})
}

export function createAlertStateVariable() {
return new QueryVariable({
name: 'alertState',
label: 'Alert State',
datasource: {
uid: '$datasource',
type: 'prometheus',
},
query: {
refId: 'alertState',
query: `label_values(ALERTS{cluster="$cluster"},alertstate)`,
},
defaultToAll: true,
allValue: '.*',
includeAll: true,
isMulti: true,
})
}

export function createTimeRange() {
return new SceneTimeRange({
from: 'now-1h',
Expand Down
6 changes: 4 additions & 2 deletions src/components/AlertsTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
VariableValueSelectors,
SceneVariables,
} from '@grafana/scenes';
import { createNamespaceVariable } from 'common/variableHelpers';
import { createAlertStateVariable, createNamespaceVariable } from 'common/variableHelpers';
import { SortingState } from 'common/sortingHelpers';
import { AsyncTable, Column, ColumnSortingConfig, QueryBuilder } from 'components/AsyncTable';
import { TextColor } from 'common/types';
Expand Down Expand Up @@ -137,13 +137,14 @@ class AlertsQueryBuilder implements QueryBuilder<TableRow> {
const serializedFilters = this.labelFilters ? serializeLabelFilters(this.labelFilters) : '';
const hasNamespaceVariable = variables.getByName('namespace') !== undefined;
const hasSearchVariable = variables.getByName('alertSearch') !== undefined;
const hasAlertStateVariable = variables.getByName('alertState') !== undefined;

const finalQuery = `
ALERTS{
cluster="$cluster",
${ hasSearchVariable ? `alertname=~"$alertSearch.*",`: '' }
${ hasNamespaceVariable ? `namespace=~"$namespace",` : '' }
alertstate="firing",
${ hasAlertStateVariable ? `alertstate=~"$alertState",` : '' }
${serializedFilters}
}
* ignoring(alertstate) group_right(alertstate) ALERTS_FOR_STATE{
Expand Down Expand Up @@ -179,6 +180,7 @@ export function AlertsTable(labelFilters?: LabelFilters, showVariableControls =
const variables = new SceneVariableSet({
variables: shouldCreateVariables ? [
createNamespaceVariable(),
createAlertStateVariable(),
new TextBoxVariable({
name: 'alertSearch',
label: 'Search',
Expand Down

0 comments on commit 3281252

Please sign in to comment.