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.
This commit is contained in:
Miek Gieben
2018-04-01 13:58:13 +01:00
committed by GitHub
parent 4df416ca1d
commit 2338120f5b
9 changed files with 34 additions and 72 deletions

View File

@@ -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
})

16
plugin/cache/setup.go vendored
View File

@@ -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
})

View File

@@ -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
})

View File

@@ -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()
})

View File

@@ -49,4 +49,4 @@ var (
})
)
var onceMetric sync.Once
var once sync.Once

View File

@@ -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
})

View File

@@ -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)
}
}

View File

@@ -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
})

View File

@@ -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
})