From c302ac5d8fcc26995ffec9325a0847e9842455d9 Mon Sep 17 00:00:00 2001 From: "sarthak.tyagi" Date: Wed, 10 Jul 2024 10:24:37 +0100 Subject: [PATCH 1/2] add type to alertingRule and recordingRule struct Signed-off-by: sarthak.tyagi --- api/prometheus/v1/api.go | 4 ++++ api/prometheus/v1/api_test.go | 30 +++++++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index cddf027..089c3f6 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -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. @@ -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. @@ -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 diff --git a/api/prometheus/v1/api_test.go b/api/prometheus/v1/api_test.go index 01ddf45..cedea06 100644 --- a/api/prometheus/v1/api_test.go +++ b/api/prometheus/v1/api_test.go @@ -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", }, }, }, @@ -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", }, }, }, @@ -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", @@ -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), }, From 94e83197caf5d4b16d7df42e3113163452ec3138 Mon Sep 17 00:00:00 2001 From: "sarthak.tyagi" Date: Wed, 10 Jul 2024 10:30:32 +0100 Subject: [PATCH 2/2] use const instead of recording and alerting string in api_test.go Signed-off-by: sarthak.tyagi --- api/prometheus/v1/api_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/prometheus/v1/api_test.go b/api/prometheus/v1/api_test.go index cedea06..dcd74e6 100644 --- a/api/prometheus/v1/api_test.go +++ b/api/prometheus/v1/api_test.go @@ -771,14 +771,14 @@ func TestAPIs(t *testing.T) { Name: "HighRequestLatency", Query: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5", LastError: "", - Type: "alerting", + Type: string(RuleTypeAlerting), }, RecordingRule{ Health: RuleHealthGood, Name: "job:http_inprogress_requests:sum", Query: "sum(http_inprogress_requests) by (job)", LastError: "", - Type: "recording", + Type: string(RuleTypeRecording), }, }, }, @@ -872,7 +872,7 @@ func TestAPIs(t *testing.T) { }, Name: "HighRequestLatency", Query: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5", - Type: "alerting", + Type: string(RuleTypeAlerting), EvaluationTime: 0.5, LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC), State: "firing", @@ -881,7 +881,7 @@ func TestAPIs(t *testing.T) { Health: RuleHealthGood, Name: "job:http_inprogress_requests:sum", Query: "sum(http_inprogress_requests) by (job)", - Type: "recording", + Type: string(RuleTypeRecording), EvaluationTime: 0.3, LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC), },