aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar yeya24 <yb532204897@gmail.com> 2019-09-19 11:38:15 -0400
committerGravatar Miek Gieben <miek@miek.nl> 2019-09-19 16:38:15 +0100
commit85e65702bd5b02c0ced5be51e02860c5ae9321aa (patch)
treeae86c5990190150201b56564fe8209a5b8607704
parent62317c3c14e306e337f7aa0507a2245495c7aed2 (diff)
downloadcoredns-85e65702bd5b02c0ced5be51e02860c5ae9321aa.tar.gz
coredns-85e65702bd5b02c0ced5be51e02860c5ae9321aa.tar.zst
coredns-85e65702bd5b02c0ced5be51e02860c5ae9321aa.zip
add host metrics (#3277)
* add host metrics Signed-off-by: yeya24 <yb532204897@gmail.com> * update hosts readme docs Signed-off-by: yeya24 <yb532204897@gmail.com>
-rw-r--r--plugin/hosts/README.md7
-rw-r--r--plugin/hosts/hostsfile.go20
-rw-r--r--plugin/hosts/setup.go7
-rw-r--r--plugin/ready/ready.go2
4 files changed, 35 insertions, 1 deletions
diff --git a/plugin/hosts/README.md b/plugin/hosts/README.md
index 044ce3671..270a8200b 100644
--- a/plugin/hosts/README.md
+++ b/plugin/hosts/README.md
@@ -72,6 +72,13 @@ hosts [FILE [ZONES...]] {
is authoritative. If specific zones are listed (for example `in-addr.arpa` and `ip6.arpa`), then only
queries for those zones will be subject to fallthrough.
+## Metrics
+
+If monitoring is enabled (via the *prometheus* directive) then the following metrics are exported:
+
+- `coredns_hosts_entries_count{}` - The combined number of entries in hosts and Corefile.
+- `coredns_hosts_reload_timestamp_seconds{}` - The timestamp of the last reload of hosts file.
+
## Examples
Load `/etc/hosts` file.
diff --git a/plugin/hosts/hostsfile.go b/plugin/hosts/hostsfile.go
index f7cc73528..ed9763de1 100644
--- a/plugin/hosts/hostsfile.go
+++ b/plugin/hosts/hostsfile.go
@@ -17,6 +17,8 @@ import (
"time"
"github.com/coredns/coredns/plugin"
+
+ "github.com/prometheus/client_golang/prometheus"
)
// parseIP calls discards any v6 zone info, before calling net.ParseIP.
@@ -135,6 +137,8 @@ func (h *Hostsfile) readHosts() {
h.mtime = stat.ModTime()
h.size = stat.Size()
+ hostsEntries.WithLabelValues().Set(float64(h.inline.Len() + h.hmap.Len()))
+ hostsReloadTime.Set(float64(stat.ModTime().UnixNano()) / 1e9)
h.Unlock()
}
@@ -252,3 +256,19 @@ func (h *Hostsfile) LookupStaticAddr(addr string) []string {
copy(hostsCp[len(hosts1):], hosts2)
return hostsCp
}
+
+var (
+ hostsEntries = prometheus.NewGaugeVec(prometheus.GaugeOpts{
+ Namespace: plugin.Namespace,
+ Subsystem: "hosts",
+ Name: "entries_count",
+ Help: "The combined number of entries in hosts and Corefile.",
+ }, []string{})
+
+ hostsReloadTime = prometheus.NewGauge(prometheus.GaugeOpts{
+ Namespace: plugin.Namespace,
+ Subsystem: "hosts",
+ Name: "reload_timestamp_seconds",
+ Help: "The timestamp of the last reload of hosts file.",
+ })
+)
diff --git a/plugin/hosts/setup.go b/plugin/hosts/setup.go
index 981ea141f..be95a547f 100644
--- a/plugin/hosts/setup.go
+++ b/plugin/hosts/setup.go
@@ -9,6 +9,7 @@ import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
+ "github.com/coredns/coredns/plugin/metrics"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/caddyserver/caddy"
@@ -57,6 +58,12 @@ func setup(c *caddy.Controller) error {
return nil
})
+ c.OnStartup(func() error {
+ metrics.MustRegister(c, hostsEntries)
+ metrics.MustRegister(c, hostsReloadTime)
+ return nil
+ })
+
c.OnShutdown(func() error {
close(parseChan)
return nil
diff --git a/plugin/ready/ready.go b/plugin/ready/ready.go
index 0b08c22f4..f40682041 100644
--- a/plugin/ready/ready.go
+++ b/plugin/ready/ready.go
@@ -45,7 +45,7 @@ func (rd *ready) onStartup() error {
ok, todo := plugins.Ready()
if ok {
w.WriteHeader(http.StatusOK)
- io.WriteString(w, http.StatusText(http.StatusOK))
+ io.WriteString(w, http.StatusText(http.StatusOK))
return
}
log.Infof("Still waiting on: %q", todo)