aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2016-04-06 09:21:46 +0100
committerGravatar Miek Gieben <miek@miek.nl> 2016-04-06 09:21:46 +0100
commit68171c7a638a4087deac495b39ca7f539ed93673 (patch)
tree8844c95249964d57122d1fc779882515d6f4f5f8
parentecb53addd6f76206e6070e2312d6a48435c450e7 (diff)
downloadcoredns-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.
-rw-r--r--core/directives.go1
-rw-r--r--core/setup/health.go33
-rw-r--r--core/setup/prometheus.go2
-rw-r--r--middleware/health/README.md21
-rw-r--r--middleware/health/health.go38
-rw-r--r--middleware/prometheus/README.md9
-rw-r--r--middleware/prometheus/metrics.go7
7 files changed, 103 insertions, 8 deletions
diff --git a/core/directives.go b/core/directives.go
index af11adb4d..fbbba40c7 100644
--- a/core/directives.go
+++ b/core/directives.go
@@ -45,6 +45,7 @@ var directiveOrder = []directive{
{"root", setup.Root},
{"bind", setup.BindHost},
{"tls", https.Setup},
+ {"health", setup.Health},
// Other directives that don't create HTTP handlers
{"startup", setup.Startup},
diff --git a/core/setup/health.go b/core/setup/health.go
new file mode 100644
index 000000000..608147b13
--- /dev/null
+++ b/core/setup/health.go
@@ -0,0 +1,33 @@
+package setup
+
+import (
+ "github.com/miekg/coredns/middleware"
+ "github.com/miekg/coredns/middleware/health"
+)
+
+func Health(c *Controller) (middleware.Middleware, error) {
+ addr, err := parseHealth(c)
+ if err != nil {
+ return nil, err
+ }
+
+ h := health.Health{Addr: addr}
+ c.Startup = append(c.Startup, h.ListenAndServe)
+ return nil, nil
+}
+
+func parseHealth(c *Controller) (string, error) {
+ addr := ""
+ for c.Next() {
+ args := c.RemainingArgs()
+
+ switch len(args) {
+ case 0:
+ case 1:
+ addr = args[0]
+ default:
+ return "", c.ArgErr()
+ }
+ }
+ return addr, nil
+}
diff --git a/core/setup/prometheus.go b/core/setup/prometheus.go
index fe2fe47d6..9ab68b6a5 100644
--- a/core/setup/prometheus.go
+++ b/core/setup/prometheus.go
@@ -9,7 +9,7 @@ import (
const (
path = "/metrics"
- addr = "localhost:9135" // 9153 is occopied by bind_exporter
+ addr = "localhost:9135" // 9153 is occupied by bind_exporter
)
var once sync.Once
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"
+)
diff --git a/middleware/prometheus/README.md b/middleware/prometheus/README.md
index d03678e7c..3b16b3b94 100644
--- a/middleware/prometheus/README.md
+++ b/middleware/prometheus/README.md
@@ -9,9 +9,10 @@ The following metrics are exported:
* coredns_dns_response_size_bytes
* coredns_dns_response_rcode_count_total
-Each counter has a label `zone` which is the zonename used for the request/response.
-The `request_count` metrics has an extra label `qtype` which holds the qtype. And
-`rcode_count` has an extra label which has the rcode.
+Each counter has a label `zone` which is the zonename used for the request/response,
+and a label `qtype` which old the query type.
+The `response_rcode_count_total` has an extra label `rcode` which holds the rcode
+of the response.
Restarting CoreDNS will stop the monitoring. This is a bug. Also [this upstream
Caddy bug](https://github.com/mholt/caddy/issues/675).
@@ -25,6 +26,6 @@ prometheus
For each zone that you want to see metrics for.
It optionally takes an address where the metrics are exported, the default
-is `localhost:9154`. The metrics path is fixed to `/metrics`.
+is `localhost:9135`. The metrics path is fixed to `/metrics`.
## Examples
diff --git a/middleware/prometheus/metrics.go b/middleware/prometheus/metrics.go
index 00db6e791..b47d06ff8 100644
--- a/middleware/prometheus/metrics.go
+++ b/middleware/prometheus/metrics.go
@@ -1,7 +1,7 @@
package metrics
import (
- "fmt"
+ "log"
"net/http"
"sync"
@@ -39,8 +39,9 @@ func (m *Metrics) Start() error {
http.Handle(path, prometheus.Handler())
go func() {
- // TODO(miek): Logging here?
- fmt.Errorf("%s", http.ListenAndServe(m.Addr, nil))
+ if err := http.ListenAndServe(m.Addr, nil); err != nil {
+ log.Printf("[ERROR] Failed to start prometheus handler: %s", err)
+ }
}()
})
return nil