mirror of
https://github.com/coredns/coredns.git
synced 2026-01-08 18:01:21 -05:00
middleware/metrics: cleanup (#355)
* middleware/metrics: add more metrics middleware/cache: Add metrics for number of elements in the cache. Also export the total size. Update README to detail the new metrics. middleware/metrics Move metrics into subpackage called "vars". This breaks the import cycle and is cleaner. This allows vars.Report to be used in the the dnsserver to log refused queries. middleware/metrics: tests Add tests to the metrics framework. The metrics/test subpackage allows scraping of the local server. Do a few test scrape of the metrics that are defined in the metrics middleware. This also allows metrics integration tests to check if the caching and dnssec middleware export their metrics correctly. * update README * typos * fix tests
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/middleware/file"
|
||||
"github.com/miekg/coredns/middleware/metrics"
|
||||
"github.com/miekg/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
@@ -20,6 +21,7 @@ type (
|
||||
Next middleware.Handler
|
||||
*Zones
|
||||
|
||||
metrics *metrics.Metrics
|
||||
loader
|
||||
}
|
||||
|
||||
@@ -97,3 +99,5 @@ func (a Auto) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (i
|
||||
w.WriteMsg(m)
|
||||
return dns.RcodeSuccess, nil
|
||||
}
|
||||
|
||||
func (a Auto) Name() string { return "auto" }
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/miekg/coredns/core/dnsserver"
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/middleware/file"
|
||||
"github.com/miekg/coredns/middleware/metrics"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
@@ -28,10 +29,16 @@ func setup(c *caddy.Controller) error {
|
||||
return middleware.Error("auto", err)
|
||||
}
|
||||
|
||||
// If we have enabled prometheus we should add newly discovered zones to it.
|
||||
met := dnsserver.GetMiddleware(c, "prometheus")
|
||||
if met != nil {
|
||||
a.metrics = met.(*metrics.Metrics)
|
||||
}
|
||||
|
||||
walkChan := make(chan bool)
|
||||
|
||||
c.OnStartup(func() error {
|
||||
err := a.Zones.Walk(a.loader)
|
||||
err := a.Walk()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -43,7 +50,7 @@ func setup(c *caddy.Controller) error {
|
||||
case <-walkChan:
|
||||
return
|
||||
case <-ticker.C:
|
||||
a.Zones.Walk(a.loader)
|
||||
a.Walk()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -13,26 +13,26 @@ import (
|
||||
)
|
||||
|
||||
// Walk will recursively walk of the file under l.directory and adds the one that match l.re.
|
||||
func (z *Zones) Walk(l loader) error {
|
||||
func (a Auto) Walk() error {
|
||||
|
||||
// TODO(miek): should add something so that we don't stomp on each other.
|
||||
|
||||
toDelete := make(map[string]bool)
|
||||
for _, n := range z.Names() {
|
||||
for _, n := range a.Zones.Names() {
|
||||
toDelete[n] = true
|
||||
}
|
||||
|
||||
filepath.Walk(l.directory, func(path string, info os.FileInfo, err error) error {
|
||||
filepath.Walk(a.loader.directory, func(path string, info os.FileInfo, err error) error {
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
match, origin := matches(l.re, info.Name(), l.template)
|
||||
match, origin := matches(a.loader.re, info.Name(), a.loader.template)
|
||||
if !match {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, ok := z.Z[origin]; ok {
|
||||
if _, ok := a.Zones.Z[origin]; ok {
|
||||
// we already have this zone
|
||||
toDelete[origin] = false
|
||||
return nil
|
||||
@@ -50,10 +50,14 @@ func (z *Zones) Walk(l loader) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
zo.NoReload = l.noReload
|
||||
zo.TransferTo = l.transferTo
|
||||
zo.NoReload = a.loader.noReload
|
||||
zo.TransferTo = a.loader.transferTo
|
||||
|
||||
z.Insert(zo, origin)
|
||||
a.Zones.Add(zo, origin)
|
||||
|
||||
if a.metrics != nil {
|
||||
a.metrics.AddZone(origin)
|
||||
}
|
||||
|
||||
zo.Notify()
|
||||
|
||||
@@ -68,7 +72,13 @@ func (z *Zones) Walk(l loader) error {
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
z.Delete(origin)
|
||||
|
||||
if a.metrics != nil {
|
||||
a.metrics.RemoveZone(origin)
|
||||
}
|
||||
|
||||
a.Zones.Remove(origin)
|
||||
|
||||
log.Printf("[INFO] Deleting zone `%s'", origin)
|
||||
}
|
||||
|
||||
|
||||
@@ -37,13 +37,16 @@ func TestWalk(t *testing.T) {
|
||||
template: `${1}`,
|
||||
}
|
||||
|
||||
z := &Zones{}
|
||||
a := Auto{
|
||||
loader: ldr,
|
||||
Zones: &Zones{},
|
||||
}
|
||||
|
||||
z.Walk(ldr)
|
||||
a.Walk()
|
||||
|
||||
// db.example.org and db.example.com should be here (created in createFiles)
|
||||
for _, name := range []string{"example.com.", "example.org."} {
|
||||
if _, ok := z.Z[name]; !ok {
|
||||
if _, ok := a.Zones.Z[name]; !ok {
|
||||
t.Errorf("%s should have been added", name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,15 +27,18 @@ func TestWatcher(t *testing.T) {
|
||||
template: `${1}`,
|
||||
}
|
||||
|
||||
z := &Zones{}
|
||||
a := Auto{
|
||||
loader: ldr,
|
||||
Zones: &Zones{},
|
||||
}
|
||||
|
||||
z.Walk(ldr)
|
||||
a.Walk()
|
||||
|
||||
// example.org and example.com should exist
|
||||
if x := len(z.Z["example.org."].All()); x != 4 {
|
||||
if x := len(a.Zones.Z["example.org."].All()); x != 4 {
|
||||
t.Fatalf("expected 4 RRs, got %d", x)
|
||||
}
|
||||
if x := len(z.Z["example.com."].All()); x != 4 {
|
||||
if x := len(a.Zones.Z["example.com."].All()); x != 4 {
|
||||
t.Fatalf("expected 4 RRs, got %d", x)
|
||||
}
|
||||
|
||||
@@ -44,5 +47,6 @@ func TestWatcher(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
z.Walk(ldr)
|
||||
a.Walk()
|
||||
// TODO(miek): check
|
||||
}
|
||||
|
||||
@@ -40,9 +40,9 @@ func (z *Zones) Zones(name string) *file.Zone {
|
||||
return zo
|
||||
}
|
||||
|
||||
// Insert inserts a new zone into z. If zo.NoReload is false, the
|
||||
// Add adds a new zone into z. If zo.NoReload is false, the
|
||||
// reload goroutine is started.
|
||||
func (z *Zones) Insert(zo *file.Zone, name string) {
|
||||
func (z *Zones) Add(zo *file.Zone, name string) {
|
||||
z.Lock()
|
||||
|
||||
if z.Z == nil {
|
||||
@@ -51,14 +51,13 @@ func (z *Zones) Insert(zo *file.Zone, name string) {
|
||||
|
||||
z.Z[name] = zo
|
||||
z.names = append(z.names, name)
|
||||
|
||||
zo.Reload()
|
||||
|
||||
z.Unlock()
|
||||
}
|
||||
|
||||
// Delete removes the zone named name from z. It also stop the the zone's reload goroutine.
|
||||
func (z *Zones) Delete(name string) {
|
||||
// Remove removes the zone named name from z. It also stop the the zone's reload goroutine.
|
||||
func (z *Zones) Remove(name string) {
|
||||
z.Lock()
|
||||
|
||||
if zo, ok := z.Z[name]; ok && !zo.NoReload {
|
||||
@@ -67,10 +66,11 @@ func (z *Zones) Delete(name string) {
|
||||
|
||||
delete(z.Z, name)
|
||||
|
||||
// just regenerate Names (might be bad if you have a lot of zones...)
|
||||
// TODO(miek): just regenerate Names (might be bad if you have a lot of zones...)
|
||||
z.names = []string{}
|
||||
for n := range z.Z {
|
||||
z.names = append(z.names, n)
|
||||
}
|
||||
|
||||
z.Unlock()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user