diff --git a/lib/promscrape/discovery/consul/service_node.go b/lib/promscrape/discovery/consul/service_node.go index 4086bd3b78..8023ecfd7e 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 0b65a1f205..7b5e3dc08e 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 bafb3f183b..4fcae3deff 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