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
This commit is contained in:
Nikolay 2021-03-23 16:35:01 +03:00 committed by Aliaksandr Valialkin
parent d1e773266f
commit 19a40faf8e
3 changed files with 11 additions and 9 deletions

View File

@ -13,8 +13,10 @@ import (
func getServiceNodesLabels(cfg *apiConfig) []map[string]string { func getServiceNodesLabels(cfg *apiConfig) []map[string]string {
sns := cfg.consulWatcher.getServiceNodesSnapshot() sns := cfg.consulWatcher.getServiceNodesSnapshot()
var ms []map[string]string var ms []map[string]string
for _, sn := range sns { for svc, sn := range sns {
ms = sn.appendTargetLabels(ms, cfg.tagSeparator) for i := range sn {
ms = sn[i].appendTargetLabels(ms, svc, cfg.tagSeparator)
}
} }
return ms return ms
} }
@ -67,7 +69,7 @@ func parseServiceNodes(data []byte) ([]ServiceNode, error) {
return sns, nil 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 var addr string
if sn.Service.Address != "" { if sn.Service.Address != "" {
addr = discoveryutils.JoinHostPort(sn.Service.Address, sn.Service.Port) 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_dc": sn.Node.Datacenter,
"__meta_consul_health": aggregatedStatus(sn.Checks), "__meta_consul_health": aggregatedStatus(sn.Checks),
"__meta_consul_node": sn.Node.Node, "__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_address": sn.Service.Address,
"__meta_consul_service_id": sn.Service.ID, "__meta_consul_service_id": sn.Service.ID,
"__meta_consul_service_port": strconv.Itoa(sn.Service.Port), "__meta_consul_service_port": strconv.Itoa(sn.Service.Port),

View File

@ -106,7 +106,7 @@ func TestParseServiceNodesSuccess(t *testing.T) {
// Check sn.appendTargetLabels() // Check sn.appendTargetLabels()
tagSeparator := "," tagSeparator := ","
labelss := sn.appendTargetLabels(nil, tagSeparator) labelss := sn.appendTargetLabels(nil, "redis", tagSeparator)
var sortedLabelss [][]prompbmarshal.Label var sortedLabelss [][]prompbmarshal.Label
for _, labels := range labelss { for _, labels := range labelss {
sortedLabelss = append(sortedLabelss, discoveryutils.GetSortedLabels(labels)) sortedLabelss = append(sortedLabelss, discoveryutils.GetSortedLabels(labels))

View File

@ -234,11 +234,11 @@ func (sw *serviceWatcher) watchForServiceNodesUpdates(cw *consulWatcher) {
} }
// getServiceNodesSnapshot returns a snapshot of discovered ServiceNodes. // getServiceNodesSnapshot returns a snapshot of discovered ServiceNodes.
func (cw *consulWatcher) getServiceNodesSnapshot() []ServiceNode { func (cw *consulWatcher) getServiceNodesSnapshot() map[string][]ServiceNode {
var sns []ServiceNode
cw.servicesLock.Lock() cw.servicesLock.Lock()
for _, sw := range cw.services { sns := make(map[string][]ServiceNode, len(cw.services))
sns = append(sns, sw.serviceNodes...) for svc, sw := range cw.services {
sns[svc] = sw.serviceNodes
} }
cw.servicesLock.Unlock() cw.servicesLock.Unlock()
return sns return sns