diff options
author | 2017-04-12 10:10:57 +0000 | |
---|---|---|
committer | 2017-04-12 10:10:57 +0000 | |
commit | c2629460093d47cd3eed17efd785308f4cbfd503 (patch) | |
tree | a66dfb970604eff263ce0b81d6236126187d448b | |
parent | 2196dde9da1cd8dd17367831c4938213edd0c7f3 (diff) | |
download | coredns-c2629460093d47cd3eed17efd785308f4cbfd503.tar.gz coredns-c2629460093d47cd3eed17efd785308f4cbfd503.tar.zst coredns-c2629460093d47cd3eed17efd785308f4cbfd503.zip |
middleware/metrics: allow multiple listeners
There was no inherent reason *not* to allow multiple listeners for the
monitoring data. Actually enforcing only one listener lead to more code
then just allowing multiple. It's probably not what you want; but
CoreDNS is happy to oblige.
-rw-r--r-- | middleware/metrics/README.md | 4 | ||||
-rw-r--r-- | middleware/metrics/setup.go | 40 |
2 files changed, 35 insertions, 9 deletions
diff --git a/middleware/metrics/README.md b/middleware/metrics/README.md index f32108932..a5d5bc8dc 100644 --- a/middleware/metrics/README.md +++ b/middleware/metrics/README.md @@ -49,5 +49,5 @@ prometheus localhost:9253 # Bugs -When reloading, we keep the handler running, meaning that any changes to the handler aren't picked -up. You'll need to restart CoreDNS for that to happen. +When reloading, we keep the handler running, meaning that any changes to the handler's address +aren't picked up. You'll need to restart CoreDNS for that to happen. diff --git a/middleware/metrics/setup.go b/middleware/metrics/setup.go index 40f0ee671..1ed455ffc 100644 --- a/middleware/metrics/setup.go +++ b/middleware/metrics/setup.go @@ -2,7 +2,6 @@ package metrics import ( "net" - "sync" "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/middleware" @@ -15,6 +14,8 @@ func init() { ServerType: "dns", Action: setup, }) + + uniqAddr = addrs{a: make(map[string]int)} } func setup(c *caddy.Controller) error { @@ -28,11 +29,14 @@ func setup(c *caddy.Controller) error { return m }) - // During restarts we will keep this handler running. - metricsOnce.Do(func() { - c.OncePerServerBlock(m.OnStartup) - c.OnFinalShutdown(m.OnShutdown) - }) + for a, v := range uniqAddr.a { + if v == todo { + // During restarts we will keep this handler running, BUG. + c.OncePerServerBlock(m.OnStartup) + } + uniqAddr.a[a] = done + } + c.OnFinalShutdown(m.OnShutdown) return nil } @@ -43,6 +47,10 @@ func prometheusParse(c *caddy.Controller) (*Metrics, error) { err error ) + defer func() { + uniqAddr.SetAddress(met.Addr) + }() + for c.Next() { if len(met.ZoneNames()) > 0 { return met, c.Err("can only have one metrics module per server") @@ -86,7 +94,25 @@ func prometheusParse(c *caddy.Controller) (*Metrics, error) { return met, err } -var metricsOnce sync.Once +var uniqAddr addrs + +// Keep track on which addrs we listen, so we only start one listener. +type addrs struct { + a map[string]int +} + +func (a *addrs) SetAddress(addr string) { + // If already there and set to done, we've already started this listener. + if a.a[addr] == done { + return + } + a.a[addr] = todo +} // Addr is the address the where the metrics are exported by default. const addr = "localhost:9153" + +const ( + todo = 1 + done = 2 +) |