Metrics: expand coredns_dns_responses_total with plugin label

This adds (somewhat hacky?) code to add a plugin label to the
coredns_dns_responses_total metric. It's completely obvlious to the
plugin as we just check who called the *recorder.WriteMsg method. We use
runtime.Caller( 1 2 3) to get multiple levels of callers, this should be
deep enough, but it depends on the dns.ResponseWriter wrapping that's
occuring.

README.md of metrics updates and test added in test/metrics_test.go to
check for the label being set.

I went through the plugin to see what metrics could be removed, but
actually didn't find any, the plugin push out metrics that make sense.

Due to the path fiddling to figure out the plugin name I doubt this
works (out-of-the-box) for external plugins, but I haven't tested that.

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2021-10-08 15:34:43 +02:00
parent d95a82350b
commit a2770e031f
8 changed files with 84 additions and 7 deletions

View File

@@ -68,6 +68,34 @@ func TestMetricsRefused(t *testing.T) {
}
}
func TestMetricsPlugin(t *testing.T) {
metricName := "coredns_dns_responses_total"
corefile := `example.org:0 {
whoami
prometheus localhost:0
}`
srv, udp, _, err := CoreDNSServerAndPorts(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer srv.Stop()
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeA)
if _, err = dns.Exchange(m, udp); err != nil {
t.Fatalf("Could not send message: %s", err)
}
data := test.Scrape("http://" + metrics.ListenAddr + "/metrics")
_, labels := test.MetricValue(metricName, data)
if labels["plugin"] != "whoami" {
t.Errorf("Expected plugin value %s, but got %s", "whoami", labels["whoami"])
}
}
func TestMetricsAuto(t *testing.T) {
tmpdir, err := ioutil.TempDir(os.TempDir(), "coredns")
if err != nil {