| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | package health
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"net/http"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus"
 | 
					
						
							| 
									
										
										
										
											2020-07-25 23:06:28 +08:00
										 |  |  | 	"github.com/prometheus/client_golang/prometheus/promauto"
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // overloaded queries the health end point and updates a metrics showing how long it took.
 | 
					
						
							|  |  |  | func (h *health) overloaded() {
 | 
					
						
							|  |  |  | 	timeout := time.Duration(5 * time.Second)
 | 
					
						
							|  |  |  | 	client := http.Client{
 | 
					
						
							|  |  |  | 		Timeout: timeout,
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-10-27 10:15:42 +02:00
										 |  |  | 	url := "http://" + h.Addr + "/health"
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 	tick := time.NewTicker(1 * time.Second)
 | 
					
						
							| 
									
										
										
										
											2019-05-04 21:06:04 +01:00
										 |  |  | 	defer tick.Stop()
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for {
 | 
					
						
							|  |  |  | 		select {
 | 
					
						
							|  |  |  | 		case <-tick.C:
 | 
					
						
							|  |  |  | 			start := time.Now()
 | 
					
						
							|  |  |  | 			resp, err := client.Get(url)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				HealthDuration.Observe(timeout.Seconds())
 | 
					
						
							|  |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			resp.Body.Close()
 | 
					
						
							|  |  |  | 			HealthDuration.Observe(time.Since(start).Seconds())
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case <-h.stop:
 | 
					
						
							|  |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var (
 | 
					
						
							|  |  |  | 	// HealthDuration is the metric used for exporting how fast we can retrieve the /health endpoint.
 | 
					
						
							| 
									
										
										
										
											2020-07-25 23:06:28 +08:00
										 |  |  | 	HealthDuration = promauto.NewHistogram(prometheus.HistogramOpts{
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 		Namespace: plugin.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: "health",
 | 
					
						
							|  |  |  | 		Name:      "request_duration_seconds",
 | 
					
						
							|  |  |  | 		Buckets:   plugin.TimeBuckets,
 | 
					
						
							|  |  |  | 		Help:      "Histogram of the time (in seconds) each request took.",
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | )
 |