Skip to content

Commit

Permalink
feat: update /api/v1/{rules,alerts} responses
Browse files Browse the repository at this point in the history
This commit adds the fields supported by newer Prometheus versions:

* /api/v1/rules
  * `state` (for alerting rules only)
  * `keepFiringFor` (for alerting rules only)
  * `evaluationTime`
  * `lastEvaluation`
* /api/v1/alerts
  * `keepFiringSince`

Signed-off-by: Simon Pasquier <[email protected]>
  • Loading branch information
simonpasquier committed Apr 29, 2024
1 parent 88509c8 commit 6a7bd93
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 32 deletions.
43 changes: 25 additions & 18 deletions injectproxy/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,30 @@ func (r *rule) UnmarshalJSON(b []byte) error {
}

type alertingRule struct {
Name string `json:"name"`
Query string `json:"query"`
Duration float64 `json:"duration"`
Labels labels.Labels `json:"labels"`
Annotations labels.Labels `json:"annotations"`
Alerts []*alert `json:"alerts"`
Health string `json:"health"`
LastError string `json:"lastError,omitempty"`
State string `json:"state"`
Name string `json:"name"`
Query string `json:"query"`
Duration float64 `json:"duration"`
KeepFiringFor float64 `json:"keepFiringFor"`
Labels labels.Labels `json:"labels"`
Annotations labels.Labels `json:"annotations"`
Alerts []*alert `json:"alerts"`
Health string `json:"health"`
LastError string `json:"lastError,omitempty"`
EvaluationTime float64 `json:"evaluationTime"`
LastEvaluation time.Time `json:"lastEvaluation"`
// Type of an alertingRule is always "alerting".
Type string `json:"type"`
}

type recordingRule struct {
Name string `json:"name"`
Query string `json:"query"`
Labels labels.Labels `json:"labels,omitempty"`
Health string `json:"health"`
LastError string `json:"lastError,omitempty"`
Name string `json:"name"`
Query string `json:"query"`
Labels labels.Labels `json:"labels,omitempty"`
Health string `json:"health"`
LastError string `json:"lastError,omitempty"`
EvaluationTime float64 `json:"evaluationTime"`
LastEvaluation time.Time `json:"lastEvaluation"`
// Type of a recordingRule is always "recording".
Type string `json:"type"`
}
Expand All @@ -153,11 +159,12 @@ type alertsData struct {
}

type alert struct {
Labels labels.Labels `json:"labels"`
Annotations labels.Labels `json:"annotations"`
State string `json:"state"`
ActiveAt *time.Time `json:"activeAt,omitempty"`
Value string `json:"value"`
Labels labels.Labels `json:"labels"`
Annotations labels.Labels `json:"annotations"`
State string `json:"state"`
ActiveAt *time.Time `json:"activeAt,omitempty"`
KeepFiringSince *time.Time `json:"keepFiringSince,omitempty"`
Value string `json:"value"`
}

