fix file_sd never stop update 'custom_sd.json' file in adapter.go (#4567)

Signed-off-by: wangyaqiang1 <wangyaqiang1@jd.com>
This commit is contained in:
Yaqiang Wang 2018-11-30 17:32:17 +08:00 committed by Simon Pasquier
parent 76b266b2f0
commit 8b85d876f2
2 changed files with 37 additions and 14 deletions

View file

@ -25,6 +25,7 @@ import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/targetgroup"
)
@ -34,6 +35,15 @@ type customSD struct {
Labels map[string]string `json:"labels"`
}
func fingerprint(group *targetgroup.Group) model.Fingerprint {
groupFingerprint := model.LabelSet{}.Fingerprint()
for _, targets := range group.Targets {
groupFingerprint ^= targets.Fingerprint()
}
groupFingerprint ^= group.Labels.Fingerprint()
return groupFingerprint
}
// Adapter runs an unknown service discovery implementation and converts its target groups
// to JSON and writes to a file for file_sd.
type Adapter struct {
@ -57,13 +67,7 @@ func mapToArray(m map[string]*customSD) []customSD {
func generateTargetGroups(allTargetGroups map[string][]*targetgroup.Group) map[string]*customSD {
groups := make(map[string]*customSD)
for k, sdTargetGroups := range allTargetGroups {
for i, group := range sdTargetGroups {
// There is no target, so no need to keep it.
if len(group.Targets) <= 0 {
continue
}
for _, group := range sdTargetGroups {
newTargets := make([]string, 0)
newLabels := make(map[string]string)
@ -76,12 +80,16 @@ func generateTargetGroups(allTargetGroups map[string][]*targetgroup.Group) map[s
for name, value := range group.Labels {
newLabels[string(name)] = string(value)
}
// Make a unique key, including the current index, in case the sd_type (map key) and group.Source is not unique.
key := fmt.Sprintf("%s:%s:%d", k, group.Source, i)
groups[key] = &customSD{
sdGroup := customSD{
Targets: newTargets,
Labels: newLabels,
}
// Make a unique key, including group's fingerprint, in case the sd_type (map key) and group.Source is not unique.
groupFingerprint := fingerprint(group)
key := fmt.Sprintf("%s:%s:%s", k, group.Source, groupFingerprint.String())
groups[key] = &sdGroup
}
}

View file

@ -41,7 +41,16 @@ func TestGenerateTargetGroups(t *testing.T) {
},
},
},
expectedCustomSD: map[string]*customSD{},
expectedCustomSD: map[string]*customSD{
"customSD:Consul:0000000000000000": {
Targets: []string{},
Labels: map[string]string{},
},
"customSD:Kubernetes:0000000000000000": {
Targets: []string{},
Labels: map[string]string{},
},
},
},
{
title: "targetGroup filled",
@ -78,7 +87,7 @@ func TestGenerateTargetGroups(t *testing.T) {
},
},
expectedCustomSD: map[string]*customSD{
"customSD:Azure:0": {
"customSD:Azure:282a007a18fadbbb": {
Targets: []string{
"host1",
"host2",
@ -87,7 +96,7 @@ func TestGenerateTargetGroups(t *testing.T) {
"__meta_test_label": "label_test_1",
},
},
"customSD:Openshift:1": {
"customSD:Openshift:281c007a18ea2ad0": {
Targets: []string{
"host3",
"host4",
@ -125,7 +134,7 @@ func TestGenerateTargetGroups(t *testing.T) {
},
},
expectedCustomSD: map[string]*customSD{
"customSD:GCE:0": {
"customSD:GCE:282a007a18fadbbb": {
Targets: []string{
"host1",
"host2",
@ -134,6 +143,12 @@ func TestGenerateTargetGroups(t *testing.T) {
"__meta_test_label": "label_test_1",
},
},
"customSD:Kubernetes:282e007a18fad483": {
Targets: []string{},
Labels: map[string]string{
"__meta_test_label": "label_test_2",
},
},
},
},
}