From 19a40faf8e68fb04345decef077a736a6690e95e Mon Sep 17 00:00:00 2001 From: Nikolay Date: Tue, 23 Mar 2021 16:35:01 +0300 Subject: [PATCH] changes consul_service label value (#1143) according to prometheus discovery. It should mitigate issue with case sensetive services https://github.com/hashicorp/consul/issues/5707 --- lib/promscrape/discovery/consul/service_node.go | 10 ++++++---- lib/promscrape/discovery/consul/service_node_test.go | 2 +- lib/promscrape/discovery/consul/watch.go | 8 ++++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/promscrape/discovery/consul/service_node.go b/lib/promscrape/discovery/consul/service_node.go index 4086bd3b7..8023ecfd7 100644 --- a/lib/promscrape/discovery/consul/service_node.go +++ b/lib/promscrape/discovery/consul/service_node.go @@ -13,8 +13,10 @@ import ( func getServiceNodesLabels(cfg *apiConfig) []map[string]string { sns := cfg.consulWatcher.getServiceNodesSnapshot() var ms []map[string]string - for _, sn := range sns { - ms = sn.appendTargetLabels(ms, cfg.tagSeparator) + for svc, sn := range sns { + for i := range sn { + ms = sn[i].appendTargetLabels(ms, svc, cfg.tagSeparator) + } } return ms } @@ -67,7 +69,7 @@ func parseServiceNodes(data []byte) ([]ServiceNode, error) { return sns, nil } -func (sn *ServiceNode) appendTargetLabels(ms []map[string]string, tagSeparator string) []map[string]string { +func (sn *ServiceNode) appendTargetLabels(ms []map[string]string, serviceName, tagSeparator string) []map[string]string { var addr string if sn.Service.Address != "" { addr = discoveryutils.JoinHostPort(sn.Service.Address, sn.Service.Port) @@ -80,7 +82,7 @@ func (sn *ServiceNode) appendTargetLabels(ms []map[string]string, tagSeparator s "__meta_consul_dc": sn.Node.Datacenter, "__meta_consul_health": aggregatedStatus(sn.Checks), "__meta_consul_node": sn.Node.Node, - "__meta_consul_service": sn.Service.Service, + "__meta_consul_service": serviceName, "__meta_consul_service_address": sn.Service.Address, "__meta_consul_service_id": sn.Service.ID, "__meta_consul_service_port": strconv.Itoa(sn.Service.Port), diff --git a/lib/promscrape/discovery/consul/service_node_test.go b/lib/promscrape/discovery/consul/service_node_test.go index 0b65a1f20..7b5e3dc08 100644 --- a/lib/promscrape/discovery/consul/service_node_test.go +++ b/lib/promscrape/discovery/consul/service_node_test.go @@ -106,7 +106,7 @@ func TestParseServiceNodesSuccess(t *testing.T) { // Check sn.appendTargetLabels() tagSeparator := "," - labelss := sn.appendTargetLabels(nil, tagSeparator) + labelss := sn.appendTargetLabels(nil, "redis", tagSeparator) var sortedLabelss [][]prompbmarshal.Label for _, labels := range labelss { sortedLabelss = append(sortedLabelss, discoveryutils.GetSortedLabels(labels)) diff --git a/lib/promscrape/discovery/consul/watch.go b/lib/promscrape/discovery/consul/watch.go index bafb3f183..4fcae3def 100644 --- a/lib/promscrape/discovery/consul/watch.go +++ b/lib/promscrape/discovery/consul/watch.go @@ -234,11 +234,11 @@ func (sw *serviceWatcher) watchForServiceNodesUpdates(cw *consulWatcher) { } // getServiceNodesSnapshot returns a snapshot of discovered ServiceNodes. -func (cw *consulWatcher) getServiceNodesSnapshot() []ServiceNode { - var sns []ServiceNode +func (cw *consulWatcher) getServiceNodesSnapshot() map[string][]ServiceNode { cw.servicesLock.Lock() - for _, sw := range cw.services { - sns = append(sns, sw.serviceNodes...) + sns := make(map[string][]ServiceNode, len(cw.services)) + for svc, sw := range cw.services { + sns[svc] = sw.serviceNodes } cw.servicesLock.Unlock() return sns