* Metrics: expand coredns_dns_responses_total with plugin label
This adds (somewhat hacky?) code to add a plugin label to the
coredns_dns_responses_total metric. It's completely obvlious to the
plugin as we just check who called the *recorder.WriteMsg method. We use
runtime.Caller( 1 2 3) to get multiple levels of callers, this should be
deep enough, but it depends on the dns.ResponseWriter wrapping that's
occuring.
README.md of metrics updates and test added in test/metrics_test.go to
check for the label being set.
I went through the plugin to see what metrics could be removed, but
actually didn't find any, the plugin push out metrics that make sense.
Due to the path fiddling to figure out the plugin name I doubt this
works (out-of-the-box) for external plugins, but I haven't tested that.
Signed-off-by: Miek Gieben <miek@miek.nl>
* better comment
Signed-off-by: Miek Gieben <miek@miek.nl>
* Metrics: expand coredns_dns_responses_total with plugin label
This adds (somewhat hacky?) code to add a plugin label to the
coredns_dns_responses_total metric. It's completely obvlious to the
plugin as we just check who called the *recorder.WriteMsg method. We use
runtime.Caller( 1 2 3) to get multiple levels of callers, this should be
deep enough, but it depends on the dns.ResponseWriter wrapping that's
occuring.
README.md of metrics updates and test added in test/metrics_test.go to
check for the label being set.
I went through the plugin to see what metrics could be removed, but
actually didn't find any, the plugin push out metrics that make sense.
Due to the path fiddling to figure out the plugin name I doubt this
works (out-of-the-box) for external plugins, but I haven't tested that.
Signed-off-by: Miek Gieben <miek@miek.nl>
* Update core/dnsserver/server.go
Co-authored-by: dilyevsky <ilyevsky@gmail.com>
* Use [3]string
Signed-off-by: Miek Gieben <miek@miek.nl>
* imports
Signed-off-by: Miek Gieben <miek@miek.nl>
* remove dnstest changes
Signed-off-by: Miek Gieben <miek@miek.nl>
* revert
Signed-off-by: Miek Gieben <miek@miek.nl>
* Add some sleeps to make it less flaky
Signed-off-by: Miek Gieben <miek@miek.nl>
* Revert "Add some sleeps to make it less flaky"
This reverts commit b5c6655196
.
* Remove forward when not needed
Signed-off-by: Miek Gieben <miek@miek.nl>
* remove newline
Signed-off-by: Miek Gieben <miek@miek.nl>
Co-authored-by: dilyevsky <ilyevsky@gmail.com>
257 lines
7.1 KiB
Go
257 lines
7.1 KiB
Go
package test
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coredns/coredns/plugin/metrics"
|
|
"github.com/coredns/coredns/plugin/metrics/vars"
|
|
"github.com/coredns/coredns/plugin/test"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// Because we don't properly shutdown the metrics servers we are re-using the metrics between tests, not a superbad issue
|
|
// but depending on the ordering of the tests this trips up stuff.
|
|
|
|
// Start test server that has metrics enabled. Then tear it down again.
|
|
func TestMetricsServer(t *testing.T) {
|
|
corefile := `
|
|
example.org:0 {
|
|
chaos CoreDNS-001 miek@miek.nl
|
|
prometheus localhost:0
|
|
}
|
|
example.com:0 {
|
|
log
|
|
prometheus localhost:0
|
|
}`
|
|
|
|
srv, err := CoreDNSServer(corefile)
|
|
if err != nil {
|
|
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
|
|
}
|
|
defer srv.Stop()
|
|
}
|
|
|
|
func TestMetricsRefused(t *testing.T) {
|
|
metricName := "coredns_dns_responses_total"
|
|
corefile := `example.org:0 {
|
|
whoami
|
|
prometheus localhost:0
|
|
}`
|
|
|
|
srv, udp, _, err := CoreDNSServerAndPorts(corefile)
|
|
if err != nil {
|
|
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
|
|
}
|
|
defer srv.Stop()
|
|
|
|
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 := test.Scrape("http://" + metrics.ListenAddr + "/metrics")
|
|
got, labels := test.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 TestMetricsAuto(t *testing.T) {
|
|
tmpdir, err := os.MkdirTemp(os.TempDir(), "coredns")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
corefile := `org:0 {
|
|
auto {
|
|
directory ` + tmpdir + ` db\.(.*) {1}
|
|
reload 0.1s
|
|
}
|
|
prometheus localhost:0
|
|
}`
|
|
|
|
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()
|
|
|
|
// Write db.example.org to get example.org.
|
|
if err = os.WriteFile(filepath.Join(tmpdir, "db.example.org"), []byte(zoneContent), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
time.Sleep(110 * 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_requests_total" // {zone, proto, family, type}
|
|
|
|
data := test.Scrape("http://" + metrics.ListenAddr + "/metrics")
|
|
// Get the value for the metrics where the one of the labels values matches "example.org."
|
|
got, _ := test.MetricValueLabel(metricName, "example.org.", data)
|
|
|
|
if got == "0" {
|
|
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(filepath.Join(tmpdir, "db.example.org"))
|
|
time.Sleep(110 * 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 = test.Scrape("http://" + metrics.ListenAddr + "/metrics")
|
|
got, _ = test.MetricValueLabel(metricName, "example.org.", data)
|
|
|
|
if got == "0" {
|
|
t.Errorf("Expected value %s for %s, but got %s", "> 1", metricName, got)
|
|
}
|
|
}
|
|
|
|
// Show that when 2 blocs share the same metric listener (they have a prometheus plugin on the same listening address),
|
|
// ALL the metrics of the second bloc in order are declared in prometheus, especially the plugins that are used ONLY in the second bloc
|
|
func TestMetricsSeveralBlocs(t *testing.T) {
|
|
cacheSizeMetricName := "coredns_cache_entries"
|
|
addrMetrics := "localhost:9155"
|
|
corefile := `
|
|
example.org:0 {
|
|
prometheus ` + addrMetrics + `
|
|
forward . 8.8.8.8:53 {
|
|
force_tcp
|
|
}
|
|
}
|
|
google.com:0 {
|
|
prometheus ` + addrMetrics + `
|
|
whoami
|
|
cache
|
|
}`
|
|
|
|
i, udp, _, err := CoreDNSServerAndPorts(corefile)
|
|
if err != nil {
|
|
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
|
|
}
|
|
defer i.Stop()
|
|
|
|
// send an initial query to setup properly the cache size
|
|
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)
|
|
}
|
|
|
|
beginCacheSize := test.ScrapeMetricAsInt(addrMetrics, cacheSizeMetricName, "", 0)
|
|
|
|
// send an query, different from initial to ensure we have another add to the cache
|
|
m = new(dns.Msg)
|
|
m.SetQuestion("www.google.com.", dns.TypeA)
|
|
|
|
if _, err = dns.Exchange(m, udp); err != nil {
|
|
t.Fatalf("Could not send message: %s", err)
|
|
}
|
|
|
|
endCacheSize := test.ScrapeMetricAsInt(addrMetrics, cacheSizeMetricName, "", 0)
|
|
if err != nil {
|
|
t.Errorf("Unexpected metric data retrieved for %s : %s", cacheSizeMetricName, err)
|
|
}
|
|
if endCacheSize-beginCacheSize != 1 {
|
|
t.Errorf("Expected metric data retrieved for %s, expected %d, got %d", cacheSizeMetricName, 1, endCacheSize-beginCacheSize)
|
|
}
|
|
}
|
|
|
|
func TestMetricsPluginEnabled(t *testing.T) {
|
|
corefile := `
|
|
example.org:0 {
|
|
chaos CoreDNS-001 miek@miek.nl
|
|
prometheus localhost:0
|
|
}
|
|
example.com:0 {
|
|
whoami
|
|
prometheus localhost:0
|
|
}`
|
|
|
|
srv, err := CoreDNSServer(corefile)
|
|
if err != nil {
|
|
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
|
|
}
|
|
defer srv.Stop()
|
|
|
|
metricName := "coredns_plugin_enabled" //{server, zone, name}
|
|
|
|
data := test.Scrape("http://" + metrics.ListenAddr + "/metrics")
|
|
|
|
// Get the value for the metrics where the one of the labels values matches "chaos".
|
|
got, _ := test.MetricValueLabel(metricName, "chaos", data)
|
|
|
|
if got != "1" {
|
|
t.Errorf("Expected value %s for %s, but got %s", "1", metricName, got)
|
|
}
|
|
|
|
// Get the value for the metrics where the one of the labels values matches "erratic".
|
|
got, _ = test.MetricValueLabel(metricName, "erratic", data) // none of these tests use 'erratic'
|
|
|
|
if got != "" {
|
|
t.Errorf("Expected value %s for %s, but got %s", "", metricName, got)
|
|
}
|
|
}
|
|
|
|
func TestMetricsAvailable(t *testing.T) {
|
|
procMetric := "coredns_build_info"
|
|
procCache := "coredns_cache_entries"
|
|
procCacheMiss := "coredns_cache_misses_total"
|
|
procForward := "coredns_dns_request_duration_seconds"
|
|
corefileWithMetrics := `.:0 {
|
|
prometheus localhost:0
|
|
cache
|
|
forward . 8.8.8.8 {
|
|
force_tcp
|
|
}
|
|
}`
|
|
|
|
inst, _, tcp, err := CoreDNSServerAndPorts(corefileWithMetrics)
|
|
defer inst.Stop()
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), inUse) {
|
|
return
|
|
}
|
|
t.Errorf("Could not get service instance: %s", err)
|
|
}
|
|
// send a query and check we can scrap corresponding metrics
|
|
cl := dns.Client{Net: "tcp"}
|
|
m := new(dns.Msg)
|
|
m.SetQuestion("www.example.org.", dns.TypeA)
|
|
|
|
if _, _, err := cl.Exchange(m, tcp); err != nil {
|
|
t.Fatalf("Could not send message: %s", err)
|
|
}
|
|
|
|
// we should have metrics from forward, cache, and metrics itself
|
|
if err := collectMetricsInfo(metrics.ListenAddr, procMetric, procCache, procCacheMiss, procForward); err != nil {
|
|
t.Errorf("Could not scrap one of expected stats : %s", err)
|
|
}
|
|
}
|