mirror of
https://github.com/coredns/coredns.git
synced 2025-11-27 22:24:09 -05: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:
@@ -1,6 +1,8 @@
|
||||
package dnssec
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
@@ -42,28 +44,28 @@ func (d Dnssec) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
|
||||
var (
|
||||
cacheSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
Subsystem: subsystem,
|
||||
Subsystem: "dnssec",
|
||||
Name: "cache_size",
|
||||
Help: "The number of elements in the dnssec cache.",
|
||||
}, []string{"type"})
|
||||
|
||||
cacheCapacity = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
Subsystem: subsystem,
|
||||
Subsystem: "dnssec",
|
||||
Name: "cache_capacity",
|
||||
Help: "The dnssec cache's capacity.",
|
||||
}, []string{"type"})
|
||||
|
||||
cacheHits = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
Subsystem: subsystem,
|
||||
Subsystem: "dnssec",
|
||||
Name: "cache_hits_total",
|
||||
Help: "The count of cache hits.",
|
||||
})
|
||||
|
||||
cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
Subsystem: subsystem,
|
||||
Subsystem: "dnssec",
|
||||
Name: "cache_misses_total",
|
||||
Help: "The count of cache misses.",
|
||||
})
|
||||
@@ -72,11 +74,4 @@ var (
|
||||
// Name implements the Handler interface.
|
||||
func (d Dnssec) Name() string { return "dnssec" }
|
||||
|
||||
const subsystem = "dnssec"
|
||||
|
||||
func init() {
|
||||
prometheus.MustRegister(cacheSize)
|
||||
prometheus.MustRegister(cacheCapacity)
|
||||
prometheus.MustRegister(cacheHits)
|
||||
prometheus.MustRegister(cacheMisses)
|
||||
}
|
||||
var once sync.Once
|
||||
|
||||
@@ -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"
|
||||
@@ -30,6 +31,22 @@ func setup(c *caddy.Controller) error {
|
||||
return New(zones, keys, next, 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("signature").Set(float64(capacity))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user