Alerting: Add limits and filters to Prometheus Rules API (#66627)

This commit adds support for limits and filters to the Prometheus Rules
API.

Limits:

It adds a number of limits to the Grafana flavour of the Prometheus Rules
API:

- `limit` limits the maximum number of Rule Groups returned
- `limit_rules` limits the maximum number of rules per Rule Group
- `limit_alerts` limits the maximum number of alerts per rule

It sorts Rule Groups and rules within Rule Groups such that data in the
response is stable across requests. It also returns summaries (totals)
for all Rule Groups, individual Rule Groups and rules.

Filters:

Alerts can be filtered by state with the `state` query string. An example
of an HTTP request asking for just firing alerts might be
`/api/prometheus/grafana/api/v1/rules?state=alerting`.

A request can filter by two or more states by adding additional `state`
query strings to the URL. For example `?state=alerting&state=normal`.

Like the alert list panel, the `firing`, `pending` and `normal` state are
first compared against the state of each alert rule. All other states are
ignored. If the alert rule matches then its alert instances are filtered
against states once more.

Alerts can also be filtered by labels using the `matcher` query string.
Like `state`, multiple matchers can be provided by adding additional
`matcher` query strings to the URL.

The match expression should be parsed using existing regular expression
and sent to the API as URL-encoded JSON in the format:

{
    "name": "test",
    "value": "value1",
    "isRegex": false,
    "isEqual": true
}

The `isRegex` and `isEqual` options work as follows:

| IsEqual | IsRegex  | Operator |
| ------- | -------- | -------- |
| true    | false    |    =     |
| true    | true     |    =~    |
| false   | true     |    !~    |
| false   | false    |    !=    |
This commit is contained in:
George Robinson
2023-04-17 17:45:06 +01:00
committed by GitHub
parent 391a192310
commit 19ebb079ba
8 changed files with 1192 additions and 24 deletions
+54
View File
@@ -180,6 +180,37 @@ type AlertRuleWithOptionals struct {
HasPause bool
}
// AlertsRulesBy is a function that defines the ordering of alert rules.
type AlertRulesBy func(a1, a2 *AlertRule) bool
func (by AlertRulesBy) Sort(rules []*AlertRule) {
sort.Sort(AlertRulesSorter{rules: rules, by: by})
}
// AlertRulesByIndex orders alert rules by rule group index. You should
// make sure that all alert rules belong to the same rule group (have the
// same RuleGroupKey) before using this ordering.
func AlertRulesByIndex(a1, a2 *AlertRule) bool {
return a1.RuleGroupIndex < a2.RuleGroupIndex
}
func AlertRulesByGroupKeyAndIndex(a1, a2 *AlertRule) bool {
k1, k2 := a1.GetGroupKey(), a2.GetGroupKey()
if k1 == k2 {
return a1.RuleGroupIndex < a2.RuleGroupIndex
}
return AlertRuleGroupKeyByNamespaceAndRuleGroup(&k1, &k2)
}
type AlertRulesSorter struct {
rules []*AlertRule
by AlertRulesBy
}
func (s AlertRulesSorter) Len() int { return len(s.rules) }
func (s AlertRulesSorter) Swap(i, j int) { s.rules[i], s.rules[j] = s.rules[j], s.rules[i] }
func (s AlertRulesSorter) Less(i, j int) bool { return s.by(s.rules[i], s.rules[j]) }
// GetDashboardUID returns the DashboardUID or "".
func (alertRule *AlertRule) GetDashboardUID() string {
if alertRule.DashboardUID != nil {
@@ -303,6 +334,29 @@ func (k AlertRuleKey) String() string {
return fmt.Sprintf("{orgID: %d, UID: %s}", k.OrgID, k.UID)
}
// AlertRuleGroupKeyBy is a function that defines the ordering of alert rule group keys.
type AlertRuleGroupKeyBy func(a1, a2 *AlertRuleGroupKey) bool
func (by AlertRuleGroupKeyBy) Sort(keys []AlertRuleGroupKey) {
sort.Sort(AlertRuleGroupKeySorter{keys: keys, by: by})
}
func AlertRuleGroupKeyByNamespaceAndRuleGroup(k1, k2 *AlertRuleGroupKey) bool {
if k1.NamespaceUID == k2.NamespaceUID {
return k1.RuleGroup < k2.RuleGroup
}
return k1.NamespaceUID < k2.NamespaceUID
}
type AlertRuleGroupKeySorter struct {
keys []AlertRuleGroupKey
by AlertRuleGroupKeyBy
}
func (s AlertRuleGroupKeySorter) Len() int { return len(s.keys) }
func (s AlertRuleGroupKeySorter) Swap(i, j int) { s.keys[i], s.keys[j] = s.keys[j], s.keys[i] }
func (s AlertRuleGroupKeySorter) Less(i, j int) bool { return s.by(&s.keys[i], &s.keys[j]) }
// GetKey returns the alert definitions identifier
func (alertRule *AlertRule) GetKey() AlertRuleKey {
return AlertRuleKey{OrgID: alertRule.OrgID, UID: alertRule.UID}
@@ -17,6 +17,51 @@ import (
"github.com/grafana/grafana/pkg/util"
)
func TestSortAlertRulesByGroupKeyAndIndex(t *testing.T) {
tc := []struct {
name string
input []*AlertRule
expected []*AlertRule
}{{
name: "alert rules are ordered by organization",
input: []*AlertRule{
{OrgID: 2, NamespaceUID: "test2"},
{OrgID: 1, NamespaceUID: "test1"},
},
expected: []*AlertRule{
{OrgID: 1, NamespaceUID: "test1"},
{OrgID: 2, NamespaceUID: "test2"},
},
}, {
name: "alert rules in same organization are ordered by namespace",
input: []*AlertRule{
{OrgID: 1, NamespaceUID: "test2"},
{OrgID: 1, NamespaceUID: "test1"},
},
expected: []*AlertRule{
{OrgID: 1, NamespaceUID: "test1"},
{OrgID: 1, NamespaceUID: "test2"},
},
}, {
name: "alert rules with same group key are ordered by index",
input: []*AlertRule{
{OrgID: 1, NamespaceUID: "test", RuleGroupIndex: 2},
{OrgID: 1, NamespaceUID: "test", RuleGroupIndex: 1},
},
expected: []*AlertRule{
{OrgID: 1, NamespaceUID: "test", RuleGroupIndex: 1},
{OrgID: 1, NamespaceUID: "test", RuleGroupIndex: 2},
},
}}
for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
AlertRulesBy(AlertRulesByGroupKeyAndIndex).Sort(tt.input)
assert.EqualValues(t, tt.expected, tt.input)
})
}
}
func TestNoDataStateFromString(t *testing.T) {
allKnownNoDataStates := [...]NoDataState{
Alerting,