mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 16:24:19 -04:00
metrics: correctly register all metrics (#1335)
After initial startup, see if prometheus is loaded and if so, register our metrics with it. Stop doing the init() func and just use the sync.Once so we don't double registrer our metrics.
This commit is contained in:
17
plugin/cache/setup.go
vendored
17
plugin/cache/setup.go
vendored
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/metrics"
|
||||
"github.com/coredns/coredns/plugin/pkg/cache"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
@@ -29,6 +30,22 @@ func setup(c *caddy.Controller) error {
|
||||
return ca
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
})
|
||||
|
||||
// Export the capacity for the metrics. This only happens once, because this is a re-load change only.
|
||||
cacheCapacity.WithLabelValues(Success).Set(float64(ca.pcap))
|
||||
cacheCapacity.WithLabelValues(Denial).Set(float64(ca.ncap))
|
||||
|
||||
Reference in New Issue
Block a user