Add config hash metric (#751)

This commit is contained in:
Frederic Branczyk 2017-04-27 20:58:15 +02:00 committed by Fabian Reinartz
parent cec7341ce7
commit 3eb81b4243
1 changed files with 24 additions and 6 deletions

View File

@ -14,6 +14,8 @@
package main
import (
"crypto/md5"
"encoding/binary"
"flag"
"fmt"
"io/ioutil"
@ -52,21 +54,24 @@ import (
)
var (
configHash = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "alertmanager_config_hash",
Help: "Hash of the currently loaded alertmanager configuration.",
})
configSuccess = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "alertmanager",
Name: "config_last_reload_successful",
Help: "Whether the last configuration reload attempt was successful.",
Name: "alertmanager_config_last_reload_successful",
Help: "Whether the last configuration reload attempt was successful.",
})
configSuccessTime = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "alertmanager",
Name: "config_last_reload_success_timestamp_seconds",
Help: "Timestamp of the last successful configuration reload.",
Name: "alertmanager_config_last_reload_success_timestamp_seconds",
Help: "Timestamp of the last successful configuration reload.",
})
)
func init() {
prometheus.MustRegister(configSuccess)
prometheus.MustRegister(configSuccessTime)
prometheus.MustRegister(configHash)
prometheus.MustRegister(version.NewCollector("alertmanager"))
}
@ -196,6 +201,7 @@ func main() {
return d + waitFunc()
}
var hash float64
reload := func() (err error) {
log.With("file", *configFile).Infof("Loading configuration file")
defer func() {
@ -205,6 +211,7 @@ func main() {
} else {
configSuccess.Set(1)
configSuccessTime.Set(float64(time.Now().Unix()))
configHash.Set(hash)
}
}()
@ -213,6 +220,8 @@ func main() {
return err
}
hash = md5HashAsMetricValue([]byte(conf.String()))
err = apiv.Update(conf.String(), time.Duration(conf.Global.ResolveTimeout))
if err != nil {
return err
@ -427,3 +436,12 @@ func mustHostname() string {
}
return hostname
}
func md5HashAsMetricValue(data []byte) float64 {
sum := md5.Sum(data)
// We only want 48 bits as a float64 only has a 53 bit mantissa.
smallSum := sum[0:6]
var bytes = make([]byte, 8)
copy(bytes, smallSum)
return float64(binary.LittleEndian.Uint64(bytes))
}