// modifyAPIResponse unwraps the Prometheus API response, passes the enforced
Expand Down
61 changes: 47 additions & 14 deletions injectproxy/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func validRules() http.Handler {
"namespace": "ns1"
},
"health": "ok",
"type": "recording"
"type": "recording",
"evaluationTime": 0.000214303,
"lastEvaluation": "2024-04-29T14:23:52.403557247+02:00"
},
{
"name": "metric2",
Expand All @@ -74,7 +76,9 @@ func validRules() http.Handler {
"operation": "create"
},
"health": "ok",
"type": "recording"
"type": "recording",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.403557247+02:00"
},
{
"name": "metric2",
Expand All @@ -84,7 +88,9 @@ func validRules() http.Handler {
"operation": "update"
},
"health": "ok",
"type": "recording"
"type": "recording",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:54.403557247+02:00"
},
{
"name": "metric2",
Expand All @@ -94,9 +100,12 @@ func validRules() http.Handler {
"operation": "delete"
},
"health": "ok",
"type": "recording"
"type": "recording",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.603557247+02:00"
},
{
"state": "firing",
"name": "Alert1",
"query": "metric1{namespace=\"ns1\"} == 0",
"duration": 0,
Expand All @@ -117,9 +126,12 @@ func validRules() http.Handler {
}
],
"health": "ok",
"type": "alerting"
"type": "alerting",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.803557247+02:00"
},
{
"state": "firing",
"name": "Alert2",
"query": "metric2{namespace=\"ns1\"} == 0",
"duration": 0,
Expand Down Expand Up @@ -152,7 +164,9 @@ func validRules() http.Handler {
}
],
"health": "ok",
"type": "alerting"
"type": "alerting",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.903557247+02:00"
}
],
"interval": 10
Expand All @@ -168,9 +182,12 @@ func validRules() http.Handler {
"namespace": "ns2"
},
"health": "ok",
"type": "recording"
"type": "recording",
"evaluationTime": 0.000214303,
"lastEvaluation": "2024-04-29T14:23:52.403557247+02:00"
},
{
"state": "inactive",
"name": "Alert1",
"query": "metric1{namespace=\"ns2\"} == 0",
"duration": 0,
Expand All @@ -180,7 +197,9 @@ func validRules() http.Handler {
"annotations": {},
"alerts": [],
"health": "ok",
"type": "alerting"
"type": "alerting",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.503557247+02:00"
}
],
"interval": 10
Expand All @@ -197,7 +216,9 @@ func validRules() http.Handler {
"operation": "create"
},
"health": "ok",
"type": "recording"
"type": "recording",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.503557247+02:00"
},
{
"name": "metric2",
Expand All @@ -207,7 +228,9 @@ func validRules() http.Handler {
"operation": "update"
},
"health": "ok",
"type": "recording"
"type": "recording",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.603557247+02:00"
},
{
"name": "metric2",
Expand All @@ -217,7 +240,9 @@ func validRules() http.Handler {
"operation": "delete"
},
"health": "ok",
"type": "recording"
"type": "recording",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.643557247+02:00"
},
{
"name": "metric3",
Expand All @@ -226,9 +251,12 @@ func validRules() http.Handler {
"namespace": "ns2"
},
"health": "ok",
"type": "recording"
"type": "recording",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.683557247+02:00"
},
{
"state": "inactive",
"name": "Alert2",
"query": "metric2{namespace=\"ns2\"} == 0",
"duration": 0,
Expand All @@ -238,9 +266,12 @@ func validRules() http.Handler {
"annotations": {},
"alerts": [],
"health": "ok",
"type": "alerting"
"type": "alerting",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.803557247+02:00"
},
{
"state": "firing",
"name": "Alert3",
"query": "metric3{namespace=\"ns2\"} == 0",
"duration": 0,
Expand All @@ -261,7 +292,9 @@ func validRules() http.Handler {
}
],
"health": "ok",
"type": "alerting"
"type": "alerting",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.903557247+02:00"
}
],
"interval": 10
Expand Down
16 changes: 16 additions & 0 deletions injectproxy/testdata/rules_match_namespace_ns1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"namespace": "ns1"
},
"health": "ok",
"evaluationTime": 0.000214303,
"lastEvaluation": "2024-04-29T14:23:52.403557247+02:00",
"type": "recording"
},
{
Expand All @@ -23,6 +25,8 @@
"operation": "create"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.403557247+02:00",
"type": "recording"
},
{
Expand All @@ -33,6 +37,8 @@
"operation": "update"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:54.403557247+02:00",
"type": "recording"
},
{
Expand All @@ -43,12 +49,16 @@
"operation": "delete"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.603557247+02:00",
"type": "recording"
},
{
"state": "firing",
"name": "Alert1",
"query": "metric1{namespace=\"ns1\"} == 0",
"duration": 0,
"keepFiringFor": 0,
"labels": {
"namespace": "ns1"
},
Expand All @@ -66,12 +76,16 @@
}
],
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.803557247+02:00",
"type": "alerting"
},
{
"state": "firing",
"name": "Alert2",
"query": "metric2{namespace=\"ns1\"} == 0",
"duration": 0,
"keepFiringFor": 0,
"labels": {
"namespace": "ns1"
},
Expand Down Expand Up @@ -101,6 +115,8 @@
}
],
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:53.903557247+02:00",
"type": "alerting"
}
],
Expand Down
22 changes: 22 additions & 0 deletions injectproxy/testdata/rules_match_namespace_ns2.golden
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@
"namespace": "ns2"
},
"health": "ok",
"evaluationTime": 0.000214303,
"lastEvaluation": "2024-04-29T14:23:52.403557247+02:00",
"type": "recording"
},
{
"state": "inactive",
"name": "Alert1",
"query": "metric1{namespace=\"ns2\"} == 0",
"duration": 0,
"keepFiringFor": 0,
"labels": {
"namespace": "ns2"
},
"annotations": {},
"alerts": [],
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.503557247+02:00",
"type": "alerting"
}
],
Expand All @@ -42,6 +48,8 @@
"operation": "create"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.503557247+02:00",
"type": "recording"
},
{
Expand All @@ -52,6 +60,8 @@
"operation": "update"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.603557247+02:00",
"type": "recording"
},
{
Expand All @@ -62,6 +72,8 @@
"operation": "delete"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.643557247+02:00",
"type": "recording"
},
{
Expand All @@ -71,24 +83,32 @@
"namespace": "ns2"
},
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.683557247+02:00",
"type": "recording"
},
{
"state": "inactive",
"name": "Alert2",
"query": "metric2{namespace=\"ns2\"} == 0",
"duration": 0,
"keepFiringFor": 0,
"labels": {
"namespace": "ns2"
},
"annotations": {},
"alerts": [],
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.803557247+02:00",
"type": "alerting"
},
{
"state": "firing",
"name": "Alert3",
"query": "metric3{namespace=\"ns2\"} == 0",
"duration": 0,
"keepFiringFor": 0,
"labels": {
"namespace": "ns2"
},
Expand All @@ -106,6 +126,8 @@
}
],
"health": "ok",
"evaluationTime": 0.000214,
"lastEvaluation": "2024-04-29T14:23:52.903557247+02:00",
"type": "alerting"
}
],
Expand Down
Loading

0 comments on commit 6a7bd93

Please sign in to comment.