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