mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-30 09:43:17 -04:00 
			
		
		
		
	middleware/metrics: export ListenAddr (#366)
ListenAddr is the address where the prometheus metric are exported. This can be used in tests to listen on "localhost:0" and then later retrieve the metrics from there. It makes the tests indepent on each other.
This commit is contained in:
		| @@ -61,6 +61,7 @@ func (m *Metrics) OnStartup() error { | |||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		m.ln = ln | 		m.ln = ln | ||||||
|  | 		ListenAddr = m.ln.Addr().String() | ||||||
|  |  | ||||||
| 		m.mux = http.NewServeMux() | 		m.mux = http.NewServeMux() | ||||||
|  |  | ||||||
| @@ -97,3 +98,7 @@ func keys(m map[string]bool) []string { | |||||||
| 	} | 	} | ||||||
| 	return sx | 	return sx | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // ListenAddr is assigned the address of the prometheus listener. Its use is mainly in tests where | ||||||
|  | // we listen on "localhost:0" and need to retrieve the actual address. | ||||||
|  | var ListenAddr string | ||||||
|   | |||||||
| @@ -13,7 +13,7 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func TestMetrics(t *testing.T) { | func TestMetrics(t *testing.T) { | ||||||
| 	met := &Metrics{Addr: Addr, zoneMap: make(map[string]bool)} | 	met := &Metrics{Addr: "localhost:0", zoneMap: make(map[string]bool)} | ||||||
| 	if err := met.OnStartup(); err != nil { | 	if err := met.OnStartup(); err != nil { | ||||||
| 		t.Fatalf("Failed to start metrics handler: %s", err) | 		t.Fatalf("Failed to start metrics handler: %s", err) | ||||||
| 	} | 	} | ||||||
| @@ -71,7 +71,7 @@ func TestMetrics(t *testing.T) { | |||||||
| 			t.Fatalf("Test %d: Expected no error, but got %s", i, err) | 			t.Fatalf("Test %d: Expected no error, but got %s", i, err) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		result := mtest.Scrape(t, "http://"+Addr+"/metrics") | 		result := mtest.Scrape(t, "http://"+ListenAddr+"/metrics") | ||||||
|  |  | ||||||
| 		if tc.expectedValue != "" { | 		if tc.expectedValue != "" { | ||||||
| 			got, _ := mtest.MetricValue(tc.metric, result) | 			got, _ := mtest.MetricValue(tc.metric, result) | ||||||
|   | |||||||
| @@ -38,7 +38,7 @@ func setup(c *caddy.Controller) error { | |||||||
|  |  | ||||||
| func prometheusParse(c *caddy.Controller) (*Metrics, error) { | func prometheusParse(c *caddy.Controller) (*Metrics, error) { | ||||||
| 	var ( | 	var ( | ||||||
| 		met = &Metrics{Addr: Addr, zoneMap: make(map[string]bool)} | 		met = &Metrics{Addr: addr, zoneMap: make(map[string]bool)} | ||||||
| 		err error | 		err error | ||||||
| 	) | 	) | ||||||
|  |  | ||||||
| @@ -88,4 +88,4 @@ func prometheusParse(c *caddy.Controller) (*Metrics, error) { | |||||||
| var metricsOnce sync.Once | var metricsOnce sync.Once | ||||||
|  |  | ||||||
| // Addr is the address the where the metrics are exported by default. | // Addr is the address the where the metrics are exported by default. | ||||||
| const Addr = "localhost:9153" | const addr = "localhost:9153" | ||||||
|   | |||||||
| @@ -20,12 +20,12 @@ import ( | |||||||
| func TestMetricsServer(t *testing.T) { | func TestMetricsServer(t *testing.T) { | ||||||
| 	corefile := `example.org:0 { | 	corefile := `example.org:0 { | ||||||
| 	chaos CoreDNS-001 miek@miek.nl | 	chaos CoreDNS-001 miek@miek.nl | ||||||
| 	prometheus | 	prometheus localhost:0 | ||||||
| } | } | ||||||
|  |  | ||||||
| example.com:0 { | example.com:0 { | ||||||
| 	proxy . 8.8.4.4:53 | 	proxy . 8.8.4.4:53 | ||||||
| 	prometheus | 	prometheus localhost:0 | ||||||
| } | } | ||||||
| ` | ` | ||||||
| 	srv, err := CoreDNSServer(corefile) | 	srv, err := CoreDNSServer(corefile) | ||||||
| @@ -40,7 +40,7 @@ func TestMetricsRefused(t *testing.T) { | |||||||
|  |  | ||||||
| 	corefile := `example.org:0 { | 	corefile := `example.org:0 { | ||||||
| 	proxy . 8.8.8.8:53 | 	proxy . 8.8.8.8:53 | ||||||
| 	prometheus | 	prometheus localhost:0 | ||||||
| } | } | ||||||
| ` | ` | ||||||
| 	srv, err := CoreDNSServer(corefile) | 	srv, err := CoreDNSServer(corefile) | ||||||
| @@ -58,7 +58,7 @@ func TestMetricsRefused(t *testing.T) { | |||||||
| 		t.Fatalf("Could not send message: %s", err) | 		t.Fatalf("Could not send message: %s", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	data := mtest.Scrape(t, "http://"+metrics.Addr+"/metrics") | 	data := mtest.Scrape(t, "http://"+metrics.ListenAddr+"/metrics") | ||||||
| 	got, labels := mtest.MetricValue(metricName, data) | 	got, labels := mtest.MetricValue(metricName, data) | ||||||
|  |  | ||||||
| 	if got != "1" { | 	if got != "1" { | ||||||
| @@ -77,7 +77,7 @@ func TestMetricsCache(t *testing.T) { | |||||||
|  |  | ||||||
| 	corefile := `example.net:0 { | 	corefile := `example.net:0 { | ||||||
| 	proxy . 8.8.8.8:53 | 	proxy . 8.8.8.8:53 | ||||||
| 	prometheus | 	prometheus localhost:0 | ||||||
| 	cache | 	cache | ||||||
| } | } | ||||||
| ` | ` | ||||||
| @@ -96,7 +96,7 @@ func TestMetricsCache(t *testing.T) { | |||||||
| 		t.Fatalf("Could not send message: %s", err) | 		t.Fatalf("Could not send message: %s", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	data := mtest.Scrape(t, "http://"+metrics.Addr+"/metrics") | 	data := mtest.Scrape(t, "http://"+metrics.ListenAddr+"/metrics") | ||||||
| 	// Get the value for the metrics where the one of the labels values matches "success" | 	// Get the value for the metrics where the one of the labels values matches "success" | ||||||
| 	got, _ := mtest.MetricValueLabel(metricName, cache.Success, data) | 	got, _ := mtest.MetricValueLabel(metricName, cache.Success, data) | ||||||
|  |  | ||||||
| @@ -111,12 +111,11 @@ func TestMetricsAuto(t *testing.T) { | |||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// TODO(miek): Random port as string and use that later? |  | ||||||
| 	corefile := `org:0 { | 	corefile := `org:0 { | ||||||
| 		auto { | 		auto { | ||||||
| 			directory ` + tmpdir + ` db\.(.*) {1} 1 | 			directory ` + tmpdir + ` db\.(.*) {1} 1 | ||||||
| 		} | 		} | ||||||
| 		prometheus | 		prometheus localhost:0 | ||||||
| 	} | 	} | ||||||
| ` | ` | ||||||
|  |  | ||||||
| @@ -149,7 +148,7 @@ func TestMetricsAuto(t *testing.T) { | |||||||
|  |  | ||||||
| 	metricName := "coredns_dns_request_count_total" //{zone, proto, family} | 	metricName := "coredns_dns_request_count_total" //{zone, proto, family} | ||||||
|  |  | ||||||
| 	data := mtest.Scrape(t, "http://"+metrics.Addr+"/metrics") | 	data := mtest.Scrape(t, "http://"+metrics.ListenAddr+"/metrics") | ||||||
| 	// Get the value for the metrics where the one of the labels values matches "example.org." | 	// Get the value for the metrics where the one of the labels values matches "example.org." | ||||||
| 	got, _ := mtest.MetricValueLabel(metricName, "example.org.", data) | 	got, _ := mtest.MetricValueLabel(metricName, "example.org.", data) | ||||||
|  |  | ||||||
| @@ -164,7 +163,7 @@ func TestMetricsAuto(t *testing.T) { | |||||||
| 		t.Fatalf("Could not send message: %s", err) | 		t.Fatalf("Could not send message: %s", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	data = mtest.Scrape(t, "http://"+metrics.Addr+"/metrics") | 	data = mtest.Scrape(t, "http://"+metrics.ListenAddr+"/metrics") | ||||||
| 	got, _ = mtest.MetricValueLabel(metricName, "example.org.", data) | 	got, _ = mtest.MetricValueLabel(metricName, "example.org.", data) | ||||||
|  |  | ||||||
| 	if got != "1" { | 	if got != "1" { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user