Skip to content

Commit

Permalink
add type to alertingRule and recordingRule struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthaktyagi-505 committed Jul 10, 2024
1 parent 7cd1249 commit f895615
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
4 changes: 4 additions & 0 deletions api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ type AlertingRule struct {
EvaluationTime float64 `json:"evaluationTime"`
LastEvaluation time.Time `json:"lastEvaluation"`
State string `json:"state"`
Type string `json:"type"`
}

// RecordingRule models a recording rule.
Expand All @@ -612,6 +613,7 @@ type RecordingRule struct {
LastError string `json:"lastError,omitempty"`
EvaluationTime float64 `json:"evaluationTime"`
LastEvaluation time.Time `json:"lastEvaluation"`
Type string `json:"type"`
}

// Alert models an active alert.
Expand Down Expand Up @@ -721,11 +723,13 @@ func (rg *RuleGroup) UnmarshalJSON(b []byte) error {

for _, rule := range v.Rules {
alertingRule := AlertingRule{}
alertingRule.Type = string(RuleTypeAlerting)
if err := json.Unmarshal(rule, &alertingRule); err == nil {
rg.Rules = append(rg.Rules, alertingRule)
continue
}
recordingRule := RecordingRule{}
recordingRule.Type = string(RuleTypeRecording)
if err := json.Unmarshal(rule, &recordingRule); err == nil {
rg.Rules = append(rg.Rules, recordingRule)
continue
Expand Down
30 changes: 17 additions & 13 deletions api/prometheus/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,20 +717,22 @@ func TestAPIs(t *testing.T) {
"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",
"duration": 600,
"health": "ok",
"name": "HighRequestLatency",
"query": "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
"lastError": "",
"type": "alerting",
},
{
"health": "ok",
"name": "job:http_inprogress_requests:sum",
"query": "sum(http_inprogress_requests) by (job)",
"type": "recording",
"health": "ok",
"name": "job:http_inprogress_requests:sum",
"query": "sum(http_inprogress_requests) by (job)",
"lastError": "",
"type": "recording",
},
},
},
Expand Down Expand Up @@ -769,12 +771,14 @@ func TestAPIs(t *testing.T) {
Name: "HighRequestLatency",
Query: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
LastError: "",
Type: "alerting",
},
RecordingRule{
Health: RuleHealthGood,
Name: "job:http_inprogress_requests:sum",
Query: "sum(http_inprogress_requests) by (job)",
LastError: "",
Type: "recording",
},
},
},
Expand Down Expand Up @@ -861,14 +865,14 @@ func TestAPIs(t *testing.T) {
Annotations: model.LabelSet{
"summary": "High request latency",
},
Duration: 600,
Health: RuleHealthGood,
Labels: model.LabelSet{
"severity": "page",
},
Duration: 600,
Health: RuleHealthGood,
Name: "HighRequestLatency",
Query: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
LastError: "",
Type: "alerting",
EvaluationTime: 0.5,
LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC),
State: "firing",
Expand All @@ -877,7 +881,7 @@ func TestAPIs(t *testing.T) {
Health: RuleHealthGood,
Name: "job:http_inprogress_requests:sum",
Query: "sum(http_inprogress_requests) by (job)",
LastError: "",
Type: "recording",
EvaluationTime: 0.3,
LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC),
},
Expand Down

0 comments on commit f895615

Please sign in to comment.