mirror of
https://github.com/coredns/coredns.git
synced 2025-11-30 15:44:23 -05:00
Overloaded (#1364)
* plugin/health: add 'overloaded metrics' Query our on health endpoint and record (and export as a metric) the time it takes. The Get has a 5s timeout, that, when reached, will set the metric duration to 5s. The actually call "I'm I overloaded" is left to an external entity. * README * golint and govet * and the tests
This commit is contained in:
52
plugin/health/overloaded.go
Normal file
52
plugin/health/overloaded.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// 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,
|
||||
}
|
||||
url := "http://" + h.Addr
|
||||
tick := time.NewTicker(1 * time.Second)
|
||||
|
||||
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:
|
||||
tick.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
// HealthDuration is the metric used for exporting how fast we can retrieve the /health endpoint.
|
||||
HealthDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||
Namespace: plugin.Namespace,
|
||||
Subsystem: "health",
|
||||
Name: "request_duration_seconds",
|
||||
Buckets: plugin.TimeBuckets,
|
||||
Help: "Histogram of the time (in seconds) each request took.",
|
||||
})
|
||||
)
|
||||
|
||||
var onceMetric sync.Once
|
||||
Reference in New Issue
Block a user