Skip to content

Commit 84d3a62

Browse files
committed
Include keepFiringFor and keepFiringSince in API response
Signed-off-by: Mustafain Ali Khan <[email protected]>
1 parent 1a28bf0 commit 84d3a62

File tree

5 files changed

+159
-86
lines changed

5 files changed

+159
-86
lines changed

pkg/ruler/api.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ type AlertDiscovery struct {
4646

4747
// Alert has info for an alert.
4848
type Alert struct {
49-
Labels labels.Labels `json:"labels"`
50-
Annotations labels.Labels `json:"annotations"`
51-
State string `json:"state"`
52-
ActiveAt *time.Time `json:"activeAt"`
53-
Value string `json:"value"`
49+
Labels labels.Labels `json:"labels"`
50+
Annotations labels.Labels `json:"annotations"`
51+
State string `json:"state"`
52+
ActiveAt *time.Time `json:"activeAt"`
53+
KeepFiringSince *time.Time `json:"keepFiringSince"`
54+
Value string `json:"value"`
5455
}
5556

5657
// RuleDiscovery has info for all rules
@@ -80,6 +81,7 @@ type alertingRule struct {
8081
Name string `json:"name"`
8182
Query string `json:"query"`
8283
Duration float64 `json:"duration"`
84+
KeepFiringFor float64 `json:"keepFiringFor"`
8385
Labels labels.Labels `json:"labels"`
8486
Annotations labels.Labels `json:"annotations"`
8587
Alerts []*Alert `json:"alerts"`
@@ -211,13 +213,17 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
211213
if g.ActiveRules[i].Rule.Alert != "" {
212214
alerts := make([]*Alert, 0, len(rl.Alerts))
213215
for _, a := range rl.Alerts {
214-
alerts = append(alerts, &Alert{
216+
alert := &Alert{
215217
Labels: cortexpb.FromLabelAdaptersToLabels(a.Labels),
216218
Annotations: cortexpb.FromLabelAdaptersToLabels(a.Annotations),
217219
State: a.GetState(),
218220
ActiveAt: &a.ActiveAt,
219221
Value: strconv.FormatFloat(a.Value, 'e', -1, 64),
220-
})
222+
}
223+
if !a.KeepFiringSince.IsZero() {
224+
alert.KeepFiringSince = &a.KeepFiringSince
225+
}
226+
alerts = append(alerts, alert)
221227
}
222228
grp.Rules[i] = alertingRule{
223229
State: rl.GetState(),
@@ -232,6 +238,7 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
232238
LastEvaluation: rl.GetEvaluationTimestamp(),
233239
EvaluationTime: rl.GetEvaluationDuration().Seconds(),
234240
Type: v1.RuleTypeAlerting,
241+
KeepFiringFor: rl.Rule.KeepFiringFor.Seconds(),
235242
}
236243
} else {
237244
grp.Rules[i] = recordingRule{
@@ -296,13 +303,17 @@ func (a *API) PrometheusAlerts(w http.ResponseWriter, req *http.Request) {
296303
for _, rl := range g.ActiveRules {
297304
if rl.Rule.Alert != "" {
298305
for _, a := range rl.Alerts {
299-
alerts = append(alerts, &Alert{
306+
alert := &Alert{
300307
Labels: cortexpb.FromLabelAdaptersToLabels(a.Labels),
301308
Annotations: cortexpb.FromLabelAdaptersToLabels(a.Annotations),
302309
State: a.GetState(),
303310
ActiveAt: &a.ActiveAt,
304311
Value: strconv.FormatFloat(a.Value, 'e', -1, 64),
305-
})
312+
}
313+
if !a.KeepFiringSince.IsZero() {
314+
alert.KeepFiringSince = &a.KeepFiringSince
315+
}
316+
alerts = append(alerts, alert)
306317
}
307318
}
308319
}

pkg/ruler/ruler.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -829,15 +829,16 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest) ([]*Grou
829829
alerts := []*AlertStateDesc{}
830830
for _, a := range rule.ActiveAlerts() {
831831
alerts = append(alerts, &AlertStateDesc{
832-
State: a.State.String(),
833-
Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels),
834-
Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations),
835-
Value: a.Value,
836-
ActiveAt: a.ActiveAt,
837-
FiredAt: a.FiredAt,
838-
ResolvedAt: a.ResolvedAt,
839-
LastSentAt: a.LastSentAt,
840-
ValidUntil: a.ValidUntil,
832+
State: a.State.String(),
833+
Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels),
834+
Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations),
835+
Value: a.Value,
836+
ActiveAt: a.ActiveAt,
837+
FiredAt: a.FiredAt,
838+
ResolvedAt: a.ResolvedAt,
839+
LastSentAt: a.LastSentAt,
840+
ValidUntil: a.ValidUntil,
841+
KeepFiringSince: a.KeepFiringSince,
841842
})
842843
}
843844
ruleDesc = &RuleStateDesc{

0 commit comments

Comments
 (0)