Fix handling of ConstLabels in checkLabels

This commit is contained in:
beorn7 2017-05-10 20:39:36 +02:00
parent 753a259e20
commit 023c31fd59
1 changed files with 31 additions and 19 deletions

View File

@ -27,6 +27,9 @@ import (
"github.com/prometheus/client_golang/prometheus" "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 // InstrumentHandlerInFlight is a middleware that wraps the provided
// http.Handler. It sets the provided prometheus.Gauge to the number of // http.Handler. It sets the provided prometheus.Gauge to the number of
// requests currently handled by the wrapped http.Handler. // 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 { if _, err := prometheus.NewConstMetric(desc, prometheus.UntypedValue, 0); err == nil {
return 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 { if err := m.Write(&pm); err != nil {
panic("error checking metric for labels") 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 { 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 continue
} }
panic("metric partitioned with non-supported labels") panic("metric partitioned with non-supported labels")
} }
code = true code = true
method = true method = true
return return
} }
panic("metric partitioned with non-supported labels") panic("metric partitioned with non-supported labels")
} }