pagination WIP

alerting/prometheus-pagination
Gilles De Mey 10 months ago
parent 8b215d60ac
commit bbbc23b691
No known key found for this signature in database
  1. 1
      pkg/services/ngalert/api/tooling/definitions/prom.go
  2. 31
      public/app/features/alerting/unified/api/prometheus.ts
  3. 2
      public/app/features/alerting/unified/state/actions.ts
  4. 1
      public/app/types/unified-alerting-dto.ts

@ -72,6 +72,7 @@ type DiscoveryBase struct {
type RuleDiscovery struct {
// required: true
RuleGroups []RuleGroup `json:"groups"`
NextToken string `json:"nextToken,omitempty"`
Totals map[string]int64 `json:"totals,omitempty"`
}

@ -22,11 +22,12 @@ export interface FetchPromRulesFilter {
export interface PrometheusDataSourceConfig {
dataSourceName: string;
limitAlerts?: number;
maxGroups?: number;
identifier?: RuleIdentifier;
}
export function prometheusUrlBuilder(dataSourceConfig: PrometheusDataSourceConfig) {
const { dataSourceName, limitAlerts, identifier } = dataSourceConfig;
const { dataSourceName, limitAlerts, identifier, maxGroups } = dataSourceConfig;
return {
rules: (filter?: FetchPromRulesFilter, state?: string[], matcher?: Matcher[]) => {
@ -38,6 +39,10 @@ export function prometheusUrlBuilder(dataSourceConfig: PrometheusDataSourceConfi
searchParams.set('limit_alerts', String(limitAlerts));
}
if (Number.isFinite(maxGroups)) {
searchParams.set('max_groups', String(maxGroups));
}
if (identifier && (isPrometheusRuleIdentifier(identifier) || isCloudRuleIdentifier(identifier))) {
searchParams.set('file', identifier.namespace);
searchParams.set('rule_group', identifier.groupName);
@ -138,13 +143,14 @@ export async function fetchRules(
limitAlerts?: number,
matcher?: Matcher[],
state?: string[],
identifier?: RuleIdentifier
identifier?: RuleIdentifier,
maxGroups?: number
): Promise<RuleNamespace[]> {
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, identifier }).rules(
const { url, params } = prometheusUrlBuilder({ dataSourceName, limitAlerts, maxGroups, identifier }).rules(
filter,
state,
matcher
@ -158,12 +164,19 @@ export async function fetchRules(
showErrorAlert: false,
showSuccessAlert: false,
})
).catch((e) => {
if ('status' in e && e.status === 404) {
throw new Error('404 from rule state endpoint. Perhaps ruler API is not enabled?');
}
throw e;
});
)
.then((result) => {
if (result?.data?.nextToken) {
console.log('have next token');
}
return result;
})
.catch((e) => {
if ('status' in e && e.status === 404) {
throw new Error('404 from rule state endpoint. Perhaps ruler API is not enabled?');
}
throw e;
});
return groupRulesByFileName(response.data.data.groups, dataSourceName);
}

@ -95,7 +95,7 @@ export const fetchPromRulesAction = createAsyncThunk(
});
return await withSerializedError(
fetchRulesWithLogging(rulesSourceName, filter, limitAlerts, matcher, state, identifier)
fetchRulesWithLogging(rulesSourceName, filter, limitAlerts, matcher, state, identifier, 1)
);
}
);

@ -169,6 +169,7 @@ export interface PromResponse<T> {
export type PromRulesResponse = PromResponse<{
groups: PromRuleGroupDTO[];
nextToken?: string; // if the Prometheus-API supports pagination it will return a "nextToken"
totals?: AlertGroupTotals;
}>;

Loading…
Cancel
Save