Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule component: Add a new API /api/v1/alerts/:state for pull alerts from Thanos Rule #901

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkg/rule/api/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (api *API) Register(r *route.Router, tracer opentracing.Tracer, logger log.
}

r.Get("/alerts", instr("alerts", api.alerts))
r.Get("/alerts/:state", instr("alerts", api.stateAlerts))
r.Get("/rules", instr("rules", api.rules))

}
Expand Down Expand Up @@ -113,6 +114,7 @@ func (api *API) rules(r *http.Request) (interface{}, []error, *qapi.ApiError) {
}

func (api *API) alerts(r *http.Request) (interface{}, []error, *qapi.ApiError) {

alertingRules := api.rulesRetriever.AlertingRules()
alerts := []*Alert{}

Expand All @@ -128,6 +130,27 @@ func (api *API) alerts(r *http.Request) (interface{}, []error, *qapi.ApiError) {
return res, nil, nil
}

func (api *API) stateAlerts(r *http.Request) (interface{}, []error, *qapi.ApiError) {
ctx := r.Context()
state := route.Param(ctx, "state")

alertingRules := api.rulesRetriever.AlertingRules()
alerts := []*Alert{}

for _, alertingRule := range alertingRules {
stateAlerts := rulesAlertsToAPIAlerts(alertingRule.ActiveAlerts())
for _, a := range stateAlerts {
if a.State == state {
alerts = append(alerts, a)
}
}
}

res := &AlertDiscovery{Alerts: alerts}

return res, nil, nil
}

type AlertDiscovery struct {
Alerts []*Alert `json:"alerts"`
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/rule/api/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ func testEndpoints(t *testing.T, api *API) {
},
},
},
{
endpoint: api.alerts,
response: &AlertDiscovery{Alerts: []*Alert{}},
},
{
endpoint: api.stateAlerts,
params: map[string]string{"state": "firing"},
response: &AlertDiscovery{Alerts: []*Alert{}},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add proper tests mock some firing alerts and check that they are returned currectly

},
}

methods := func(f qapi.ApiFunc) []string {
Expand Down