mirror of
https://github.com/coredns/coredns.git
synced 2025-10-28 00:34:24 -04:00
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>
This commit is contained in:
@@ -72,6 +72,13 @@ hosts [FILE [ZONES...]] {
|
|||||||
is authoritative. If specific zones are listed (for example `in-addr.arpa` and `ip6.arpa`), then only
|
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.
|
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
|
## Examples
|
||||||
|
|
||||||
Load `/etc/hosts` file.
|
Load `/etc/hosts` file.
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coredns/coredns/plugin"
|
"github.com/coredns/coredns/plugin"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// parseIP calls discards any v6 zone info, before calling net.ParseIP.
|
// parseIP calls discards any v6 zone info, before calling net.ParseIP.
|
||||||
@@ -135,6 +137,8 @@ func (h *Hostsfile) readHosts() {
|
|||||||
h.mtime = stat.ModTime()
|
h.mtime = stat.ModTime()
|
||||||
h.size = stat.Size()
|
h.size = stat.Size()
|
||||||
|
|
||||||
|
hostsEntries.WithLabelValues().Set(float64(h.inline.Len() + h.hmap.Len()))
|
||||||
|
hostsReloadTime.Set(float64(stat.ModTime().UnixNano()) / 1e9)
|
||||||
h.Unlock()
|
h.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,3 +256,19 @@ func (h *Hostsfile) LookupStaticAddr(addr string) []string {
|
|||||||
copy(hostsCp[len(hosts1):], hosts2)
|
copy(hostsCp[len(hosts1):], hosts2)
|
||||||
return hostsCp
|
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.",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/coredns/coredns/core/dnsserver"
|
"github.com/coredns/coredns/core/dnsserver"
|
||||||
"github.com/coredns/coredns/plugin"
|
"github.com/coredns/coredns/plugin"
|
||||||
|
"github.com/coredns/coredns/plugin/metrics"
|
||||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy"
|
"github.com/caddyserver/caddy"
|
||||||
@@ -57,6 +58,12 @@ func setup(c *caddy.Controller) error {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
c.OnStartup(func() error {
|
||||||
|
metrics.MustRegister(c, hostsEntries)
|
||||||
|
metrics.MustRegister(c, hostsReloadTime)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
c.OnShutdown(func() error {
|
c.OnShutdown(func() error {
|
||||||
close(parseChan)
|
close(parseChan)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func (rd *ready) onStartup() error {
|
|||||||
ok, todo := plugins.Ready()
|
ok, todo := plugins.Ready()
|
||||||
if ok {
|
if ok {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
io.WriteString(w, http.StatusText(http.StatusOK))
|
io.WriteString(w, http.StatusText(http.StatusOK))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Infof("Still waiting on: %q", todo)
|
log.Infof("Still waiting on: %q", todo)
|
||||||
|
|||||||
Reference in New Issue
Block a user