Skip to content

Commit

Permalink
Include keepFiringFor and keepFiringSince in API response
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafain Ali Khan <mustalik@amazon.com>
  • Loading branch information
mustafain117 committed Mar 21, 2024
1 parent 1a28bf0 commit 84d3a62
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 86 deletions.
29 changes: 20 additions & 9 deletions pkg/ruler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ type AlertDiscovery struct {

// Alert has info for an alert.
type Alert struct {
Labels labels.Labels `json:"labels"`
Annotations labels.Labels `json:"annotations"`
State string `json:"state"`
ActiveAt *time.Time `json:"activeAt"`
Value string `json:"value"`
Labels labels.Labels `json:"labels"`
Annotations labels.Labels `json:"annotations"`
State string `json:"state"`
ActiveAt *time.Time `json:"activeAt"`
KeepFiringSince *time.Time `json:"keepFiringSince"`
Value string `json:"value"`
}

// RuleDiscovery has info for all rules
Expand Down Expand Up @@ -80,6 +81,7 @@ type alertingRule struct {
Name string `json:"name"`
Query string `json:"query"`
Duration float64 `json:"duration"`
KeepFiringFor float64 `json:"keepFiringFor"`
Labels labels.Labels `json:"labels"`
Annotations labels.Labels `json:"annotations"`
Alerts []*Alert `json:"alerts"`
Expand Down Expand Up @@ -211,13 +213,17 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
if g.ActiveRules[i].Rule.Alert != "" {
alerts := make([]*Alert, 0, len(rl.Alerts))
for _, a := range rl.Alerts {
alerts = append(alerts, &Alert{
alert := &Alert{
Labels: cortexpb.FromLabelAdaptersToLabels(a.Labels),
Annotations: cortexpb.FromLabelAdaptersToLabels(a.Annotations),
State: a.GetState(),
ActiveAt: &a.ActiveAt,
Value: strconv.FormatFloat(a.Value, 'e', -1, 64),
})
}
if !a.KeepFiringSince.IsZero() {
alert.KeepFiringSince = &a.KeepFiringSince
}
alerts = append(alerts, alert)
}
grp.Rules[i] = alertingRule{
State: rl.GetState(),
Expand All @@ -232,6 +238,7 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
LastEvaluation: rl.GetEvaluationTimestamp(),
EvaluationTime: rl.GetEvaluationDuration().Seconds(),
Type: v1.RuleTypeAlerting,
KeepFiringFor: rl.Rule.KeepFiringFor.Seconds(),
}
} else {
grp.Rules[i] = recordingRule{
Expand Down Expand Up @@ -296,13 +303,17 @@ func (a *API) PrometheusAlerts(w http.ResponseWriter, req *http.Request) {
for _, rl := range g.ActiveRules {
if rl.Rule.Alert != "" {
for _, a := range rl.Alerts {
alerts = append(alerts, &Alert{
alert := &Alert{
Labels: cortexpb.FromLabelAdaptersToLabels(a.Labels),
Annotations: cortexpb.FromLabelAdaptersToLabels(a.Annotations),
State: a.GetState(),
ActiveAt: &a.ActiveAt,
Value: strconv.FormatFloat(a.Value, 'e', -1, 64),
})
}
if !a.KeepFiringSince.IsZero() {
alert.KeepFiringSince = &a.KeepFiringSince
}
alerts = append(alerts, alert)
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions pkg/ruler/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,15 +829,16 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest) ([]*Grou
alerts := []*AlertStateDesc{}
for _, a := range rule.ActiveAlerts() {
alerts = append(alerts, &AlertStateDesc{
State: a.State.String(),
Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels),
Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations),
Value: a.Value,
ActiveAt: a.ActiveAt,
FiredAt: a.FiredAt,
ResolvedAt: a.ResolvedAt,
LastSentAt: a.LastSentAt,
ValidUntil: a.ValidUntil,
State: a.State.String(),
Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels),
Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations),
Value: a.Value,
ActiveAt: a.ActiveAt,
FiredAt: a.FiredAt,
ResolvedAt: a.ResolvedAt,
LastSentAt: a.LastSentAt,
ValidUntil: a.ValidUntil,
KeepFiringSince: a.KeepFiringSince,
})
}
ruleDesc = &RuleStateDesc{
Expand Down
Loading

0 comments on commit 84d3a62

Please sign in to comment.