diff --git a/public/app/features/alerting/unified/PanelAlertTabContent.test.tsx b/public/app/features/alerting/unified/PanelAlertTabContent.test.tsx index c350e122493..9620dbf93da 100644 --- a/public/app/features/alerting/unified/PanelAlertTabContent.test.tsx +++ b/public/app/features/alerting/unified/PanelAlertTabContent.test.tsx @@ -327,6 +327,7 @@ describe('PanelAlertTabContent', () => { }, undefined, undefined, + undefined, undefined ); }); diff --git a/public/app/features/alerting/unified/api/prometheus.ts b/public/app/features/alerting/unified/api/prometheus.ts index 1e7a88c7d0f..2f7b8d05dbe 100644 --- a/public/app/features/alerting/unified/api/prometheus.ts +++ b/public/app/features/alerting/unified/api/prometheus.ts @@ -2,10 +2,11 @@ import { lastValueFrom } from 'rxjs'; import { getBackendSrv } from '@grafana/runtime'; import { Matcher } from 'app/plugins/datasource/alertmanager/types'; -import { RuleNamespace } from 'app/types/unified-alerting'; +import { RuleIdentifier, RuleNamespace } from 'app/types/unified-alerting'; import { PromRulesResponse } from 'app/types/unified-alerting-dto'; import { getDatasourceAPIUid, GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource'; +import { isCloudRuleIdentifier, isPrometheusRuleIdentifier } from '../utils/rules'; export interface FetchPromRulesFilter { dashboardUID: string; @@ -15,10 +16,11 @@ export interface FetchPromRulesFilter { export interface PrometheusDataSourceConfig { dataSourceName: string; limitAlerts?: number; + identifier?: RuleIdentifier; } export function prometheusUrlBuilder(dataSourceConfig: PrometheusDataSourceConfig) { - const { dataSourceName, limitAlerts } = dataSourceConfig; + const { dataSourceName, limitAlerts, identifier } = dataSourceConfig; return { rules: (filter?: FetchPromRulesFilter, state?: string[], matcher?: Matcher[]) => { @@ -30,6 +32,11 @@ export function prometheusUrlBuilder(dataSourceConfig: PrometheusDataSourceConfi searchParams.set('limit_alerts', String(limitAlerts)); } + if (identifier && (isPrometheusRuleIdentifier(identifier) || isCloudRuleIdentifier(identifier))) { + searchParams.set('file', identifier.namespace); + searchParams.set('rule_group', identifier.groupName); + } + const params = prepareRulesFilterQueryParams(searchParams, filter); return { @@ -81,13 +88,18 @@ export async function fetchRules( filter?: FetchPromRulesFilter, limitAlerts?: number, matcher?: Matcher[], - state?: string[] + state?: string[], + identifier?: RuleIdentifier ): Promise { if (filter?.dashboardUID && dataSourceName !== GRAFANA_RULES_SOURCE_NAME) { throw new Error('Filtering by dashboard UID is only supported for Grafana Managed rules.'); } - const { url, params } = prometheusUrlBuilder({ dataSourceName, limitAlerts }).rules(filter, state, matcher); + const { url, params } = prometheusUrlBuilder({ dataSourceName, limitAlerts, identifier }).rules( + filter, + state, + matcher + ); // adding state param here instead of adding it in prometheusUrlBuilder, for being a possible multiple query param const response = await lastValueFrom( diff --git a/public/app/features/alerting/unified/hooks/useCombinedRule.ts b/public/app/features/alerting/unified/hooks/useCombinedRule.ts index 393cca5c80d..9b1640373f4 100644 --- a/public/app/features/alerting/unified/hooks/useCombinedRule.ts +++ b/public/app/features/alerting/unified/hooks/useCombinedRule.ts @@ -17,7 +17,7 @@ export function useCombinedRule( identifier: RuleIdentifier | undefined, ruleSourceName: string | undefined ): AsyncRequestState { - const requestState = useCombinedRulesLoader(ruleSourceName); + const requestState = useCombinedRulesLoader(ruleSourceName, identifier); const combinedRules = useCombinedRuleNamespaces(ruleSourceName); const rule = useMemo(() => { @@ -79,7 +79,10 @@ export function useCombinedRulesMatching( }; } -function useCombinedRulesLoader(rulesSourceName: string | undefined): AsyncRequestState { +function useCombinedRulesLoader( + rulesSourceName: string | undefined, + identifier?: RuleIdentifier +): AsyncRequestState { const dispatch = useDispatch(); const promRuleRequests = useUnifiedAlertingSelector((state) => state.promRules); const promRuleRequest = getRequestState(rulesSourceName, promRuleRequests); @@ -91,7 +94,7 @@ function useCombinedRulesLoader(rulesSourceName: string | undefined): AsyncReque return; } - await dispatch(fetchPromAndRulerRulesAction({ rulesSourceName })); + await dispatch(fetchPromAndRulerRulesAction({ rulesSourceName, identifier })); }, [dispatch, rulesSourceName]); return { diff --git a/public/app/features/alerting/unified/state/actions.ts b/public/app/features/alerting/unified/state/actions.ts index 7c96f819d83..853b05844fe 100644 --- a/public/app/features/alerting/unified/state/actions.ts +++ b/public/app/features/alerting/unified/state/actions.ts @@ -107,12 +107,14 @@ export const fetchPromRulesAction = createAsyncThunk( limitAlerts, matcher, state, + identifier, }: { rulesSourceName: string; filter?: FetchPromRulesFilter; limitAlerts?: number; matcher?: Matcher[]; state?: string[]; + identifier?: RuleIdentifier; }, thunkAPI ): Promise => { @@ -123,7 +125,9 @@ export const fetchPromRulesAction = createAsyncThunk( thunk: 'unifiedalerting/fetchPromRules', }); - return await withSerializedError(fetchRulesWithLogging(rulesSourceName, filter, limitAlerts, matcher, state)); + return await withSerializedError( + fetchRulesWithLogging(rulesSourceName, filter, limitAlerts, matcher, state, identifier) + ); } ); @@ -239,12 +243,18 @@ export const fetchRulerRulesAction = createAsyncThunk( } ); -export function fetchPromAndRulerRulesAction({ rulesSourceName }: { rulesSourceName: string }): ThunkResult { +export function fetchPromAndRulerRulesAction({ + rulesSourceName, + identifier, +}: { + rulesSourceName: string; + identifier?: RuleIdentifier; +}): ThunkResult { return async (dispatch, getState) => { await dispatch(fetchRulesSourceBuildInfoAction({ rulesSourceName })); const dsConfig = getDataSourceConfig(getState, rulesSourceName); - await dispatch(fetchPromRulesAction({ rulesSourceName })); + await dispatch(fetchPromRulesAction({ rulesSourceName, identifier })); if (dsConfig.rulerConfig) { await dispatch(fetchRulerRulesAction({ rulesSourceName })); }