aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Zou Nengren <zounengren@cmss.chinamobile.com> 2020-03-25 21:33:04 +0800
committerGravatar GitHub <noreply@github.com> 2020-03-25 14:33:04 +0100
commit1dba31ee7d7ea5d4d02f3f2f519f25e5c9a3c379 (patch)
treebea207cbd0e67ec288f1a09c969aa484dabef46c
parent1766568398e3120c85d44f5c6237a724248b652e (diff)
downloadcoredns-1dba31ee7d7ea5d4d02f3f2f519f25e5c9a3c379.tar.gz
coredns-1dba31ee7d7ea5d4d02f3f2f519f25e5c9a3c379.tar.zst
coredns-1dba31ee7d7ea5d4d02f3f2f519f25e5c9a3c379.zip
export config file hash in a metric (#3768)
Signed-off-by: zounengren <zounengren@cmss.chinamobile.com>
-rw-r--r--plugin/reload/README.md3
-rw-r--r--plugin/reload/metrics.go7
-rw-r--r--plugin/reload/reload.go5
-rw-r--r--plugin/reload/setup.go7
4 files changed, 19 insertions, 3 deletions
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 {