Add alerts endpoint to API client (#552)

Add alerts endpoint to API client. The alerts endpoint returns a list of all active alerts.

Signed-off-by: Roald Nefs <info@roaldnefs.com>
This commit is contained in:
Roald Nefs 2019-03-25 09:23:28 +01:00 committed by Krasi Georgiev
parent fa4aa9000d
commit 7490f0a745
1 changed files with 26 additions and 0 deletions

View File

@ -35,6 +35,7 @@ const (
apiPrefix = "/api/v1"
epAlerts = apiPrefix + "/alerts"
epAlertManagers = apiPrefix + "/alertmanagers"
epQuery = apiPrefix + "/query"
epQueryRange = apiPrefix + "/query_range"
@ -115,6 +116,8 @@ type Range struct {
// API provides bindings for Prometheus's v1 API.
type API interface {
// Alerts returns a list of all active alerts.
Alerts(ctx context.Context) (AlertsResult, error)
// AlertManagers returns an overview of the current state of the Prometheus alert manager discovery.
AlertManagers(ctx context.Context) (AlertManagersResult, error)
// CleanTombstones removes the deleted data from disk and cleans up the existing tombstones.
@ -142,6 +145,11 @@ type API interface {
Targets(ctx context.Context) (TargetsResult, error)
}
// AlertsResult contains the result from querying the alerts endpoint.
type AlertsResult struct {
Alerts []Alert `json:"alerts"`
}
// AlertManagersResult contains the result from querying the alertmanagers endpoint.
type AlertManagersResult struct {
Active []AlertManager `json:"activeAlertManagers"`
@ -402,6 +410,24 @@ type httpAPI struct {
client api.Client
}
func (h *httpAPI) Alerts(ctx context.Context) (AlertsResult, error) {
u := h.client.URL(epAlerts, nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return AlertsResult{}, err
}
_, body, err := h.client.Do(ctx, req)
if err != nil {
return AlertsResult{}, err
}
var res AlertsResult
err = json.Unmarshal(body, &res)
return res, err
}
func (h *httpAPI) AlertManagers(ctx context.Context) (AlertManagersResult, error) {
u := h.client.URL(epAlertManagers, nil)