Skip to content

Commit

Permalink
Update GetAllTargetsByCollectorAndJob to use TargetItem hash (#1086)
Browse files Browse the repository at this point in the history
* Update GetAllTargetsByCollectorAndJob to use TargetItem hash to prevent overwriting of labels for targets with the same address

* Fix unit test after merging main

Co-authored-by: Pavol Loffay <p.loffay@gmail.com>
  • Loading branch information
kelseyma and pavolloffay authored Sep 12, 2022
1 parent 0f2178e commit e48373c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
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

0 comments on commit e48373c

Please sign in to comment.