diff options
author | 2019-05-18 18:34:46 +0100 | |
---|---|---|
committer | 2019-05-18 18:34:46 +0100 | |
commit | 118b0c940890161d185b64497605a7ef84c38a0a (patch) | |
tree | a66511c124afb8c87c16a7784250dc441650e671 /plugin/metrics/metrics.go | |
parent | d41e9ff7b7196374856d8db4bf33b31df8e20abc (diff) | |
download | coredns-118b0c940890161d185b64497605a7ef84c38a0a.tar.gz coredns-118b0c940890161d185b64497605a7ef84c38a0a.tar.zst coredns-118b0c940890161d185b64497605a7ef84c38a0a.zip |
plugin/metrcs: fix datarace on listeners (#2835)
This fixes a data race on the listener(s) that get started in the
metrics plugins.
It also restore pkg/uniq to its former glory and removes and state being
carried in there; this means for metrics that registry.go was to
replicate that behavior *with* locking (as pkg/uniq doesn't do, or need
that).
Also renamed uniqAddr to just u, to make it slightly shorter.
Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'plugin/metrics/metrics.go')
-rw-r--r-- | plugin/metrics/metrics.go | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/plugin/metrics/metrics.go b/plugin/metrics/metrics.go index acf31c0a8..2b165e5b9 100644 --- a/plugin/metrics/metrics.go +++ b/plugin/metrics/metrics.go @@ -15,22 +15,24 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) -// Metrics holds the prometheus configuration. The metrics' path is fixed to be /metrics +// Metrics holds the prometheus configuration. The metrics' path is fixed to be /metrics . type Metrics struct { - Next plugin.Handler - Addr string - Reg *prometheus.Registry + Next plugin.Handler + Addr string + Reg *prometheus.Registry + ln net.Listener lnSetup bool - mux *http.ServeMux - srv *http.Server + + mux *http.ServeMux + srv *http.Server zoneNames []string zoneMap map[string]struct{} zoneMu sync.RWMutex } -// New returns a new instance of Metrics with the given address +// New returns a new instance of Metrics with the given address. func New(addr string) *Metrics { met := &Metrics{ Addr: addr, @@ -101,14 +103,19 @@ func (m *Metrics) OnStartup() error { m.ln = ln m.lnSetup = true - ListenAddr = m.ln.Addr().String() // For tests m.mux = http.NewServeMux() m.mux.Handle("/metrics", promhttp.HandlerFor(m.Reg, promhttp.HandlerOpts{})) - m.srv = &http.Server{Handler: m.mux} + + // creating some helper variables to avoid data races on m.srv and m.ln + server := &http.Server{Handler: m.mux} + m.srv = server + go func() { - m.srv.Serve(m.ln) + server.Serve(ln) }() + + ListenAddr = ln.Addr().String() // For tests. return nil } @@ -117,7 +124,7 @@ func (m *Metrics) OnRestart() error { if !m.lnSetup { return nil } - uniqAddr.Unset(m.Addr) + u.Unset(m.Addr) return m.stopServer() } |