mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -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
|
||||
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.
|
||||
|
||||
@@ -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.",
|
||||
})
|
||||
)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user