From 5260c650ec3ed2eeeca9857f598de5d9270284bd Mon Sep 17 00:00:00 2001 From: Krasi Georgiev Date: Tue, 16 Jan 2018 11:10:54 +0000 Subject: [PATCH] use the config hash for the map lookup --- cmd/prometheus/main.go | 11 ++++++++--- notifier/notifier.go | 10 +++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 303a9d242..9aa9d6d03 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -16,6 +16,8 @@ package main import ( "context" + "crypto/md5" + "encoding/json" "fmt" "net" "net/http" @@ -295,9 +297,12 @@ func main() { func(cfg *config.Config) error { c := make(map[string]sd_config.ServiceDiscoveryConfig) for _, v := range cfg.AlertingConfig.AlertmanagerConfigs { - // AlertmanagerConfigs doesn't hold an unique identifier so we use the config pointer as the identifier. - // TODO Krasi - Maybe use the slice index as the reference. - c[fmt.Sprintf("%p", v)] = v.ServiceDiscoveryConfig + // AlertmanagerConfigs doesn't hold an unique identifier so we use the config hash as the identifier. + b, err := json.Marshal(v) + if err != nil { + return err + } + c[fmt.Sprintf("%x", md5.Sum(b))] = v.ServiceDiscoveryConfig } return discoveryManagerNotify.ApplyConfig(c) }, diff --git a/notifier/notifier.go b/notifier/notifier.go index ba3e9d496..411104383 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -16,6 +16,7 @@ package notifier import ( "bytes" "context" + "crypto/md5" "encoding/json" "fmt" "net" @@ -256,9 +257,12 @@ func (n *Notifier) ApplyConfig(conf *config.Config) error { ams.metrics = n.metrics - // The config pointer is used for the map lookup identifier. - // TODO Krasi - Maybe use the slice index as the reference. - amSets[fmt.Sprintf("%p", cfg)] = ams + // The config hash is used for the map lookup identifier. + b, err := json.Marshal(cfg) + if err != nil { + return err + } + amSets[fmt.Sprintf("%x", md5.Sum(b))] = ams } n.alertmanagers = amSets