Alerting: Fix for data source filter on cloud rules. (#79327)

* fix data source filter not worling for cloud rules

* Add test
This commit is contained in:
Sonia Aguilar 2023-12-11 17:14:46 +01:00 committed by GitHub
parent 12793bfbed
commit e6f2de51a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 7 deletions

View File

@ -2,7 +2,9 @@ import { setDataSourceSrv } from '@grafana/runtime';
import { PromAlertingRuleState } from '../../../../types/unified-alerting-dto';
import {
getCloudRule,
mockAlertQuery,
mockCombinedCloudRuleNamespace,
mockCombinedRule,
mockCombinedRuleGroup,
mockCombinedRuleNamespace,
@ -154,16 +156,24 @@ describe('filterRules', function () {
data: [mockAlertQuery({ datasourceUid: dataSources.loki.uid })],
}),
}),
getCloudRule({ name: 'Cloud' }),
];
const ns = mockCombinedRuleNamespace({
groups: [mockCombinedRuleGroup('Resources usage group', rules)],
});
const cloudNs = mockCombinedCloudRuleNamespace(
{
groups: [mockCombinedRuleGroup('Resources usage group', rules)],
},
'Prometheus-ds'
);
const filtered = filterRules([ns], getFilter({ dataSourceNames: ['loki'] }));
const filtered = filterRules([ns, cloudNs], getFilter({ dataSourceNames: ['loki', 'Prometheus-ds'] }));
expect(filtered[0].groups[0].rules).toHaveLength(1);
expect(filtered[0].groups[0].rules).toHaveLength(2);
expect(filtered[0].groups[0].rules[0].name).toBe('Memory too low');
expect(filtered[0].groups[0].rules[1].name).toBe('Cloud');
});
it('should be able to combine multiple predicates with AND', () => {

View File

@ -204,9 +204,13 @@ const reduceGroups = (filterState: RulesFilter) => {
}
if ('dataSourceNames' in matchesFilterFor) {
const doesNotQueryDs = isGrafanaRulerRule(rule.rulerRule) && isQueryingDataSource(rule.rulerRule, filterState);
if (isGrafanaRulerRule(rule.rulerRule)) {
const doesNotQueryDs = isQueryingDataSource(rule.rulerRule, filterState);
if (doesNotQueryDs) {
if (doesNotQueryDs) {
matchesFilterFor.dataSourceNames = true;
}
} else {
matchesFilterFor.dataSourceNames = true;
}
}

View File

@ -15,16 +15,16 @@ import {
ScopedVars,
TestDataSourceResponse,
} from '@grafana/data';
import { config, DataSourceSrv, GetDataSourceListFilters } from '@grafana/runtime';
import { DataSourceSrv, GetDataSourceListFilters, config } from '@grafana/runtime';
import { defaultDashboard } from '@grafana/schema';
import { contextSrv } from 'app/core/services/context_srv';
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
import {
AlertmanagerAlert,
AlertManagerCortexConfig,
AlertState,
AlertmanagerAlert,
AlertmanagerGroup,
AlertmanagerStatus,
AlertState,
GrafanaManagedReceiverConfig,
MatcherOperator,
Silence,
@ -657,6 +657,17 @@ export function mockCombinedRuleNamespace(namespace: Partial<CombinedRuleNamespa
...namespace,
};
}
export function mockCombinedCloudRuleNamespace(
namespace: Partial<CombinedRuleNamespace>,
dataSourceName: string
): CombinedRuleNamespace {
return {
name: 'Grafana',
groups: [],
rulesSource: mockDataSource({ name: dataSourceName, uid: 'Prometheus-1' }),
...namespace,
};
}
export function getGrafanaRule(override?: Partial<CombinedRule>, rulerOverride?: Partial<GrafanaRuleDefinition>) {
return mockCombinedRule({