aboutsummaryrefslogtreecommitdiff
path: root/plugin/metrics/recorder.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2021-11-12 16:07:05 +0000
committerGravatar GitHub <noreply@github.com> 2021-11-12 16:07:05 +0000
commit6953ab2b4f23f916a08b68ba51b9a26e41e9a748 (patch)
tree4cd98dfc2314f4a56926a5ab5b0627e1eb040e9f /plugin/metrics/recorder.go
parent29cae579325e5968343d665f02f8d961624f9481 (diff)
downloadcoredns-6953ab2b4f23f916a08b68ba51b9a26e41e9a748.tar.gz
coredns-6953ab2b4f23f916a08b68ba51b9a26e41e9a748.tar.zst
coredns-6953ab2b4f23f916a08b68ba51b9a26e41e9a748.zip
Metrics: expand coredns_dns_responses_total with plugin label (#4914)
* 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> * better comment Signed-off-by: Miek Gieben <miek@miek.nl> * 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> * Update core/dnsserver/server.go Co-authored-by: dilyevsky <ilyevsky@gmail.com> * Use [3]string Signed-off-by: Miek Gieben <miek@miek.nl> * imports Signed-off-by: Miek Gieben <miek@miek.nl> * remove dnstest changes Signed-off-by: Miek Gieben <miek@miek.nl> * revert Signed-off-by: Miek Gieben <miek@miek.nl> * Add some sleeps to make it less flaky Signed-off-by: Miek Gieben <miek@miek.nl> * Revert "Add some sleeps to make it less flaky" This reverts commit b5c6655196e3ad570555f086832ceb1f48f6f2d5. * Remove forward when not needed Signed-off-by: Miek Gieben <miek@miek.nl> * remove newline Signed-off-by: Miek Gieben <miek@miek.nl> Co-authored-by: dilyevsky <ilyevsky@gmail.com>
Diffstat (limited to 'plugin/metrics/recorder.go')
-rw-r--r--plugin/metrics/recorder.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/plugin/metrics/recorder.go b/plugin/metrics/recorder.go
new file mode 100644
index 000000000..a37f420d5
--- /dev/null
+++ b/plugin/metrics/recorder.go
@@ -0,0 +1,30 @@
+package metrics
+
+import (
+ "runtime"
+
+ "github.com/coredns/coredns/plugin/pkg/dnstest"
+
+ "github.com/miekg/dns"
+)
+
+// Recorder is a dnstest.Recorder specific to the metrics plugin.
+type Recorder struct {
+ *dnstest.Recorder
+ // CallerN holds the string return value of the call to runtime.Caller(N+1)
+ Caller [3]string
+}
+
+// NewRecorder makes and returns a new Recorder.
+func NewRecorder(w dns.ResponseWriter) *Recorder { return &Recorder{Recorder: dnstest.NewRecorder(w)} }
+
+// WriteMsg records the status code and calls the
+// underlying ResponseWriter's WriteMsg method.
+func (r *Recorder) WriteMsg(res *dns.Msg) error {
+ _, r.Caller[0], _, _ = runtime.Caller(1)
+ _, r.Caller[1], _, _ = runtime.Caller(2)
+ _, r.Caller[2], _, _ = runtime.Caller(3)
+ r.Len += res.Len()
+ r.Msg = res
+ return r.ResponseWriter.WriteMsg(res)
+}