Skip to content

Commit

Permalink
Merge pull request #855 from gouthamve/add-new-elements-to-rules
Browse files Browse the repository at this point in the history
Add newer fields to Rules API
  • Loading branch information
beorn7 committed Apr 12, 2021
2 parents 17e98a7 + 629f64a commit 28ea8c2
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 26 deletions.
67 changes: 41 additions & 26 deletions api/prometheus/v1/api.go
Expand Up @@ -344,23 +344,28 @@ type Rules []interface{}

// AlertingRule models a alerting rule.
type AlertingRule struct {
Name string `json:"name"`
Query string `json:"query"`
Duration float64 `json:"duration"`
Labels model.LabelSet `json:"labels"`
Annotations model.LabelSet `json:"annotations"`
Alerts []*Alert `json:"alerts"`
Health RuleHealth `json:"health"`
LastError string `json:"lastError,omitempty"`
Name string `json:"name"`
Query string `json:"query"`
Duration float64 `json:"duration"`
Labels model.LabelSet `json:"labels"`
Annotations model.LabelSet `json:"annotations"`
Alerts []*Alert `json:"alerts"`
Health RuleHealth `json:"health"`
LastError string `json:"lastError,omitempty"`
EvaluationTime float64 `json:"evaluationTime"`
LastEvaluation time.Time `json:"lastEvaluation"`
State string `json:"state"`
}

// RecordingRule models a recording rule.
type RecordingRule struct {
Name string `json:"name"`
Query string `json:"query"`
Labels model.LabelSet `json:"labels,omitempty"`
Health RuleHealth `json:"health"`
LastError string `json:"lastError,omitempty"`
Name string `json:"name"`
Query string `json:"query"`
Labels model.LabelSet `json:"labels,omitempty"`
Health RuleHealth `json:"health"`
LastError string `json:"lastError,omitempty"`
EvaluationTime float64 `json:"evaluationTime"`
LastEvaluation time.Time `json:"lastEvaluation"`
}

// Alert models an active alert.
Expand Down Expand Up @@ -480,14 +485,17 @@ func (r *AlertingRule) UnmarshalJSON(b []byte) error {
}

rule := struct {
Name string `json:"name"`
Query string `json:"query"`
Duration float64 `json:"duration"`
Labels model.LabelSet `json:"labels"`
Annotations model.LabelSet `json:"annotations"`
Alerts []*Alert `json:"alerts"`
Health RuleHealth `json:"health"`
LastError string `json:"lastError,omitempty"`
Name string `json:"name"`
Query string `json:"query"`
Duration float64 `json:"duration"`
Labels model.LabelSet `json:"labels"`
Annotations model.LabelSet `json:"annotations"`
Alerts []*Alert `json:"alerts"`
Health RuleHealth `json:"health"`
LastError string `json:"lastError,omitempty"`
EvaluationTime float64 `json:"evaluationTime"`
LastEvaluation time.Time `json:"lastEvaluation"`
State string `json:"state"`
}{}
if err := json.Unmarshal(b, &rule); err != nil {
return err
Expand All @@ -500,6 +508,9 @@ func (r *AlertingRule) UnmarshalJSON(b []byte) error {
r.Duration = rule.Duration
r.Labels = rule.Labels
r.LastError = rule.LastError
r.EvaluationTime = rule.EvaluationTime
r.LastEvaluation = rule.LastEvaluation
r.State = rule.State

return nil
}
Expand All @@ -519,11 +530,13 @@ func (r *RecordingRule) UnmarshalJSON(b []byte) error {
}

rule := struct {
Name string `json:"name"`
Query string `json:"query"`
Labels model.LabelSet `json:"labels,omitempty"`
Health RuleHealth `json:"health"`
LastError string `json:"lastError,omitempty"`
Name string `json:"name"`
Query string `json:"query"`
Labels model.LabelSet `json:"labels,omitempty"`
Health RuleHealth `json:"health"`
LastError string `json:"lastError,omitempty"`
EvaluationTime float64 `json:"evaluationTime"`
LastEvaluation time.Time `json:"lastEvaluation"`
}{}
if err := json.Unmarshal(b, &rule); err != nil {
return err
Expand All @@ -533,6 +546,8 @@ func (r *RecordingRule) UnmarshalJSON(b []byte) error {
r.Name = rule.Name
r.LastError = rule.LastError
r.Query = rule.Query
r.EvaluationTime = rule.EvaluationTime
r.LastEvaluation = rule.LastEvaluation

return nil
}
Expand Down
105 changes: 105 additions & 0 deletions api/prometheus/v1/api_test.go
Expand Up @@ -846,6 +846,111 @@ func TestAPIs(t *testing.T) {
},
},

// This has the newer API elements like lastEvaluation, evaluationTime, etc.
{
do: doRules(),
reqMethod: "GET",
reqPath: "/api/v1/rules",
inRes: map[string]interface{}{
"groups": []map[string]interface{}{
{
"file": "/rules.yaml",
"interval": 60,
"name": "example",
"rules": []map[string]interface{}{
{
"alerts": []map[string]interface{}{
{
"activeAt": testTime.UTC().Format(time.RFC3339Nano),
"annotations": map[string]interface{}{
"summary": "High request latency",
},
"labels": map[string]interface{}{
"alertname": "HighRequestLatency",
"severity": "page",
},
"state": "firing",
"value": "1e+00",
},
},
"annotations": map[string]interface{}{
"summary": "High request latency",
},
"duration": 600,
"health": "ok",
"labels": map[string]interface{}{
"severity": "page",
},
"name": "HighRequestLatency",
"query": "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
"type": "alerting",
"evaluationTime": 0.5,
"lastEvaluation": "2020-05-18T15:52:53.4503113Z",
"state": "firing",
},
{
"health": "ok",
"name": "job:http_inprogress_requests:sum",
"query": "sum(http_inprogress_requests) by (job)",
"type": "recording",
"evaluationTime": 0.3,
"lastEvaluation": "2020-05-18T15:52:53.4503113Z",
},
},
},
},
},
res: RulesResult{
Groups: []RuleGroup{
{
Name: "example",
File: "/rules.yaml",
Interval: 60,
Rules: []interface{}{
AlertingRule{
Alerts: []*Alert{
{
ActiveAt: testTime.UTC(),
Annotations: model.LabelSet{
"summary": "High request latency",
},
Labels: model.LabelSet{
"alertname": "HighRequestLatency",
"severity": "page",
},
State: AlertStateFiring,
Value: "1e+00",
},
},
Annotations: model.LabelSet{
"summary": "High request latency",
},
Labels: model.LabelSet{
"severity": "page",
},
Duration: 600,
Health: RuleHealthGood,
Name: "HighRequestLatency",
Query: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
LastError: "",
EvaluationTime: 0.5,
LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC),
State: "firing",
},
RecordingRule{
Health: RuleHealthGood,
Name: "job:http_inprogress_requests:sum",
Query: "sum(http_inprogress_requests) by (job)",
LastError: "",
EvaluationTime: 0.3,
LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC),
},
},
},
},
},
},

{
do: doRules(),
reqMethod: "GET",
Expand Down

0 comments on commit 28ea8c2

Please sign in to comment.