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:
Miek Gieben 2017-12-27 15:48:14 +00:00 committed by GitHub
parent 5ac42ed5c2
commit 90dd4bbd45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 45 deletions

17
plugin/cache/setup.go vendored
View file

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