diff --git a/prometheus/promhttp/instrument_server.go b/prometheus/promhttp/instrument_server.go index 193598d..1beab4b 100644 --- a/prometheus/promhttp/instrument_server.go +++ b/prometheus/promhttp/instrument_server.go @@ -27,6 +27,9 @@ import ( "github.com/prometheus/client_golang/prometheus" ) +// magicString is used for the hacky label test in checkLabels. Remove once fixed. +const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa" + // InstrumentHandlerInFlight is a middleware that wraps the provided // http.Handler. It sets the provided prometheus.Gauge to the number of // requests currently handled by the wrapped http.Handler. @@ -191,37 +194,46 @@ func checkLabels(c prometheus.Collector) (code bool, method bool) { if _, err := prometheus.NewConstMetric(desc, prometheus.UntypedValue, 0); err == nil { return - } else if m, err := prometheus.NewConstMetric(desc, prometheus.UntypedValue, 0, ""); err == nil { + } + if m, err := prometheus.NewConstMetric(desc, prometheus.UntypedValue, 0, magicString); err == nil { if err := m.Write(&pm); err != nil { panic("error checking metric for labels") } - - name := *pm.Label[0].Name - if name == "code" { - code = true - } else if name == "method" { - method = true - } else { - panic("metric partitioned with non-supported labels") - } - return - } else if m, err := prometheus.NewConstMetric(desc, prometheus.UntypedValue, 0, "", ""); err == nil { - if err := m.Write(&pm); err != nil { - panic("error checking metric for labels") - } - for _, label := range pm.Label { - if *label.Name == "code" || *label.Name == "method" { + name, value := label.GetName(), label.GetValue() + if value != magicString { + continue + } + switch name { + case "code": + code = true + case "method": + method = true + default: + panic("metric partitioned with non-supported labels") + } + return + } + panic("previously set label not found – this must never happen") + } + if m, err := prometheus.NewConstMetric(desc, prometheus.UntypedValue, 0, magicString, magicString); err == nil { + if err := m.Write(&pm); err != nil { + panic("error checking metric for labels") + } + for _, label := range pm.Label { + name, value := label.GetName(), label.GetValue() + if value != magicString { + continue + } + if name == "code" || name == "method" { continue } panic("metric partitioned with non-supported labels") } - code = true method = true return } - panic("metric partitioned with non-supported labels") }