Commit 90dd4bbd authored by Miek Gieben's avatar Miek Gieben Committed by GitHub

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.
parent 5ac42ed5
...@@ -18,12 +18,4 @@ var ( ...@@ -18,12 +18,4 @@ var (
}, []string{}) }, []string{})
) )
// OnStartupMetrics sets up the metrics on startup. var once sync.Once
func OnStartupMetrics() error {
metricsOnce.Do(func() {
prometheus.MustRegister(AutoPathCount)
})
return nil
}
var metricsOnce sync.Once
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/miekg/dns" "github.com/miekg/dns"
...@@ -24,7 +25,18 @@ func setup(c *caddy.Controller) error { ...@@ -24,7 +25,18 @@ func setup(c *caddy.Controller) error {
return plugin.Error("autopath", err) return plugin.Error("autopath", err)
} }
c.OnStartup(OnStartupMetrics) 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)
}
})
return nil
})
// Do this in OnStartup, so all plugin has been initialized. // Do this in OnStartup, so all plugin has been initialized.
c.OnStartup(func() error { c.OnStartup(func() error {
......
package cache package cache
import ( import (
"sync"
"time" "time"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
...@@ -84,38 +85,31 @@ func (c *Cache) get(now time.Time, qname string, qtype uint16, do bool) (*item, ...@@ -84,38 +85,31 @@ func (c *Cache) get(now time.Time, qname string, qtype uint16, do bool) (*item,
var ( var (
cacheSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ cacheSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: subsystem, Subsystem: "cache",
Name: "size", Name: "size",
Help: "The number of elements in the cache.", Help: "The number of elements in the cache.",
}, []string{"type"}) }, []string{"type"})
cacheCapacity = prometheus.NewGaugeVec(prometheus.GaugeOpts{ cacheCapacity = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: subsystem, Subsystem: "cache",
Name: "capacity", Name: "capacity",
Help: "The cache's capacity.", Help: "The cache's capacity.",
}, []string{"type"}) }, []string{"type"})
cacheHits = prometheus.NewCounterVec(prometheus.CounterOpts{ cacheHits = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: subsystem, Subsystem: "cache",
Name: "hits_total", Name: "hits_total",
Help: "The count of cache hits.", Help: "The count of cache hits.",
}, []string{"type"}) }, []string{"type"})
cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{ cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: subsystem, Subsystem: "cache",
Name: "misses_total", Name: "misses_total",
Help: "The count of cache misses.", Help: "The count of cache misses.",
}) })
) )
const subsystem = "cache" var once sync.Once
func init() {
prometheus.MustRegister(cacheSize)
prometheus.MustRegister(cacheCapacity)
prometheus.MustRegister(cacheHits)
prometheus.MustRegister(cacheMisses)
}
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/coredns/coredns/core/dnsserver" "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/pkg/cache" "github.com/coredns/coredns/plugin/pkg/cache"
"github.com/mholt/caddy" "github.com/mholt/caddy"
...@@ -29,6 +30,22 @@ func setup(c *caddy.Controller) error { ...@@ -29,6 +30,22 @@ func setup(c *caddy.Controller) error {
return ca 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. // 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(Success).Set(float64(ca.pcap))
cacheCapacity.WithLabelValues(Denial).Set(float64(ca.ncap)) cacheCapacity.WithLabelValues(Denial).Set(float64(ca.ncap))
......
package dnssec package dnssec
import ( import (
"sync"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
...@@ -42,28 +44,28 @@ func (d Dnssec) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) ...@@ -42,28 +44,28 @@ func (d Dnssec) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
var ( var (
cacheSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ cacheSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: subsystem, Subsystem: "dnssec",
Name: "cache_size", Name: "cache_size",
Help: "The number of elements in the dnssec cache.", Help: "The number of elements in the dnssec cache.",
}, []string{"type"}) }, []string{"type"})
cacheCapacity = prometheus.NewGaugeVec(prometheus.GaugeOpts{ cacheCapacity = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: subsystem, Subsystem: "dnssec",
Name: "cache_capacity", Name: "cache_capacity",
Help: "The dnssec cache's capacity.", Help: "The dnssec cache's capacity.",
}, []string{"type"}) }, []string{"type"})
cacheHits = prometheus.NewCounter(prometheus.CounterOpts{ cacheHits = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: subsystem, Subsystem: "dnssec",
Name: "cache_hits_total", Name: "cache_hits_total",
Help: "The count of cache hits.", Help: "The count of cache hits.",
}) })
cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{ cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: plugin.Namespace, Namespace: plugin.Namespace,
Subsystem: subsystem, Subsystem: "dnssec",
Name: "cache_misses_total", Name: "cache_misses_total",
Help: "The count of cache misses.", Help: "The count of cache misses.",
}) })
...@@ -72,11 +74,4 @@ var ( ...@@ -72,11 +74,4 @@ var (
// Name implements the Handler interface. // Name implements the Handler interface.
func (d Dnssec) Name() string { return "dnssec" } func (d Dnssec) Name() string { return "dnssec" }
const subsystem = "dnssec" var once sync.Once
func init() {
prometheus.MustRegister(cacheSize)
prometheus.MustRegister(cacheCapacity)
prometheus.MustRegister(cacheHits)
prometheus.MustRegister(cacheMisses)
}
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/coredns/coredns/core/dnsserver" "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/pkg/cache" "github.com/coredns/coredns/plugin/pkg/cache"
"github.com/mholt/caddy" "github.com/mholt/caddy"
...@@ -30,6 +31,22 @@ func setup(c *caddy.Controller) error { ...@@ -30,6 +31,22 @@ func setup(c *caddy.Controller) error {
return New(zones, keys, next, ca) 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. // Export the capacity for the metrics. This only happens once, because this is a re-load change only.
cacheCapacity.WithLabelValues("signature").Set(float64(capacity)) cacheCapacity.WithLabelValues("signature").Set(float64(capacity))
......
...@@ -25,15 +25,6 @@ var ( ...@@ -25,15 +25,6 @@ var (
}, []string{"proto", "proxy_proto", "family", "to"}) }, []string{"proto", "proxy_proto", "family", "to"})
) )
// OnStartupMetrics sets up the metrics on startup. This is done for all proxy protocols.
func OnStartupMetrics() error {
metricsOnce.Do(func() {
prometheus.MustRegister(RequestCount)
prometheus.MustRegister(RequestDuration)
})
return nil
}
// familyToString returns the string form of either 1, or 2. Returns // familyToString returns the string form of either 1, or 2. Returns
// empty string is not a known family // empty string is not a known family
func familyToString(f int) string { func familyToString(f int) string {
...@@ -46,4 +37,4 @@ func familyToString(f int) string { ...@@ -46,4 +37,4 @@ func familyToString(f int) string {
return "" return ""
} }
var metricsOnce sync.Once var once sync.Once
...@@ -3,6 +3,7 @@ package proxy ...@@ -3,6 +3,7 @@ package proxy
import ( import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
...@@ -28,7 +29,19 @@ func setup(c *caddy.Controller) error { ...@@ -28,7 +29,19 @@ func setup(c *caddy.Controller) error {
return P return P
}) })
c.OnStartup(OnStartupMetrics) 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)
}
})
return nil
})
for i := range upstreams { for i := range upstreams {
u := upstreams[i] u := upstreams[i]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment