mirror of
https://github.com/coredns/coredns.git
synced 2025-11-03 02:33:21 -05:00
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>
62 lines
1.7 KiB
Go
62 lines
1.7 KiB
Go
// Package dnstest allows for easy testing of DNS client against a test server.
|
|
package dnstest
|
|
|
|
import (
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// Recorder is a type of ResponseWriter that captures
|
|
// the rcode code written to it and also the size of the message
|
|
// written in the response. A rcode code does not have
|
|
// to be written, however, in which case 0 must be assumed.
|
|
// It is best to have the constructor initialize this type
|
|
// with that default status code.
|
|
type Recorder struct {
|
|
dns.ResponseWriter
|
|
Rcode int
|
|
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,
|
|
// which captures the DNS rcode from the ResponseWriter
|
|
// and also the length of the response message written through it.
|
|
func NewRecorder(w dns.ResponseWriter) *Recorder {
|
|
return &Recorder{
|
|
ResponseWriter: w,
|
|
Rcode: 0,
|
|
Msg: nil,
|
|
Start: time.Now(),
|
|
}
|
|
}
|
|
|
|
// WriteMsg records the status code and calls the
|
|
// underlying ResponseWriter's WriteMsg method.
|
|
func (r *Recorder) WriteMsg(res *dns.Msg) error {
|
|
_, 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()
|
|
r.Msg = res
|
|
return r.ResponseWriter.WriteMsg(res)
|
|
}
|
|
|
|
// Write is a wrapper that records the length of the message that gets written.
|
|
func (r *Recorder) Write(buf []byte) (int, error) {
|
|
n, err := r.ResponseWriter.Write(buf)
|
|
if err == nil {
|
|
r.Len += n
|
|
}
|
|
return n, err
|
|
}
|