diff options
author | 2016-04-06 09:21:46 +0100 | |
---|---|---|
committer | 2016-04-06 09:21:46 +0100 | |
commit | 68171c7a638a4087deac495b39ca7f539ed93673 (patch) | |
tree | 8844c95249964d57122d1fc779882515d6f4f5f8 /middleware/health | |
parent | ecb53addd6f76206e6070e2312d6a48435c450e7 (diff) | |
download | coredns-68171c7a638a4087deac495b39ca7f539ed93673.tar.gz coredns-68171c7a638a4087deac495b39ca7f539ed93673.tar.zst coredns-68171c7a638a4087deac495b39ca7f539ed93673.zip |
A health middleware
Start http handler on port 8080 and return OK. Also add some
documentation fixes for the prometheus middleware.
Diffstat (limited to 'middleware/health')
-rw-r--r-- | middleware/health/README.md | 21 | ||||
-rw-r--r-- | middleware/health/health.go | 38 |
2 files changed, 59 insertions, 0 deletions
diff --git a/middleware/health/README.md b/middleware/health/README.md new file mode 100644 index 000000000..3bfb5bb5f --- /dev/null +++ b/middleware/health/README.md @@ -0,0 +1,21 @@ +# health + +This module enables a simple health check. + +By default it will listen on port 9180. + +Restarting CoreDNS will stop the health checking. This is a bug. Also [this upstream +Caddy bug](https://github.com/mholt/caddy/issues/675). + +## Syntax + +~~~ +health +~~~ + +It optionally takes an address, the default is `:8080`. The health path is fixed to `/health`. It +will just return "OK", when CoreDNS is healthy. + +This middleware only needs to be enabled once. + +## Examples diff --git a/middleware/health/health.go b/middleware/health/health.go new file mode 100644 index 000000000..8c1665e57 --- /dev/null +++ b/middleware/health/health.go @@ -0,0 +1,38 @@ +package health + +import ( + "io" + "log" + "net/http" + "sync" +) + +var once sync.Once + +type Health struct { + Addr string +} + +func health(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, ok) +} + +func (h Health) ListenAndServe() error { + if h.Addr == "" { + h.Addr = defAddr + } + once.Do(func() { + http.HandleFunc("/health", health) + go func() { + if err := http.ListenAndServe(h.Addr, nil); err != nil { + log.Printf("[ERROR] Failed to start health handler: %s", err) + } + }() + }) + return nil +} + +const ( + ok = "OK" + defAddr = ":8080" +) |