diff --git a/plugin/reload/README.md b/plugin/reload/README.md index 8e58d1e73..9809cdbaa 100644 --- a/plugin/reload/README.md +++ b/plugin/reload/README.md @@ -99,6 +99,9 @@ CoreDNS v1.7.0 and later does parse the Corefile and supports detecting changes If monitoring is enabled (via the *prometheus* plugin) then the following metric is exported: * `coredns_reload_failed_count_total{}` - counts the number of failed reload attempts. +* `coredns_reload_version_info{hash, value}` - record the hash value during reload. + +Currently the type of `hash` is "md5", the `value` is the returned hash value. ## Also See diff --git a/plugin/reload/metrics.go b/plugin/reload/metrics.go index 1f790d4c1..de46862a4 100644 --- a/plugin/reload/metrics.go +++ b/plugin/reload/metrics.go @@ -14,4 +14,11 @@ var ( Name: "failed_count_total", Help: "Counter of the number of failed reload attempts.", }) + + reloadInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: plugin.Namespace, + Subsystem: "reload", + Name: "version_info", + Help: "A metric with a constant '1' value labeled by hash, and value which type of hash generated.", + }, []string{"hash", "value"}) ) diff --git a/plugin/reload/reload.go b/plugin/reload/reload.go index 817582c8c..0b58d2a8e 100644 --- a/plugin/reload/reload.go +++ b/plugin/reload/reload.go @@ -4,12 +4,14 @@ package reload import ( "bytes" "crypto/md5" + "encoding/hex" "encoding/json" "sync" "time" "github.com/caddyserver/caddy" "github.com/caddyserver/caddy/caddyfile" + "github.com/prometheus/client_golang/prometheus" ) const ( @@ -61,7 +63,6 @@ func hook(event caddy.EventName, info interface{}) error { if event != caddy.InstanceStartupEvent { return nil } - // if reload is removed from the Corefile, then the hook // is still registered but setup is never called again // so we need a flag to tell us not to reload @@ -96,12 +97,14 @@ func hook(event caddy.EventName, info interface{}) error { } s := md5.Sum(parsedCorefile) if s != md5sum { + reloadInfo.Delete(prometheus.Labels{"hash": "md5", "value": hex.EncodeToString(md5sum[:])}) // Let not try to restart with the same file, even though it is wrong. md5sum = s // now lets consider that plugin will not be reload, unless appear in next config file // change status of usage will be reset in setup if the plugin appears in config file r.setUsage(maybeUsed) _, err := instance.Restart(corefile) + reloadInfo.WithLabelValues("md5", hex.EncodeToString(md5sum[:])).Set(1) if err != nil { log.Errorf("Corefile changed but reload failed: %s", err) FailedCount.Add(1) diff --git a/plugin/reload/setup.go b/plugin/reload/setup.go index c0dcc14e9..b0ba3e1c4 100644 --- a/plugin/reload/setup.go +++ b/plugin/reload/setup.go @@ -7,6 +7,7 @@ import ( "time" "github.com/coredns/coredns/plugin" + "github.com/coredns/coredns/plugin/metrics" clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/caddyserver/caddy" @@ -67,11 +68,13 @@ func setup(c *caddy.Controller) error { // prepare info for next onInstanceStartup event r.setInterval(i) r.setUsage(used) - once.Do(func() { caddy.RegisterEventHook("reload", hook) + c.OnRestart(func() error { + metrics.MustRegister(c, reloadInfo) + return nil + }) }) - // re-register on finalShutDown as the instance most-likely will be changed shutOnce.Do(func() { c.OnFinalShutdown(func() error {