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 1 commit
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,23 +46,23 @@ func GetAllTargetsByJob(job string, cMap map[string][]TargetItem, allocator *All

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)
for _, col := range allocator.Collectors() {
if col.Name == collector {
for _, targetItemArr := range cMap {
for _, targetItem := range targetItemArr {
if targetItem.Collector.Name == 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
55 changes: 53 additions & 2 deletions cmd/otel-allocator/allocation/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,63 @@ 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",
Collector: &collector{
Name: "test-collector",
NumTargets: 2,
},
},
TargetItem{
JobName: "test-job",
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url",
Collector: &collector{
Name: "test-collector",
NumTargets: 2,
},
},
},
},
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