coredns/middleware/metrics/setup.go
Miek Gieben 9ac3cab1b7 Make CoreDNS a server type plugin for Caddy (#220)
* Make CoreDNS a server type plugin for Caddy

Remove code we don't need and port all middleware over. Fix all tests
and rework the documentation.

Also make `go generate` build a caddy binary which we then copy into
our directory. This means `go build`-builds remain working as-is.

And new etc instances in each etcd test for better isolation.
Fix more tests and rework test.Server with the newer support Caddy offers.

Fix Makefile to support new mode of operation.
2016-08-19 17:14:17 -07:00

84 lines
1.5 KiB
Go

package metrics
import (
"sync"
"github.com/miekg/coredns/core/dnsserver"
"github.com/miekg/coredns/middleware"
"github.com/mholt/caddy"
)
func init() {
caddy.RegisterPlugin("prometheus", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
m, err := prometheusParse(c)
if err != nil {
return err
}
dnsserver.GetConfig(c).AddMiddleware(func(next dnsserver.Handler) dnsserver.Handler {
m.Next = next
return m
})
metricsOnce.Do(func() {
c.OnStartup(m.Startup)
c.OnShutdown(m.Shutdown)
})
return nil
}
func prometheusParse(c *caddy.Controller) (Metrics, error) {
var (
met Metrics
err error
)
for c.Next() {
if len(met.ZoneNames) > 0 {
return Metrics{}, c.Err("metrics: can only have one metrics module per server")
}
met.ZoneNames = make([]string, len(c.ServerBlockKeys))
copy(met.ZoneNames, c.ServerBlockKeys)
for i, _ := range met.ZoneNames {
met.ZoneNames[i] = middleware.Host(met.ZoneNames[i]).Normalize()
}
args := c.RemainingArgs()
switch len(args) {
case 0:
case 1:
met.Addr = args[0]
default:
return Metrics{}, c.ArgErr()
}
for c.NextBlock() {
switch c.Val() {
case "address":
args = c.RemainingArgs()
if len(args) != 1 {
return Metrics{}, c.ArgErr()
}
met.Addr = args[0]
default:
return Metrics{}, c.Errf("metrics: unknown item: %s", c.Val())
}
}
}
if met.Addr == "" {
met.Addr = addr
}
return met, err
}
var metricsOnce sync.Once
const addr = "localhost:9153"