mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
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:
@@ -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
16
plugin/cache/setup.go
vendored
@@ -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
|
||||
})
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
@@ -49,4 +49,4 @@ var (
|
||||
})
|
||||
)
|
||||
|
||||
var onceMetric sync.Once
|
||||
var once sync.Once
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
|
||||
23
plugin/metrics/register.go
Normal file
23
plugin/metrics/register.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
})
|
||||
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user