Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 629f64a

Browse files
committed
Add newer fields to Rules API
Signed-off-by: Goutham Veeramachaneni <[email protected]>
1 parent 17e98a7 commit 629f64a

File tree

2 files changed

+146
-26
lines changed

2 files changed

+146
-26
lines changed

api/prometheus/v1/api.go

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,23 +344,28 @@ type Rules []interface{}
344344

345345
// AlertingRule models a alerting rule.
346346
type AlertingRule struct {
347-
Name string `json:"name"`
348-
Query string `json:"query"`
349-
Duration float64 `json:"duration"`
350-
Labels model.LabelSet `json:"labels"`
351-
Annotations model.LabelSet `json:"annotations"`
352-
Alerts []*Alert `json:"alerts"`
353-
Health RuleHealth `json:"health"`
354-
LastError string `json:"lastError,omitempty"`
347+
Name string `json:"name"`
348+
Query string `json:"query"`
349+
Duration float64 `json:"duration"`
350+
Labels model.LabelSet `json:"labels"`
351+
Annotations model.LabelSet `json:"annotations"`
352+
Alerts []*Alert `json:"alerts"`
353+
Health RuleHealth `json:"health"`
354+
LastError string `json:"lastError,omitempty"`
355+
EvaluationTime float64 `json:"evaluationTime"`
356+
LastEvaluation time.Time `json:"lastEvaluation"`
357+
State string `json:"state"`
355358
}
356359

357360
// RecordingRule models a recording rule.
358361
type RecordingRule struct {
359-
Name string `json:"name"`
360-
Query string `json:"query"`
361-
Labels model.LabelSet `json:"labels,omitempty"`
362-
Health RuleHealth `json:"health"`
363-
LastError string `json:"lastError,omitempty"`
362+
Name string `json:"name"`
363+
Query string `json:"query"`
364+
Labels model.LabelSet `json:"labels,omitempty"`
365+
Health RuleHealth `json:"health"`
366+
LastError string `json:"lastError,omitempty"`
367+
EvaluationTime float64 `json:"evaluationTime"`
368+
LastEvaluation time.Time `json:"lastEvaluation"`
364369
}
365370

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

