aboutsummaryrefslogtreecommitdiff
path: root/plugin/metrics/test/scrape.go
diff options
context:
space:
mode:
authorGravatar Francois Tur <ftur@infoblox.com> 2018-11-01 15:56:00 -0400
committerGravatar Miek Gieben <miek@miek.nl> 2018-11-01 19:56:00 +0000
commit05204ef14253de13c18b8442c2b04dd03345d98b (patch)
tree1050182126f5c9288e59aeb8aa7275a47935bdea /plugin/metrics/test/scrape.go
parentf5aa6cac67d65357dfa81f39f3ef33e57a376795 (diff)
downloadcoredns-05204ef14253de13c18b8442c2b04dd03345d98b.tar.gz
coredns-05204ef14253de13c18b8442c2b04dd03345d98b.tar.zst
coredns-05204ef14253de13c18b8442c2b04dd03345d98b.zip
Metrics registered on wrong prometheus registry (#2246)
* - UT on metrics verifying that all plugins of all blocs have their metrics collectors declared * - fix error msg * - redirect Registry of metric to the one that handle the listener - allow duplicate of metrics collector on the same Registry (case of same plugin in 2 blocs listening metrics on the same address) * - fix change of signature * - ensure cleaning metrics before starting the test (metrics collectors are global vars .. and re-used by several tests) * - I think I fixed this test. Ensure correct mn of hits and clean metrics before test. * - fix typo in error msg - proposed at review * - fix typo in comment * - remove ResetMetrics functions - change a way to test the numeric metrics : get the diff between begin and end of test * - oops. removing debug logs
Diffstat (limited to 'plugin/metrics/test/scrape.go')
-rw-r--r--plugin/metrics/test/scrape.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/plugin/metrics/test/scrape.go b/plugin/metrics/test/scrape.go
index a21c0061d..185627491 100644
--- a/plugin/metrics/test/scrape.go
+++ b/plugin/metrics/test/scrape.go
@@ -27,6 +27,7 @@ import (
"io"
"mime"
"net/http"
+ "strconv"
"testing"
"github.com/matttproud/golang_protobuf_extensions/pbutil"
@@ -78,6 +79,47 @@ func Scrape(t *testing.T, url string) []*MetricFamily {
return result
}
+// ScrapeMetricAsInt provide a sum of all metrics collected for the name and label provided.
+// if the metric is not a numeric value, it will be counted a 0.
+func ScrapeMetricAsInt(t *testing.T, addr string, name string, label string, nometricvalue int) int {
+
+ valueToInt := func(m metric) int {
+ v := m.Value
+ r, err := strconv.Atoi(v)
+ if err != nil {
+ return 0
+ }
+ return r
+ }
+
+ met := Scrape(t, fmt.Sprintf("http://%s/metrics", addr))
+ found := false
+ tot := 0
+ for _, mf := range met {
+ if mf.Name == name {
+ // Sum all metrics available
+ for _, m := range mf.Metrics {
+ if label == "" {
+ tot += valueToInt(m.(metric))
+ found = true
+ continue
+ }
+ for _, v := range m.(metric).Labels {
+ if v == label {
+ tot += valueToInt(m.(metric))
+ found = true
+ }
+ }
+ }
+ }
+ }
+
+ if !found {
+ return nometricvalue
+ }
+ return tot
+}
+
// MetricValue returns the value associated with name as a string as well as the labels.
// It only returns the first metrics of the slice.
func MetricValue(name string, mfs []*MetricFamily) (string, map[string]string) {