Skip to content

Commit f895615

Browse files
add type to alertingRule and recordingRule struct
1 parent 7cd1249 commit f895615

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

api/prometheus/v1/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ type AlertingRule struct {
601601
EvaluationTime float64 `json:"evaluationTime"`
602602
LastEvaluation time.Time `json:"lastEvaluation"`
603603
State string `json:"state"`
604+
Type string `json:"type"`
604605
}
605606

606607
// RecordingRule models a recording rule.
@@ -612,6 +613,7 @@ type RecordingRule struct {
612613
LastError string `json:"lastError,omitempty"`
613614
EvaluationTime float64 `json:"evaluationTime"`
614615
LastEvaluation time.Time `json:"lastEvaluation"`
616+
Type string `json:"type"`
615617
}
616618

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

722724
for _, rule := range v.Rules {
723725
alertingRule := AlertingRule{}
726+
alertingRule.Type = string(RuleTypeAlerting)
724727
if err := json.Unmarshal(rule, &alertingRule); err == nil {
725728
rg.Rules = append(rg.Rules, alertingRule)
726729
continue
727730
}
728731
recordingRule := RecordingRule{}
732+
recordingRule.Type = string(RuleTypeRecording)
729733
if err := json.Unmarshal(rule, &recordingRule); err == nil {
730734
rg.Rules = append(rg.Rules, recordingRule)
731735
continue

api/prometheus/v1/api_test.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -717,20 +717,22 @@ func TestAPIs(t *testing.T) {
717717
"annotations": map[string]interface{}{
718718
"summary": "High request latency",
719719
},
720-
"duration": 600,
721-
"health": "ok",
722720
"labels": map[string]interface{}{
723721
"severity": "page",
724722
},
725-
"name": "HighRequestLatency",
726-
"query": "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
727-
"type": "alerting",
723+
"duration": 600,
724+
"health": "ok",
725+
"name": "HighRequestLatency",
726+
"query": "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
727+
"lastError": "",
728+
"type": "alerting",
728729
},
729730
{
730-
"health": "ok",
731-
"name": "job:http_inprogress_requests:sum",
732-
"query": "sum(http_inprogress_requests) by (job)",
733-
"type": "recording",
731+
"health": "ok",
732+
"name": "job:http_inprogress_requests:sum",
733+
"query": "sum(http_inprogress_requests) by (job)",
734+
"lastError": "",
735+
"type": "recording",
734736
},
735737
},
736738
},
@@ -769,12 +771,14 @@ func TestAPIs(t *testing.T) {
769771
Name: "HighRequestLatency",
770772
Query: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
771773
LastError: "",
774+
Type: "alerting",
772775
},
773776
RecordingRule{
774777
Health: RuleHealthGood,
775778
Name: "job:http_inprogress_requests:sum",
776779
Query: "sum(http_inprogress_requests) by (job)",
777780
LastError: "",
781+
Type: "recording",
778782
},
779783
},
780784
},
@@ -861,14 +865,14 @@ func TestAPIs(t *testing.T) {
861865
Annotations: model.LabelSet{
862866
"summary": "High request latency",
863867
},
868+
Duration: 600,
869+
Health: RuleHealthGood,
864870
Labels: model.LabelSet{
865871
"severity": "page",
866872
},
867-
Duration: 600,
868-
Health: RuleHealthGood,
869873
Name: "HighRequestLatency",
870874
Query: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
871-
LastError: "",
875+
Type: "alerting",
872876
EvaluationTime: 0.5,
873877
LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC),
874878
State: "firing",
@@ -877,7 +881,7 @@ func TestAPIs(t *testing.T) {
877881
Health: RuleHealthGood,
878882
Name: "job:http_inprogress_requests:sum",
879883
Query: "sum(http_inprogress_requests) by (job)",
880-
LastError: "",
884+
Type: "recording",
881885
EvaluationTime: 0.3,
882886
LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC),
883887
},

0 commit comments

Comments
 (0)