Alerting: Normalize health when filtering rules (#113087)

This commit is contained in:
Gilles De Mey
2025-10-28 13:23:29 +00:00
committed by GitHub
parent 7df95261f3
commit 5670f1c34c
4 changed files with 21 additions and 3 deletions
@@ -1,3 +1,3 @@
export type Health = 'nodata' | 'error';
export type Health = 'ok' | 'nodata' | 'error';
export type State = 'normal' | 'firing' | 'pending' | 'unknown' | 'recovering';
export type Type = 'alerting' | 'recording';
@@ -93,7 +93,7 @@ export function normalizeHealth(health?: RuleHealth): NormalizedHealth {
}
function isValidHealth(health: string): health is NonNullable<NormalizedHealth> {
const valid: Array<NonNullable<NormalizedHealth>> = ['nodata', 'error'] as const;
const valid: Array<NonNullable<NormalizedHealth>> = ['ok', 'nodata', 'error'] as const;
return valid.some((v) => v === health);
}
@@ -138,9 +138,26 @@ describe('ruleFilter', () => {
health: RuleHealth.Error,
});
const prometheusErrorRule = mockPromAlertingRule({
name: 'Error Rule',
health: 'err',
});
expect(ruleFilter(healthyRule, getFilter({ ruleHealth: RuleHealth.Ok }))).toBe(true);
expect(ruleFilter(healthyRule, getFilter({ ruleHealth: RuleHealth.Error }))).toBe(false);
expect(ruleFilter(errorRule, getFilter({ ruleHealth: RuleHealth.Error }))).toBe(true);
expect(ruleFilter(prometheusErrorRule, getFilter({ ruleHealth: RuleHealth.Error }))).toBe(true);
});
it('should normalize health values when filtering', () => {
// Legacy Prometheus health value 'err' should be normalized to 'error'
const legacyErrorRule = mockPromAlertingRule({
name: 'Legacy Error Rule',
health: 'err',
});
// When filtering for 'error', it should match rules with health 'err' (legacy) or 'error'
expect(ruleFilter(legacyErrorRule, getFilter({ ruleHealth: RuleHealth.Error }))).toBe(true);
});
it('should filter by dashboard UID', () => {
@@ -11,6 +11,7 @@ import { getDatasourceAPIUid } from '../../utils/datasource';
import { fuzzyMatches } from '../../utils/fuzzySearch';
import { parseMatcher } from '../../utils/matchers';
import { isPluginProvidedRule, prometheusRuleType } from '../../utils/rules';
import { normalizeHealth } from '../components/util';
/**
* @returns True if the group matches the filter, false otherwise. Keeps rules intact
@@ -79,7 +80,7 @@ export function ruleFilter(rule: PromRuleDTO, filterState: RulesFilter) {
}
}
if (filterState.ruleHealth && health !== filterState.ruleHealth) {
if (filterState.ruleHealth && normalizeHealth(health) !== filterState.ruleHealth) {
return false;
}