plugin/metrics: add MustRegister function (#1648)

This registers the Collectors iff the metrics plugin has been loaded.
Safes a bunch of code in each and every plugin's setup code.
This commit is contained in:
Miek Gieben
2018-04-01 13:58:13 +01:00
committed by GitHub
parent 4df416ca1d
commit 2338120f5b
9 changed files with 34 additions and 72 deletions

View File

@@ -0,0 +1,23 @@
package metrics
import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/mholt/caddy"
"github.com/prometheus/client_golang/prometheus"
)
// MustRegister registers the prometheus Collectors when the metrics middleware is used.
func MustRegister(c *caddy.Controller, cs ...prometheus.Collector) {
m := dnsserver.GetConfig(c).Handler("prometheus")
if m == nil {
return
}
x, ok := m.(*Metrics)
if !ok {
return
}
for _, c := range cs {
x.MustRegister(c)
}
}