package metrics import ( "os" "regexp" "strings" "testing" "github.com/prometheus/client_golang/prometheus" ) // TestGrafanaDashboardOnlyUsesMetricsThatExist keeps deploy/grafana/fancy-dns.json // from drifting away from the collectors: a renamed metric would otherwise // leave a panel empty and nothing would say why. func TestGrafanaDashboardOnlyUsesMetricsThatExist(t *testing.T) { initCollectors() descs := make(chan *prometheus.Desc, 256) go func() { Registry.Describe(descs); close(descs) }() known := map[string]bool{} nameRe := regexp.MustCompile(`fqName: "([a-z0-9_]+)"`) for d := range descs { if m := nameRe.FindStringSubmatch(d.String()); m != nil { known[m[1]] = true } } if !known["fancydns_queries_total"] { t.Fatalf("collector names were not read; got %v", known) } raw, err := os.ReadFile("../../deploy/grafana/fancy-dns.json") if err != nil { t.Fatal(err) } used := regexp.MustCompile(`fancydns_[a-z0-9_]+`).FindAllString(string(raw), -1) if len(used) == 0 { t.Fatal("the dashboard references no fancydns metrics") } for _, name := range used { base := name for _, suffix := range []string{"_bucket", "_count", "_sum"} { base = strings.TrimSuffix(base, suffix) } if !known[base] { t.Errorf("the dashboard queries %s, which the resolver does not export", name) } } }