From 0c026b43871ac967f47bb38c53545c8e3e36dbdd Mon Sep 17 00:00:00 2001 From: Stuart Nelson Date: Tue, 20 Mar 2018 12:06:34 +0100 Subject: [PATCH] Remove empty alert labels on ingest The same behavior exists in prometheus. This is a bit superfluous, but in the event people are using old versions of prometheus or a different metric gathering system, it's still valid to check. --- api/api.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/api/api.go b/api/api.go index b9e2ef15..aae8da72 100644 --- a/api/api.go +++ b/api/api.go @@ -496,6 +496,8 @@ func (api *API) insertAlerts(w http.ResponseWriter, r *http.Request, alerts ...* validationErrs = &types.MultiError{} ) for _, a := range alerts { + removeEmptyLabels(a.Labels) + if err := a.Validate(); err != nil { validationErrs.Add(err) numInvalidAlerts.Inc() @@ -522,6 +524,14 @@ func (api *API) insertAlerts(w http.ResponseWriter, r *http.Request, alerts ...* api.respond(w, nil) } +func removeEmptyLabels(ls model.LabelSet) { + for k, v := range ls { + if string(v) == "" { + delete(ls, k) + } + } +} + func (api *API) setSilence(w http.ResponseWriter, r *http.Request) { var sil types.Silence if err := api.receive(r, &sil); err != nil {