From 48e461f7db5f46821db0944160b24038f53e7204 Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Sun, 16 Aug 2015 15:02:02 -0700 Subject: [PATCH] Pass through current agent Consul datacenter name Instead of only filling __meta_consul_dc when datacenter is set in consul_sd_config this change fills the label based on what the agent reports it's current data center is, if datacenter isn't manually set, otherwise it uses whatever datacenter was set to. --- retrieval/discovery/consul.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/retrieval/discovery/consul.go b/retrieval/discovery/consul.go index 587af210c..8be65b0d0 100644 --- a/retrieval/discovery/consul.go +++ b/retrieval/discovery/consul.go @@ -52,10 +52,11 @@ const ( // ConsulDiscovery retrieves target information from a Consul server // and updates them via watches. type ConsulDiscovery struct { - client *consul.Client - clientConf *consul.Config - tagSeparator string - scrapedServices map[string]struct{} + client *consul.Client + clientConf *consul.Config + clientDatacenter string + tagSeparator string + scrapedServices map[string]struct{} mu sync.RWMutex services map[string]*consulService @@ -95,6 +96,17 @@ func NewConsulDiscovery(conf *config.ConsulSDConfig) *ConsulDiscovery { scrapedServices: map[string]struct{}{}, services: map[string]*consulService{}, } + // 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 { + panic(fmt.Errorf("discovery.NewConsulDiscovery: %s", err)) + } + cd.clientDatacenter = info["Config"]["Datacenter"].(string) + } else { + cd.clientDatacenter = clientConf.Datacenter + } for _, name := range conf.Services { cd.scrapedServices[name] = struct{}{} } @@ -214,7 +226,7 @@ func (cd *ConsulDiscovery) watchServices(update chan<- *consulService, done <-ch } srv.tgroup.Labels = clientmodel.LabelSet{ ConsulServiceLabel: clientmodel.LabelValue(name), - ConsulDCLabel: clientmodel.LabelValue(cd.clientConf.Datacenter), + ConsulDCLabel: clientmodel.LabelValue(cd.clientDatacenter), } update <- srv }