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:
Miek Gieben
2016-10-26 10:01:52 +01:00
committed by GitHub
parent 6d9d60081d
commit 219bfd0493
39 changed files with 828 additions and 259 deletions

View File

@@ -1,12 +1,31 @@
package test
import "testing"
import (
"io/ioutil"
"log"
"os"
"path"
"testing"
"time"
"github.com/miekg/coredns/middleware/cache"
"github.com/miekg/coredns/middleware/metrics"
mtest "github.com/miekg/coredns/middleware/metrics/test"
"github.com/miekg/coredns/middleware/metrics/vars"
"github.com/miekg/dns"
)
// Start test server that has metrics enabled. Then tear it down again.
func TestMetricsServer(t *testing.T) {
corefile := `.:0 {
corefile := `example.org:0 {
chaos CoreDNS-001 miek@miek.nl
prometheus localhost:0
prometheus
}
example.com:0 {
proxy . 8.8.4.4:53
prometheus
}
`
srv, err := CoreDNSServer(corefile)
@@ -15,3 +34,140 @@ func TestMetricsServer(t *testing.T) {
}
defer srv.Stop()
}
func TestMetricsRefused(t *testing.T) {
metricName := "coredns_dns_response_rcode_count_total"
corefile := `example.org:0 {
proxy . 8.8.8.8:53
prometheus
}
`
srv, err := CoreDNSServer(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer srv.Stop()
udp, _ := CoreDNSServerPorts(srv, 0)
m := new(dns.Msg)
m.SetQuestion("google.com.", dns.TypeA)
if _, err = dns.Exchange(m, udp); err != nil {
t.Fatalf("Could not send message: %s", err)
}
data := mtest.Scrape(t, "http://"+metrics.Addr+"/metrics")
got, labels := mtest.MetricValue(metricName, data)
if got != "1" {
t.Errorf("Expected value %s for refused, but got %s", "1", got)
}
if labels["zone"] != vars.Dropped {
t.Errorf("Expected zone value %s for refused, but got %s", vars.Dropped, labels["zone"])
}
if labels["rcode"] != "REFUSED" {
t.Errorf("Expected zone value %s for refused, but got %s", "REFUSED", labels["rcode"])
}
}
func TestMetricsCache(t *testing.T) {
metricName := "coredns_cache_size_guage"
corefile := `example.net:0 {
proxy . 8.8.8.8:53
prometheus
cache
}
`
srv, err := CoreDNSServer(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer srv.Stop()
udp, _ := CoreDNSServerPorts(srv, 0)
m := new(dns.Msg)
m.SetQuestion("www.example.net.", dns.TypeA)
if _, err = dns.Exchange(m, udp); err != nil {
t.Fatalf("Could not send message: %s", err)
}
data := mtest.Scrape(t, "http://"+metrics.Addr+"/metrics")
// Get the value for the metrics where the one of the labels values matches "success"
got, _ := mtest.MetricValueLabel(metricName, cache.Success, data)
if got != "1" {
t.Errorf("Expected value %s for %s, but got %s", "1", metricName, got)
}
}
func TestMetricsAuto(t *testing.T) {
tmpdir, err := ioutil.TempDir(os.TempDir(), "coredns")
if err != nil {
t.Fatal(err)
}
// TODO(miek): Random port as string and use that later?
corefile := `org:0 {
auto {
directory ` + tmpdir + ` db\.(.*) {1} 1
}
prometheus
}
`
i, err := CoreDNSServer(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
udp, _ := CoreDNSServerPorts(i, 0)
if udp == "" {
t.Fatalf("Could not get UDP listening port")
}
defer i.Stop()
log.SetOutput(ioutil.Discard)
// Write db.example.org to get example.org.
if err = ioutil.WriteFile(path.Join(tmpdir, "db.example.org"), []byte(zoneContent), 0644); err != nil {
t.Fatal(err)
}
// TODO(miek): make the auto sleep even less.
time.Sleep(1100 * time.Millisecond) // wait for it to be picked up
m := new(dns.Msg)
m.SetQuestion("www.example.org.", dns.TypeA)
if _, err := dns.Exchange(m, udp); err != nil {
t.Fatalf("Could not send message: %s", err)
}
metricName := "coredns_dns_request_count_total" //{zone, proto, family}
data := mtest.Scrape(t, "http://"+metrics.Addr+"/metrics")
// Get the value for the metrics where the one of the labels values matches "example.org."
got, _ := mtest.MetricValueLabel(metricName, "example.org.", data)
if got != "1" {
t.Errorf("Expected value %s for %s, but got %s", "1", metricName, got)
}
// Remove db.example.org again. And see if the metric stops increasing.
os.Remove(path.Join(tmpdir, "db.example.org"))
time.Sleep(1100 * time.Millisecond) // wait for it to be picked up
if _, err := dns.Exchange(m, udp); err != nil {
t.Fatalf("Could not send message: %s", err)
}
data = mtest.Scrape(t, "http://"+metrics.Addr+"/metrics")
got, _ = mtest.MetricValueLabel(metricName, "example.org.", data)
if got != "1" {
t.Errorf("Expected value %s for %s, but got %s", "1", metricName, got)
}
}