From 7490f0a74525a1f863eaf81b42f3ead3a1ecc43a Mon Sep 17 00:00:00 2001 From: Roald Nefs Date: Mon, 25 Mar 2019 09:23:28 +0100 Subject: [PATCH] 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 --- api/prometheus/v1/api.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index 5078df1..c048179 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -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)