diff options
author | 2017-12-14 13:19:03 -0500 | |
---|---|---|
committer | 2017-12-14 18:19:03 +0000 | |
commit | 671d17061955d3182528e7c7669b3e8823246e46 (patch) | |
tree | 2dafd2467d45228ab53562ae204374e6ced01d30 /plugin | |
parent | 1919913c986b7c1e1652a94bf69e54b503e40a18 (diff) | |
download | coredns-671d17061955d3182528e7c7669b3e8823246e46.tar.gz coredns-671d17061955d3182528e7c7669b3e8823246e46.tar.zst coredns-671d17061955d3182528e7c7669b3e8823246e46.zip |
plugin/metrics: Switch to using promhttp instead of deprecated Handler (#1312)
prometheus.Handler is deprecated according to the godoc for the package so
instead we're using promhttp.
Additionally, we are exposing the Registry that metrics is using so other
plugins that are not inside of coredns can read the registry. Otherwise, if
we kept using the Default one, there's no way to access that from outside
of the coredns repo since it is vendored.
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/metrics/metrics.go | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/plugin/metrics/metrics.go b/plugin/metrics/metrics.go index df07da0ae..a2ded2235 100644 --- a/plugin/metrics/metrics.go +++ b/plugin/metrics/metrics.go @@ -5,29 +5,20 @@ import ( "log" "net" "net/http" + "os" "sync" "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/metrics/vars" - "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" ) -func init() { - prometheus.MustRegister(vars.RequestCount) - prometheus.MustRegister(vars.RequestDuration) - prometheus.MustRegister(vars.RequestSize) - prometheus.MustRegister(vars.RequestDo) - prometheus.MustRegister(vars.RequestType) - - prometheus.MustRegister(vars.ResponseSize) - prometheus.MustRegister(vars.ResponseRcode) -} - // Metrics holds the prometheus configuration. The metrics' path is fixed to be /metrics type Metrics struct { Next plugin.Handler Addr string + Reg *prometheus.Registry ln net.Listener mux *http.ServeMux @@ -38,7 +29,24 @@ type Metrics struct { // New returns a new instance of Metrics with the given address func New(addr string) *Metrics { - return &Metrics{Addr: addr, zoneMap: make(map[string]bool)} + met := &Metrics{ + Addr: addr, + Reg: prometheus.NewRegistry(), + zoneMap: make(map[string]bool), + } + // Add the default collectors + met.Reg.MustRegister(prometheus.NewGoCollector()) + met.Reg.MustRegister(prometheus.NewProcessCollector(os.Getpid(), "")) + + // Add all of our collectors + met.Reg.MustRegister(vars.RequestCount) + met.Reg.MustRegister(vars.RequestDuration) + met.Reg.MustRegister(vars.RequestSize) + met.Reg.MustRegister(vars.RequestDo) + met.Reg.MustRegister(vars.RequestType) + met.Reg.MustRegister(vars.ResponseSize) + met.Reg.MustRegister(vars.ResponseRcode) + return met } // AddZone adds zone z to m. @@ -77,7 +85,7 @@ func (m *Metrics) OnStartup() error { ListenAddr = m.ln.Addr().String() m.mux = http.NewServeMux() - m.mux.Handle("/metrics", prometheus.Handler()) + m.mux.Handle("/metrics", promhttp.HandlerFor(m.Reg, promhttp.HandlerOpts{})) go func() { http.Serve(m.ln, m.mux) |