export config file hash in a metric (#3768)

Signed-off-by: zounengren <zounengren@cmss.chinamobile.com>
This commit is contained in:
Zou Nengren 2020-03-25 21:33:04 +08:00 committed by GitHub
parent 1766568398
commit 1dba31ee7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 3 deletions

View file

@ -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: 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_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 ## Also See

View file

@ -14,4 +14,11 @@ var (
Name: "failed_count_total", Name: "failed_count_total",
Help: "Counter of the number of failed reload attempts.", 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"})
) )

View file

@ -4,12 +4,14 @@ package reload
import ( import (
"bytes" "bytes"
"crypto/md5" "crypto/md5"
"encoding/hex"
"encoding/json" "encoding/json"
"sync" "sync"
"time" "time"
"github.com/caddyserver/caddy" "github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyfile" "github.com/caddyserver/caddy/caddyfile"
"github.com/prometheus/client_golang/prometheus"
) )
const ( const (
@ -61,7 +63,6 @@ func hook(event caddy.EventName, info interface{}) error {
if event != caddy.InstanceStartupEvent { if event != caddy.InstanceStartupEvent {
return nil return nil
} }
// if reload is removed from the Corefile, then the hook // if reload is removed from the Corefile, then the hook
// is still registered but setup is never called again // is still registered but setup is never called again
// so we need a flag to tell us not to reload // 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) s := md5.Sum(parsedCorefile)
if s != md5sum { 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. // Let not try to restart with the same file, even though it is wrong.
md5sum = s md5sum = s
// now lets consider that plugin will not be reload, unless appear in next config file // 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 // change status of usage will be reset in setup if the plugin appears in config file
r.setUsage(maybeUsed) r.setUsage(maybeUsed)
_, err := instance.Restart(corefile) _, err := instance.Restart(corefile)
reloadInfo.WithLabelValues("md5", hex.EncodeToString(md5sum[:])).Set(1)
if err != nil { if err != nil {
log.Errorf("Corefile changed but reload failed: %s", err) log.Errorf("Corefile changed but reload failed: %s", err)
FailedCount.Add(1) FailedCount.Add(1)

View file

@ -7,6 +7,7 @@ import (
"time" "time"
"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"
@ -67,11 +68,13 @@ func setup(c *caddy.Controller) error {
// prepare info for next onInstanceStartup event // prepare info for next onInstanceStartup event
r.setInterval(i) r.setInterval(i)
r.setUsage(used) r.setUsage(used)
once.Do(func() { once.Do(func() {
caddy.RegisterEventHook("reload", hook) 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 // re-register on finalShutDown as the instance most-likely will be changed
shutOnce.Do(func() { shutOnce.Do(func() {
c.OnFinalShutdown(func() error { c.OnFinalShutdown(func() error {