From 5bc4643122ce18dc07e553bcf153151127961c13 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sun, 27 Feb 2022 14:19:11 +0000 Subject: [PATCH] Update package web for new labels.Labels type Signed-off-by: Bryan Boreham --- web/federate.go | 18 +++++++++++------- web/web.go | 5 +---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/web/federate.go b/web/federate.go index baa3b5866..85472bb44 100644 --- a/web/federate.go +++ b/web/federate.go @@ -171,26 +171,24 @@ func (h *Handler) federation(w http.ResponseWriter, req *http.Request) { Untyped: &dto.Untyped{}, } - for _, l := range s.Metric { + err := s.Metric.Validate(func(l labels.Label) error { if l.Value == "" { // No value means unset. Never consider those labels. // This is also important to protect against nameless metrics. - continue + return nil } if l.Name == labels.MetricName { nameSeen = true if l.Value == lastMetricName { // We already have the name in the current MetricFamily, // and we ignore nameless metrics. - continue + return nil } // Need to start a new MetricFamily. Ship off the old one (if any) before // creating the new one. if protMetricFam != nil { if err := enc.Encode(protMetricFam); err != nil { - federationErrors.Inc() - level.Error(h.logger).Log("msg", "federation failed", "err", err) - return + return err } } protMetricFam = &dto.MetricFamily{ @@ -198,7 +196,7 @@ func (h *Handler) federation(w http.ResponseWriter, req *http.Request) { Name: proto.String(l.Value), } lastMetricName = l.Value - continue + return nil } protMetric.Label = append(protMetric.Label, &dto.LabelPair{ Name: proto.String(l.Name), @@ -207,6 +205,12 @@ func (h *Handler) federation(w http.ResponseWriter, req *http.Request) { if _, ok := externalLabels[l.Name]; ok { globalUsed[l.Name] = struct{}{} } + return nil + }) + if err != nil { + federationErrors.Inc() + level.Error(h.logger).Log("msg", "federation failed", "err", err) + return } if !nameSeen { level.Warn(h.logger).Log("msg", "Ignoring nameless metric during federation", "metric", s.Metric) diff --git a/web/web.go b/web/web.go index 2c3676fa2..f43d5104c 100644 --- a/web/web.go +++ b/web/web.go @@ -653,13 +653,10 @@ func (h *Handler) consoles(w http.ResponseWriter, r *http.Request) { params[k] = v[0] } - externalLabels := map[string]string{} h.mtx.RLock() els := h.config.GlobalConfig.ExternalLabels h.mtx.RUnlock() - for _, el := range els { - externalLabels[el.Name] = el.Value - } + externalLabels := els.Map() // Inject some convenience variables that are easier to remember for users // who are not used to Go's templating system.