diff --git a/documentation/examples/custom-sd/adapter/adapter.go b/documentation/examples/custom-sd/adapter/adapter.go index 4fd0ea9db..c07a79f45 100644 --- a/documentation/examples/custom-sd/adapter/adapter.go +++ b/documentation/examples/custom-sd/adapter/adapter.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" "reflect" + "sort" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" @@ -76,7 +77,7 @@ func generateTargetGroups(allTargetGroups map[string][]*targetgroup.Group) map[s newTargets = append(newTargets, string(target)) } } - + sort.Strings(newTargets) for name, value := range group.Labels { newLabels[string(name)] = string(value) } diff --git a/documentation/examples/custom-sd/adapter/adapter_test.go b/documentation/examples/custom-sd/adapter/adapter_test.go index 7a97ef1cf..2cdc98c61 100644 --- a/documentation/examples/custom-sd/adapter/adapter_test.go +++ b/documentation/examples/custom-sd/adapter/adapter_test.go @@ -151,6 +151,63 @@ func TestGenerateTargetGroups(t *testing.T) { }, }, }, + { + title: "Disordered Ips in Alibaba's application management system", + targetGroup: map[string][]*targetgroup.Group{ + "cart": { + { + Source: "alibaba", + Targets: []model.LabelSet{ + { + model.AddressLabel: "192.168.1.55", + }, + { + model.AddressLabel: "192.168.1.44", + }, + }, + Labels: model.LabelSet{ + model.LabelName("__meta_test_label"): model.LabelValue("label_test_1"), + }, + }, + }, + "buy": { + { + Source: "alibaba", + Targets: []model.LabelSet{ + { + model.AddressLabel: "192.168.1.22", + }, + { + model.AddressLabel: "192.168.1.33", + }, + }, + Labels: model.LabelSet{ + model.LabelName("__meta_test_label"): model.LabelValue("label_test_1"), + }, + }, + }, + }, + expectedCustomSD: map[string]*customSD{ + "buy:alibaba:21c0d97a1e27e6fe": { + Targets: []string{ + "192.168.1.22", + "192.168.1.33", + }, + Labels: map[string]string{ + "__meta_test_label": "label_test_1", + }, + }, + "cart:alibaba:1112e97a13b159fa": { + Targets: []string{ + "192.168.1.44", + "192.168.1.55", + }, + Labels: map[string]string{ + "__meta_test_label": "label_test_1", + }, + }, + }, + }, } for _, testCase := range testCases {