diff options
author | 2017-09-14 09:36:06 +0100 | |
---|---|---|
committer | 2017-09-14 09:36:06 +0100 | |
commit | d8714e64e400ef873c2adc4d929a07d7890727b9 (patch) | |
tree | c9fa4c157e6af12eb1517654f8d23ca5d5619513 /plugin/metrics/setup.go | |
parent | b984aa45595dc95253b91191afe7d3ee29e71b48 (diff) | |
download | coredns-d8714e64e400ef873c2adc4d929a07d7890727b9.tar.gz coredns-d8714e64e400ef873c2adc4d929a07d7890727b9.tar.zst coredns-d8714e64e400ef873c2adc4d929a07d7890727b9.zip |
Remove the word middleware (#1067)
* Rename middleware to plugin
first pass; mostly used 'sed', few spots where I manually changed
text.
This still builds a coredns binary.
* fmt error
* Rename AddMiddleware to AddPlugin
* Readd AddMiddleware to remain backwards compat
Diffstat (limited to 'plugin/metrics/setup.go')
-rw-r--r-- | plugin/metrics/setup.go | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/plugin/metrics/setup.go b/plugin/metrics/setup.go new file mode 100644 index 000000000..eecfac62c --- /dev/null +++ b/plugin/metrics/setup.go @@ -0,0 +1,100 @@ +package metrics + +import ( + "net" + + "github.com/coredns/coredns/core/dnsserver" + "github.com/coredns/coredns/plugin" + + "github.com/mholt/caddy" +) + +func init() { + caddy.RegisterPlugin("prometheus", caddy.Plugin{ + ServerType: "dns", + Action: setup, + }) + + uniqAddr = addrs{a: make(map[string]int)} +} + +func setup(c *caddy.Controller) error { + m, err := prometheusParse(c) + if err != nil { + return plugin.Error("prometheus", err) + } + + dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { + m.Next = next + return m + }) + + 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 +} + +func prometheusParse(c *caddy.Controller) (*Metrics, error) { + var ( + met = &Metrics{Addr: addr, zoneMap: make(map[string]bool)} + 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") + } + + for _, z := range c.ServerBlockKeys { + met.AddZone(plugin.Host(z).Normalize()) + } + args := c.RemainingArgs() + + switch len(args) { + case 0: + case 1: + met.Addr = args[0] + _, _, e := net.SplitHostPort(met.Addr) + if e != nil { + return met, e + } + default: + return met, c.ArgErr() + } + } + return met, err +} + +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 +) |