From 8b85d876f23fe41558da25fdf27f32b77676c189 Mon Sep 17 00:00:00 2001 From: Yaqiang Wang Date: Fri, 30 Nov 2018 17:32:17 +0800 Subject: [PATCH] fix file_sd never stop update 'custom_sd.json' file in adapter.go (#4567) Signed-off-by: wangyaqiang1 --- .../examples/custom-sd/adapter/adapter.go | 28 ++++++++++++------- .../custom-sd/adapter/adapter_test.go | 23 ++++++++++++--- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/documentation/examples/custom-sd/adapter/adapter.go b/documentation/examples/custom-sd/adapter/adapter.go index 3d9e7afe133..4fd0ea9db4f 100644 --- a/documentation/examples/custom-sd/adapter/adapter.go +++ b/documentation/examples/custom-sd/adapter/adapter.go @@ -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 } } diff --git a/documentation/examples/custom-sd/adapter/adapter_test.go b/documentation/examples/custom-sd/adapter/adapter_test.go index 812da09d06e..7a97ef1cf6a 100644 --- a/documentation/examples/custom-sd/adapter/adapter_test.go +++ b/documentation/examples/custom-sd/adapter/adapter_test.go @@ -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", + }, + }, }, }, }