| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | package health
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2022-04-13 19:09:03 +02:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2022-06-17 15:49:53 -04:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 	"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.
 | 
					
						
							| 
									
										
										
										
											2022-04-13 19:09:03 +02:00
										 |  |  | func (h *health) overloaded(ctx context.Context) {
 | 
					
						
							| 
									
										
										
										
											2022-06-17 15:49:53 -04:00
										 |  |  | 	bypassProxy := &http.Transport{
 | 
					
						
							|  |  |  | 		Proxy: nil,
 | 
					
						
							|  |  |  | 		DialContext: (&net.Dialer{
 | 
					
						
							|  |  |  | 			Timeout:   30 * time.Second,
 | 
					
						
							|  |  |  | 			KeepAlive: 30 * time.Second,
 | 
					
						
							|  |  |  | 		}).DialContext,
 | 
					
						
							|  |  |  | 		ForceAttemptHTTP2:     true,
 | 
					
						
							|  |  |  | 		MaxIdleConns:          100,
 | 
					
						
							|  |  |  | 		IdleConnTimeout:       90 * time.Second,
 | 
					
						
							|  |  |  | 		TLSHandshakeTimeout:   10 * time.Second,
 | 
					
						
							|  |  |  | 		ExpectContinueTimeout: 1 * time.Second,
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-04-13 19:09:03 +02:00
										 |  |  | 	timeout := 3 * time.Second
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 	client := http.Client{
 | 
					
						
							| 
									
										
										
										
											2022-06-17 15:49:53 -04:00
										 |  |  | 		Timeout:   timeout,
 | 
					
						
							|  |  |  | 		Transport: bypassProxy,
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-04-13 19:09:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-29 06:57:54 -07:00
										 |  |  | 	req, _ := http.NewRequestWithContext(ctx, http.MethodGet, h.healthURI.String(), nil)
 | 
					
						
							| 
									
										
										
										
											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()
 | 
					
						
							| 
									
										
										
										
											2022-04-13 19:09:03 +02:00
										 |  |  | 			resp, err := client.Do(req)
 | 
					
						
							|  |  |  | 			if err != nil && ctx.Err() == context.Canceled {
 | 
					
						
							|  |  |  | 				// request was cancelled by parent goroutine
 | 
					
						
							|  |  |  | 				return
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 			if err != nil {
 | 
					
						
							| 
									
										
										
										
											2021-05-27 15:16:38 +02:00
										 |  |  | 				HealthDuration.Observe(time.Since(start).Seconds())
 | 
					
						
							|  |  |  | 				HealthFailures.Inc()
 | 
					
						
							| 
									
										
										
										
											2023-03-29 06:57:54 -07:00
										 |  |  | 				log.Warningf("Local health request to %q failed: %s", req.URL.String(), err)
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			resp.Body.Close()
 | 
					
						
							| 
									
										
										
										
											2021-03-19 11:40:38 +01:00
										 |  |  | 			elapsed := time.Since(start)
 | 
					
						
							|  |  |  | 			HealthDuration.Observe(elapsed.Seconds())
 | 
					
						
							|  |  |  | 			if elapsed > time.Second { // 1s is pretty random, but a *local* scrape taking that long isn't good
 | 
					
						
							| 
									
										
										
										
											2023-03-29 06:57:54 -07:00
										 |  |  | 				log.Warningf("Local health request to %q took more than 1s: %s", req.URL.String(), elapsed)
 | 
					
						
							| 
									
										
										
										
											2021-03-19 11:40:38 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 19:09:03 +02:00
										 |  |  | 		case <-ctx.Done():
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 			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{
 | 
					
						
							| 
									
										
										
										
											2024-03-11 21:09:09 +01:00
										 |  |  | 		Namespace:                   plugin.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem:                   "health",
 | 
					
						
							|  |  |  | 		Name:                        "request_duration_seconds",
 | 
					
						
							|  |  |  | 		Buckets:                     plugin.SlimTimeBuckets,
 | 
					
						
							|  |  |  | 		NativeHistogramBucketFactor: plugin.NativeHistogramBucketFactor,
 | 
					
						
							|  |  |  | 		Help:                        "Histogram of the time (in seconds) each request took.",
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2021-11-15 20:29:52 +08:00
										 |  |  | 	// HealthFailures is the metric used to count how many times the health request failed
 | 
					
						
							| 
									
										
										
										
											2021-05-27 15:16:38 +02:00
										 |  |  | 	HealthFailures = promauto.NewCounter(prometheus.CounterOpts{
 | 
					
						
							|  |  |  | 		Namespace: plugin.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: "health",
 | 
					
						
							|  |  |  | 		Name:      "request_failures_total",
 | 
					
						
							|  |  |  | 		Help:      "The number of times the health check failed.",
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | )
 |