482487
rule := struct {
483-
Name string `json:"name"`
484-
Query string `json:"query"`
485-
Duration float64 `json:"duration"`
486-
Labels model.LabelSet `json:"labels"`
487-
Annotations model.LabelSet `json:"annotations"`
488-
Alerts []*Alert `json:"alerts"`
489-
Health RuleHealth `json:"health"`
490-
LastError string `json:"lastError,omitempty"`
488+
Name string `json:"name"`
489+
Query string `json:"query"`
490+
Duration float64 `json:"duration"`
491+
Labels model.LabelSet `json:"labels"`
492+
Annotations model.LabelSet `json:"annotations"`
493+
Alerts []*Alert `json:"alerts"`
494+
Health RuleHealth `json:"health"`
495+
LastError string `json:"lastError,omitempty"`
496+
EvaluationTime float64 `json:"evaluationTime"`
497+
LastEvaluation time.Time `json:"lastEvaluation"`
498+
State string `json:"state"`
491499
}{}
492500
if err := json.Unmarshal(b, &rule); err != nil {
493501
return err
@@ -500,6 +508,9 @@ func (r *AlertingRule) UnmarshalJSON(b []byte) error {
500508
r.Duration = rule.Duration
501509
r.Labels = rule.Labels
502510
r.LastError = rule.LastError
511+
r.EvaluationTime = rule.EvaluationTime
512+
r.LastEvaluation = rule.LastEvaluation
513+
r.State = rule.State
503514

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

521532
rule := struct {
522-
Name string `json:"name"`
523-
Query string `json:"query"`
524-
Labels model.LabelSet `json:"labels,omitempty"`
525-
Health RuleHealth `json:"health"`
526-
LastError string `json:"lastError,omitempty"`
533+
Name string `json:"name"`
534+
Query string `json:"query"`
535+
Labels model.LabelSet `json:"labels,omitempty"`
536+
Health RuleHealth `json:"health"`
537+
LastError string `json:"lastError,omitempty"`
538+
EvaluationTime float64 `json:"evaluationTime"`
539+
LastEvaluation time.Time `json:"lastEvaluation"`
527540
}{}
528541
if err := json.Unmarshal(b, &rule); err != nil {
529542
return err
@@ -533,6 +546,8 @@ func (r *RecordingRule) UnmarshalJSON(b []byte) error {
533546
r.Name = rule.Name
534547
r.LastError = rule.LastError
535548
r.Query = rule.Query
549+
r.EvaluationTime = rule.EvaluationTime
550+
r.LastEvaluation = rule.LastEvaluation
536551

537552
return nil
538553
}

api/prometheus/v1/api_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,111 @@ func TestAPIs(t *testing.T) {
846846
},
847847
},
848848

849+
// This has the newer API elements like lastEvaluation, evaluationTime, etc.
850+
{
851+
do: doRules(),
852+
reqMethod: "GET",
853+
reqPath: "/api/v1/rules",
854+
inRes: map[string]interface{}{
855+
"groups": []map[string]interface{}{
856+
{
857+
"file": "/rules.yaml",
858+
"interval": 60,
859+
"name": "example",
860+
"rules": []map[string]interface{}{
861+
{
862+
"alerts": []map[string]interface{}{
863+
{
864+
"activeAt": testTime.UTC().Format(time.RFC3339Nano),
865+
"annotations": map[string]interface{}{
866+
"summary": "High request latency",
867+
},
868+
"labels": map[string]interface{}{
869+
"alertname": "HighRequestLatency",
870+
"severity": "page",
871+
},
872+
"state": "firing",
873+
"value": "1e+00",
874+
},
875+
},
876+
"annotations": map[string]interface{}{
877+
"summary": "High request latency",
878+
},
879+
"duration": 600,
880+
"health": "ok",
881+
"labels": map[string]interface{}{
882+
"severity": "page",
883+
},
884+
"name": "HighRequestLatency",
885+
"query": "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
886+
"type": "alerting",
887+
"evaluationTime": 0.5,
888+
"lastEvaluation": "2020-05-18T15:52:53.4503113Z",
889+
"state": "firing",
890+
},
891+
{
892+
"health": "ok",
893+
"name": "job:http_inprogress_requests:sum",
894+
"query": "sum(http_inprogress_requests) by (job)",
895+
"type": "recording",
896+
"evaluationTime": 0.3,
897+
"lastEvaluation": "2020-05-18T15:52:53.4503113Z",
898+
},
899+
},
900+
},
901+
},
902+
},
903+
res: RulesResult{
904+
Groups: []RuleGroup{
905+
{
906+
Name: "example",
907+
File: "/rules.yaml",
908+
Interval: 60,
909+
Rules: []interface{}{
910+
AlertingRule{
911+
Alerts: []*Alert{
912+
{
913+
ActiveAt: testTime.UTC(),
914+
Annotations: model.LabelSet{
915+
"summary": "High request latency",
916+
},
917+
Labels: model.LabelSet{
918+
"alertname": "HighRequestLatency",
919+
"severity": "page",
920+
},
921+
State: AlertStateFiring,
922+
Value: "1e+00",
923+
},
924+
},
925+
Annotations: model.LabelSet{
926+
"summary": "High request latency",
927+
},
928+
Labels: model.LabelSet{
929+
"severity": "page",
930+
},
931+
Duration: 600,
932+
Health: RuleHealthGood,
933+
Name: "HighRequestLatency",
934+
Query: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
935+
LastError: "",
936+
EvaluationTime: 0.5,
937+
LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC),
938+
State: "firing",
939+
},
940+
RecordingRule{
941+
Health: RuleHealthGood,
942+
Name: "job:http_inprogress_requests:sum",
943+
Query: "sum(http_inprogress_requests) by (job)",
944+
LastError: "",
945+
EvaluationTime: 0.3,
946+
LastEvaluation: time.Date(2020, 5, 18, 15, 52, 53, 450311300, time.UTC),
947+
},
948+
},
949+
},
950+
},
951+
},
952+
},
953+
849954
{
850955
do: doRules(),
851956
reqMethod: "GET",

0 commit comments

Comments
 (0)