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

@@ -2,6 +2,7 @@
package dnstest
import (
"runtime"
"time"
"github.com/miekg/dns"
@@ -19,6 +20,10 @@ type Recorder struct {
Len int
Msg *dns.Msg
Start time.Time
// CallerN holds string parameters of a call to runtime.Caller(N)
Caller1 string
Caller2 string
Caller3 string
}
// NewRecorder makes and returns a new Recorder,
@@ -36,7 +41,9 @@ func NewRecorder(w dns.ResponseWriter) *Recorder {
// WriteMsg records the status code and calls the
// underlying ResponseWriter's WriteMsg method.
func (r *Recorder) WriteMsg(res *dns.Msg) error {
r.Rcode = res.Rcode
_, r.Caller1, _, _ = runtime.Caller(1)
_, r.Caller2, _, _ = runtime.Caller(2)
_, r.Caller3, _, _ = runtime.Caller(3)
// We may get called multiple times (axfr for instance).
// Save the last message, but add the sizes.
r.Len += res.Len()