Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GetAllTargetsByCollectorAndJob to use TargetItem hash #1086

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/otel-allocator/allocation/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ func GetAllTargetsByJob(job string, cMap map[string][]TargetItem, allocator Allo

func GetAllTargetsByCollectorAndJob(collector string, job string, cMap map[string][]TargetItem, allocator Allocator) []targetGroupJSON {
var tgs []targetGroupJSON
group := make(map[string]string)
group := make(map[string]TargetItem)
labelSet := make(map[string]model.LabelSet)
if _, ok := allocator.Collectors()[collector]; ok {
for _, targetItemArr := range cMap {
for _, targetItem := range targetItemArr {
if targetItem.CollectorName == collector && targetItem.JobName == job {
group[targetItem.Label.String()] = targetItem.TargetURL
labelSet[targetItem.TargetURL] = targetItem.Label
group[targetItem.Label.String()] = targetItem
labelSet[targetItem.Hash()] = targetItem.Label
}
}
}
}
for _, v := range group {
tgs = append(tgs, targetGroupJSON{Targets: []string{v}, Labels: labelSet[v]})
tgs = append(tgs, targetGroupJSON{Targets: []string{v.TargetURL}, Labels: labelSet[v.Hash()]})
}

return tgs
Expand Down
49 changes: 47 additions & 2 deletions cmd/otel-allocator/allocation/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,57 @@ func TestGetAllTargetsByCollectorAndJob(t *testing.T) {
},
},
},
{
name: "Multiple entry target map with same target address",
args: args{
collector: "test-collector",
job: "test-job",
cMap: map[string][]TargetItem{
"test-collectortest-job": {
TargetItem{
JobName: "test-job",
Label: model.LabelSet{
"test-label": "test-value",
"foo": "bar",
},
TargetURL: "test-url",
CollectorName: "test-collector",
},
TargetItem{
JobName: "test-job",
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url",
CollectorName: "test-collector",
},
},
},
allocator: baseAllocator,
},
want: []targetGroupJSON{
{
Targets: []string{"test-url"},
Labels: map[model.LabelName]model.LabelValue{
"test-label": "test-value",
"foo": "bar",
},
},
{
Targets: []string{"test-url"},
Labels: map[model.LabelName]model.LabelValue{
"test-label": "test-value",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, groupJSON := range GetAllTargetsByCollectorAndJob(tt.args.collector, tt.args.job, tt.args.cMap, tt.args.allocator) {
targetGroups := GetAllTargetsByCollectorAndJob(tt.args.collector, tt.args.job, tt.args.cMap, tt.args.allocator)
for _, wantGroupJson := range tt.want {
exist := false
for _, wantGroupJson := range tt.want {
for _, groupJSON := range targetGroups {
if groupJSON.Labels.String() == wantGroupJson.Labels.String() {
exist = reflect.DeepEqual(groupJSON, wantGroupJson)
}
Expand Down