File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -71,13 +71,18 @@ inactive = 0`,
7171 // We use the Rules endpoint since it contains
7272 // the state of inactive Alert Rules, unlike the Alert endpoint
7373 // Search requested Alert in all Groups and all Rules
74- alerts , err := c .API .Rules (ctx )
75- if err != nil {
76- check .ExitError (err )
74+ alertrules , errR := c .API .Rules (ctx )
75+ if errR != nil {
76+ check .ExitError (errR )
77+ }
78+
79+ alerts , errA := c .API .Alerts (ctx )
80+ if errA != nil {
81+ check .ExitError (errA )
7782 }
7883
7984 // Get all rules from all groups into a single list
80- rules := alert .FlattenRules (alerts .Groups , cliAlertConfig .Group )
85+ rules := alert .FlattenRules (alertrules .Groups , cliAlertConfig .Group , alerts . Alerts )
8186
8287 // If there are no rules we can exit early
8388 if len (rules ) == 0 {
Original file line number Diff line number Diff line change @@ -12,14 +12,18 @@ import (
1212 "github.com/prometheus/common/model"
1313)
1414
15+ const (
16+ alertnameLabelKey = "alertname"
17+ )
18+
1519// Internal representation of Prometheus Rules.
1620// Alert attribute will be used when iterating over multiple AlertingRules.
1721type Rule struct {
1822 AlertingRule v1.AlertingRule
1923 Alert * v1.Alert
2024}
2125
22- func FlattenRules (groups []v1.RuleGroup , wantedGroups []string ) []Rule {
26+ func FlattenRules (groups []v1.RuleGroup , wantedGroups []string , alerts []v1. Alert ) []Rule {
2327 // Flattens a list of RuleGroup containing a list of Rules into
2428 // a list of internal Alertingrules.
2529 var l int
@@ -50,6 +54,14 @@ func FlattenRules(groups []v1.RuleGroup, wantedGroups []string) []Rule {
5054 // since RecodingRules can simply be queried.
5155 if _ , ok := rl .(v1.AlertingRule ); ok {
5256 r .AlertingRule = rl .(v1.AlertingRule )
57+ // Merge labels from active alerts
58+ for _ , al := range alerts {
59+ alertName := al .Labels [alertnameLabelKey ]
60+ if r .AlertingRule .Name == string (alertName ) {
61+ r .AlertingRule .Labels = r .AlertingRule .Labels .Merge (al .Labels )
62+ }
63+ }
64+
5365 rules = append (rules , r )
5466 }
5567 }
Original file line number Diff line number Diff line change @@ -183,6 +183,15 @@ func TestGetOutput(t *testing.T) {
183183func TestFlattenRules (t * testing.T ) {
184184 testTime := time .Now ()
185185
186+ alerts := []v1.Alert {
187+ v1.Alert {
188+ Labels : model.LabelSet {
189+ "alertname" : "HighRequestLatency" ,
190+ "instance" : "node01" ,
191+ },
192+ },
193+ }
194+
186195 rg := []v1.RuleGroup {
187196 {
188197 Name : "example" ,
@@ -226,9 +235,9 @@ func TestFlattenRules(t *testing.T) {
226235 },
227236 }
228237
229- fr := FlattenRules (rg , nil )
230- if len (fr ) != 1 {
231- t .Error ("\n Actual: " , fr )
232- }
238+ actual := FlattenRules (rg , nil , alerts )
233239
240+ if len (actual ) != 1 {
241+ t .Error ("\n Actual: " , actual )
242+ }
234243}
You can’t perform that action at this time.
0 commit comments