mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 10:43:17 -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:
@@ -25,15 +25,6 @@ var (
|
||||
}, []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
|
||||
// empty string is not a known family
|
||||
func familyToString(f int) string {
|
||||
@@ -46,4 +37,4 @@ func familyToString(f int) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
var metricsOnce sync.Once
|
||||
var once sync.Once
|
||||
|
||||
@@ -3,6 +3,7 @@ package proxy
|
||||
import (
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/metrics"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
@@ -28,7 +29,19 @@ func setup(c *caddy.Controller) error {
|
||||
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 {
|
||||
u := upstreams[i]
|
||||
|
||||
Reference in New Issue
Block a user