mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 16:24:19 -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 {
|
c.OnStartup(func() error {
|
||||||
once.Do(func() {
|
once.Do(func() { metrics.MustRegister(c, AutoPathCount) })
|
||||||
m := dnsserver.GetConfig(c).Handler("prometheus")
|
|
||||||
if m == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if x, ok := m.(*metrics.Metrics); ok {
|
|
||||||
x.MustRegister(AutoPathCount)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return nil
|
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 {
|
c.OnStartup(func() error {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
m := dnsserver.GetConfig(c).Handler("prometheus")
|
metrics.MustRegister(c,
|
||||||
if m == nil {
|
cacheSize, cacheCapacity,
|
||||||
return
|
cacheHits, cacheMisses,
|
||||||
}
|
cachePrefetches, cacheDrops)
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -33,16 +33,7 @@ func setup(c *caddy.Controller) error {
|
|||||||
|
|
||||||
c.OnStartup(func() error {
|
c.OnStartup(func() error {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
m := dnsserver.GetConfig(c).Handler("prometheus")
|
metrics.MustRegister(c, cacheSize, cacheCapacity, cacheHits, cacheMisses)
|
||||||
if m == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if x, ok := m.(*metrics.Metrics); ok {
|
|
||||||
x.MustRegister(cacheSize)
|
|
||||||
x.MustRegister(cacheCapacity)
|
|
||||||
x.MustRegister(cacheHits)
|
|
||||||
x.MustRegister(cacheMisses)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -38,17 +38,7 @@ func setup(c *caddy.Controller) error {
|
|||||||
|
|
||||||
c.OnStartup(func() error {
|
c.OnStartup(func() error {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
m := dnsserver.GetConfig(c).Handler("prometheus")
|
metrics.MustRegister(c, RequestCount, RcodeCount, RequestDuration, HealthcheckFailureCount, SocketGauge)
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return f.OnStartup()
|
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 {
|
c.OnStartup(func() error {
|
||||||
onceMetric.Do(func() {
|
once.Do(func() { metrics.MustRegister(c, HealthDuration) })
|
||||||
m := dnsserver.GetConfig(c).Handler("prometheus")
|
|
||||||
if m == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if x, ok := m.(*metrics.Metrics); ok {
|
|
||||||
x.MustRegister(HealthDuration)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return nil
|
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 {
|
c.OnStartup(func() error {
|
||||||
once.Do(func() {
|
once.Do(func() { metrics.MustRegister(c, RequestCount, RequestDuration) })
|
||||||
m := dnsserver.GetConfig(c).Handler("prometheus")
|
|
||||||
if m == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if x, ok := m.(*metrics.Metrics); ok {
|
|
||||||
x.MustRegister(RequestCount)
|
|
||||||
x.MustRegister(RequestDuration)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package template
|
|||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/coredns/coredns/core/dnsserver"
|
|
||||||
"github.com/coredns/coredns/plugin"
|
"github.com/coredns/coredns/plugin"
|
||||||
"github.com/coredns/coredns/plugin/metrics"
|
"github.com/coredns/coredns/plugin/metrics"
|
||||||
|
|
||||||
@@ -37,15 +36,7 @@ var (
|
|||||||
func setupMetrics(c *caddy.Controller) error {
|
func setupMetrics(c *caddy.Controller) error {
|
||||||
c.OnStartup(func() error {
|
c.OnStartup(func() error {
|
||||||
metricsOnce.Do(func() {
|
metricsOnce.Do(func() {
|
||||||
m := dnsserver.GetConfig(c).Handler("prometheus")
|
metrics.MustRegister(c, TemplateMatchesCount, TemplateFailureCount, TemplateRRFailureCount)
|
||||||
if m == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if x, ok := m.(*metrics.Metrics); ok {
|
|
||||||
x.MustRegister(TemplateMatchesCount)
|
|
||||||
x.MustRegister(TemplateFailureCount)
|
|
||||||
x.MustRegister(TemplateRRFailureCount)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user