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

@ -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)