aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2018-04-01 13:58:13 +0100
committerGravatar GitHub <noreply@github.com> 2018-04-01 13:58:13 +0100
commit2338120f5b8987abdeff12018473c7cef14eebf1 (patch)
tree1b51fa5c3aca2497b3a318d4c0dfb672a1bb5b68
parent4df416ca1da2fff396ff84805518aa8f8b55b80b (diff)
downloadcoredns-2338120f5b8987abdeff12018473c7cef14eebf1.tar.gz
coredns-2338120f5b8987abdeff12018473c7cef14eebf1.tar.zst
coredns-2338120f5b8987abdeff12018473c7cef14eebf1.zip
plugin/metrics: add MustRegister function (#1648)
This registers the Collectors iff the metrics plugin has been loaded. Safes a bunch of code in each and every plugin's setup code.
-rw-r--r--plugin/autopath/setup.go10
-rw-r--r--plugin/cache/setup.go16
-rw-r--r--plugin/dnssec/setup.go11
-rw-r--r--plugin/forward/setup.go12
-rw-r--r--plugin/health/overloaded.go2
-rw-r--r--plugin/health/setup.go10
-rw-r--r--plugin/metrics/register.go23
-rw-r--r--plugin/proxy/setup.go11
-rw-r--r--plugin/template/metrics.go11
9 files changed, 34 insertions, 72 deletions
diff --git a/plugin/autopath/setup.go b/plugin/autopath/setup.go
index ac0f05ec5..6015c7460 100644
--- a/plugin/autopath/setup.go
+++ b/plugin/autopath/setup.go
@@ -26,15 +26,7 @@ func setup(c *caddy.Controller) error {
}
c.OnStartup(func() error {
- once.Do(func() {
- m := dnsserver.GetConfig(c).Handler("prometheus")
- if m == nil {
- return
- }
- if x, ok := m.(*metrics.Metrics); ok {
- x.MustRegister(AutoPathCount)
- }
- })
+ once.Do(func() { metrics.MustRegister(c, AutoPathCount) })
return nil
})
diff --git a/plugin/cache/setup.go b/plugin/cache/setup.go
index 57edd3246..8fe8eb59c 100644
--- a/plugin/cache/setup.go
+++ b/plugin/cache/setup.go
@@ -32,18 +32,10 @@ func setup(c *caddy.Controller) error {
c.OnStartup(func() error {
once.Do(func() {
- m := dnsserver.GetConfig(c).Handler("prometheus")
- if m == nil {
- return
- }
- if x, ok := m.(*metrics.Metrics); ok {
- x.MustRegister(cacheSize)
- x.MustRegister(cacheCapacity)
- x.MustRegister(cacheHits)
- x.MustRegister(cacheMisses)
- x.MustRegister(cachePrefetches)
- x.MustRegister(cacheDrops)
- }
+ metrics.MustRegister(c,
+ cacheSize, cacheCapacity,
+ cacheHits, cacheMisses,
+ cachePrefetches, cacheDrops)
})
return nil
})
diff --git a/plugin/dnssec/setup.go b/plugin/dnssec/setup.go
index efcc1aa85..2f82315f8 100644
--- a/plugin/dnssec/setup.go
+++ b/plugin/dnssec/setup.go
@@ -33,16 +33,7 @@ func setup(c *caddy.Controller) error {
c.OnStartup(func() error {
once.Do(func() {
- m := dnsserver.GetConfig(c).Handler("prometheus")
- if m == nil {
- return
- }
- if x, ok := m.(*metrics.Metrics); ok {
- x.MustRegister(cacheSize)
- x.MustRegister(cacheCapacity)
- x.MustRegister(cacheHits)
- x.MustRegister(cacheMisses)
- }
+ metrics.MustRegister(c, cacheSize, cacheCapacity, cacheHits, cacheMisses)
})
return nil
})
diff --git a/plugin/forward/setup.go b/plugin/forward/setup.go
index 71a5ec1f0..137137d45 100644
--- a/plugin/forward/setup.go
+++ b/plugin/forward/setup.go
@@ -38,17 +38,7 @@ func setup(c *caddy.Controller) error {
c.OnStartup(func() error {
once.Do(func() {
- m := dnsserver.GetConfig(c).Handler("prometheus")
- if m == nil {
- return
- }
- if x, ok := m.(*metrics.Metrics); ok {
- x.MustRegister(RequestCount)
- x.MustRegister(RcodeCount)
- x.MustRegister(RequestDuration)
- x.MustRegister(HealthcheckFailureCount)
- x.MustRegister(SocketGauge)
- }
+ metrics.MustRegister(c, RequestCount, RcodeCount, RequestDuration, HealthcheckFailureCount, SocketGauge)
})
return f.OnStartup()
})
diff --git a/plugin/health/overloaded.go b/plugin/health/overloaded.go
index 976cafa84..06b0b65e1 100644
--- a/plugin/health/overloaded.go
+++ b/plugin/health/overloaded.go
@@ -49,4 +49,4 @@ var (
})
)
-var onceMetric sync.Once
+var once sync.Once
diff --git a/plugin/health/setup.go b/plugin/health/setup.go
index acb194a34..bd5b5c3fc 100644
--- a/plugin/health/setup.go
+++ b/plugin/health/setup.go
@@ -55,15 +55,7 @@ func setup(c *caddy.Controller) error {
})
c.OnStartup(func() error {
- onceMetric.Do(func() {
- m := dnsserver.GetConfig(c).Handler("prometheus")
- if m == nil {
- return
- }
- if x, ok := m.(*metrics.Metrics); ok {
- x.MustRegister(HealthDuration)
- }
- })
+ once.Do(func() { metrics.MustRegister(c, HealthDuration) })
return nil
})
diff --git a/plugin/metrics/register.go b/plugin/metrics/register.go
new file mode 100644
index 000000000..bbc388d0c
--- /dev/null
+++ b/plugin/metrics/register.go
@@ -0,0 +1,23 @@
+package metrics
+
+import (
+ "github.com/coredns/coredns/core/dnsserver"
+
+ "github.com/mholt/caddy"
+ "github.com/prometheus/client_golang/prometheus"
+)
+
+// MustRegister registers the prometheus Collectors when the metrics middleware is used.
+func MustRegister(c *caddy.Controller, cs ...prometheus.Collector) {
+ m := dnsserver.GetConfig(c).Handler("prometheus")
+ if m == nil {
+ return
+ }
+ x, ok := m.(*Metrics)
+ if !ok {
+ return
+ }
+ for _, c := range cs {
+ x.MustRegister(c)
+ }
+}
diff --git a/plugin/proxy/setup.go b/plugin/proxy/setup.go
index eff7c63f4..268579751 100644
--- a/plugin/proxy/setup.go
+++ b/plugin/proxy/setup.go
@@ -30,16 +30,7 @@ func setup(c *caddy.Controller) error {
})
c.OnStartup(func() error {
- once.Do(func() {
- m := dnsserver.GetConfig(c).Handler("prometheus")
- if m == nil {
- return
- }
- if x, ok := m.(*metrics.Metrics); ok {
- x.MustRegister(RequestCount)
- x.MustRegister(RequestDuration)
- }
- })
+ once.Do(func() { metrics.MustRegister(c, RequestCount, RequestDuration) })
return nil
})
diff --git a/plugin/template/metrics.go b/plugin/template/metrics.go
index 4cdaa5321..bc2594782 100644
--- a/plugin/template/metrics.go
+++ b/plugin/template/metrics.go
@@ -3,7 +3,6 @@ package template
import (
"sync"
- "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
@@ -37,15 +36,7 @@ var (
func setupMetrics(c *caddy.Controller) error {
c.OnStartup(func() error {
metricsOnce.Do(func() {
- m := dnsserver.GetConfig(c).Handler("prometheus")
- if m == nil {
- return
- }
- if x, ok := m.(*metrics.Metrics); ok {
- x.MustRegister(TemplateMatchesCount)
- x.MustRegister(TemplateFailureCount)
- x.MustRegister(TemplateRRFailureCount)
- }
+ metrics.MustRegister(c, TemplateMatchesCount, TemplateFailureCount, TemplateRRFailureCount)
})
return nil
})