From db63a4bd2a3ca00831815aadc967c31440c0002b Mon Sep 17 00:00:00 2001 From: Roman Vynar Date: Mon, 26 Sep 2016 22:20:56 +0300 Subject: [PATCH] Do not fail Consul discovery on Prometheus startup when Consul is down. --- retrieval/discovery/consul/consul.go | 32 +++++++++++++++------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/retrieval/discovery/consul/consul.go b/retrieval/discovery/consul/consul.go index 28536b59c..75d81704b 100644 --- a/retrieval/discovery/consul/consul.go +++ b/retrieval/discovery/consul/consul.go @@ -77,21 +77,11 @@ func NewDiscovery(conf *config.ConsulSDConfig) (*Discovery, error) { return nil, err } cd := &Discovery{ - client: client, - clientConf: clientConf, - tagSeparator: conf.TagSeparator, - watchedServices: conf.Services, - } - // If the datacenter isn't set in the clientConf, let's get it from the local Consul agent - // (Consul default is to use local node's datacenter if one isn't given for a query). - if clientConf.Datacenter == "" { - info, err := client.Agent().Self() - if err != nil { - return nil, err - } - cd.clientDatacenter = info["Config"]["Datacenter"].(string) - } else { - cd.clientDatacenter = clientConf.Datacenter + client: client, + clientConf: clientConf, + tagSeparator: conf.TagSeparator, + watchedServices: conf.Services, + clientDatacenter: clientConf.Datacenter, } return cd, nil } @@ -144,6 +134,18 @@ func (cd *Discovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) { } lastIndex = meta.LastIndex + // If the datacenter was not set from clientConf, let's get it from the local Consul agent + // (Consul default is to use local node's datacenter if one isn't given for a query). + if cd.clientDatacenter == "" { + info, err := cd.client.Agent().Self() + if err != nil { + log.Errorf("Error retrieving datacenter name: %s", err) + time.Sleep(retryInterval) + continue + } + cd.clientDatacenter = info["Config"]["Datacenter"].(string) + } + // Check for new services. for name := range srvs { if !cd.shouldWatch(name) {