github.com/thanos-io/thanos@v0.32.5/pkg/extprom/testing.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package extprom 5 6 import ( 7 "fmt" 8 "strings" 9 "testing" 10 11 "github.com/efficientgo/core/testutil" 12 "github.com/prometheus/client_golang/prometheus" 13 ) 14 15 // CurrentGaugeValuesFor returns gauge values for given metric names. Useful for testing based on registry, 16 // when you don't have access to metric variable. 17 func CurrentGaugeValuesFor(t *testing.T, reg prometheus.Gatherer, metricNames ...string) map[string]float64 { 18 f, err := reg.Gather() 19 testutil.Ok(t, err) 20 21 res := make(map[string]float64, len(metricNames)) 22 for _, g := range f { 23 for _, m := range metricNames { 24 if g.GetName() != m { 25 continue 26 } 27 28 for _, metric := range g.GetMetric() { 29 var lbls []string 30 for _, l := range metric.GetLabel() { 31 lbls = append(lbls, *l.Name+"="+*l.Value) 32 } 33 34 key := fmt.Sprintf("%s{%s}", m, strings.Join(lbls, ",")) 35 if _, ok := res[key]; ok { 36 t.Fatal("duplicate metrics, should never happen with Prometheus Registry; key =", key) 37 } 38 if metric.GetGauge() == nil { 39 t.Fatal("metric is not a gauge; key =", key) 40 } 41 res[key] = *(metric.GetGauge().Value) 42 } 43 } 44 } 45 return res 46 }