aboutsummaryrefslogtreecommitdiff
path: root/plugin/metrics/metrics.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/metrics.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/metrics.go')
-rw-r--r--plugin/metrics/metrics.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/plugin/metrics/metrics.go b/plugin/metrics/metrics.go
index f6c1e6c8c..6a9e65272 100644
--- a/plugin/metrics/metrics.go
+++ b/plugin/metrics/metrics.go
@@ -8,6 +8,7 @@ import (
"sync"
"time"
+ "github.com/coredns/caddy"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/reuseport"
@@ -31,6 +32,8 @@ type Metrics struct {
zoneNames []string
zoneMap map[string]struct{}
zoneMu sync.RWMutex
+
+ plugins map[string]struct{} // all available plugins, used to determine which plugin made the client write
}
// New returns a new instance of Metrics with the given address.
@@ -39,6 +42,7 @@ func New(addr string) *Metrics {
Addr: addr,
Reg: prometheus.DefaultRegisterer.(*prometheus.Registry),
zoneMap: make(map[string]struct{}),
+ plugins: pluginList(caddy.ListPlugins()),
}
return met
@@ -140,6 +144,19 @@ func keys(m map[string]struct{}) []string {
return sx
}
+// pluginList iterates over the returned plugin map from caddy and removes the "dns." prefix from them.
+func pluginList(m map[string][]string) map[string]struct{} {
+ pm := map[string]struct{}{}
+ for _, p := range m["others"] {
+ // only add 'dns.' plugins
+ if len(p) > 3 {
+ pm[p[4:]] = struct{}{}
+ continue
+ }
+ }
+ return pm
+}
+
// ListenAddr is assigned the address of the prometheus listener. Its use is mainly in tests where
// we listen on "localhost:0" and need to retrieve the actual address.
var ListenAddr string