Skip to content

Commit

Permalink
fix(allocation): GetAllTargetsByCollectorAndJob with label merge
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderPoet committed Jul 3, 2022
1 parent a3db84e commit 907025e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cmd/otel-allocator/allocation/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ 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]string)
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()] = append(group[targetItem.Label.String()], targetItem.TargetURL)
group[targetItem.Label.String()] = targetItem.TargetURL
labelSet[targetItem.TargetURL] = targetItem.Label
}
}
Expand All @@ -62,7 +62,7 @@ func GetAllTargetsByCollectorAndJob(collector string, job string, cMap map[strin
}

for _, v := range group {
tgs = append(tgs, targetGroupJSON{Targets: v, Labels: labelSet[v[0]]})
tgs = append(tgs, targetGroupJSON{Targets: []string{v}, Labels: labelSet[v]})
}

return tgs
Expand Down
58 changes: 55 additions & 3 deletions cmd/otel-allocator/allocation/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGetAllTargetsByCollectorAndJob(t *testing.T) {
"test-collectortest-job": {
TargetItem{
JobName: "test-job",
Label: model.LabelSet{
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url",
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestGetAllTargetsByCollectorAndJob(t *testing.T) {
"test-collectortest-job": {
TargetItem{
JobName: "test-job",
Label: model.LabelSet{
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url",
Expand All @@ -86,7 +86,7 @@ func TestGetAllTargetsByCollectorAndJob(t *testing.T) {
"test-collectortest-job2": {
TargetItem{
JobName: "test-job2",
Label: model.LabelSet{
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url",
Expand All @@ -108,6 +108,58 @@ func TestGetAllTargetsByCollectorAndJob(t *testing.T) {
},
},
},
{
name: "Multiple entry target map of same job with label merge",
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-url1",
Collector: &collector{
Name: "test-collector",
NumTargets: 1,
},
},
},
"test-collectortest-job2": {
TargetItem{
JobName: "test-job",
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url2",
Collector: &collector{
Name: "test-collector",
NumTargets: 1,
},
},
},
},
allocator: baseAllocator,
},
want: []targetGroupJSON{
{
Targets: []string{"test-url1"},
Labels: map[model.LabelName]model.LabelValue{
"test-label": "test-value",
"foo": "bar",
},
},
{
Targets: []string{"test-url2"},
Labels: map[model.LabelName]model.LabelValue{
"test-label": "test-value",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions cmd/otel-allocator/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (k *Client) Watch(ctx context.Context, labelMap map[string]string, fn func(
log.Error(err, "unable to create collector pod watcher")
return
}
log.Info("Successfully started a collector pod watcher")
if msg := runWatch(ctx, k, watcher.ResultChan(), collectorMap, fn); msg != "" {
log.Info("Collector pod watch event stopped " + msg)
return
Expand Down

0 comments on commit 907025e

Please sign in to comment.