Skip to content

Commit

Permalink
Fix the issue that target-level metadata labels were missing (#948) (#…
Browse files Browse the repository at this point in the history
…949)

* Fix the issue that target-level metadata labels were missing (#948)

* fix(allocation): GetAllTargetsByCollectorAndJob with label merge
  • Loading branch information
CoderPoet authored Jul 26, 2022
1 parent 7ce297d commit ab00e8c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 33 deletions.
23 changes: 8 additions & 15 deletions cmd/otel-allocator/allocation/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,13 @@ func GetAllTargetsByJob(job string, cMap map[string][]TargetItem, allocator *All

var targetGroupList []targetGroupJSON

trg := make(map[string][]TargetItem)
for _, t := range targetList {
trg[t.JobName+t.Label.String()] = append(trg[t.JobName+t.Label.String()], t)
targetGroupList = append(targetGroupList, targetGroupJSON{
Targets: []string{t.TargetURL},
Labels: t.Label,
})
}
labelSetMap := make(map[string]model.LabelSet)
for _, tArr := range trg {
var targets []string
for _, t := range tArr {
labelSetMap[t.TargetURL] = t.Label
targets = append(targets, t.TargetURL)
}
targetGroupList = append(targetGroupList, targetGroupJSON{Targets: targets, Labels: labelSetMap[targets[0]]})

}
displayData[j.Collector.Name] = collectorJSON{Link: fmt.Sprintf("/jobs/%s/targets?collector_id=%s", url.QueryEscape(j.JobName), j.Collector.Name), Jobs: targetGroupList}

}
Expand All @@ -53,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)
if targetItem.Collector.Name == collector && targetItem.JobName == job {
group[targetItem.Label.String()] = targetItem.TargetURL
labelSet[targetItem.TargetURL] = targetItem.Label
}
}
Expand All @@ -69,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
72 changes: 66 additions & 6 deletions cmd/otel-allocator/allocation/http_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package allocation

import (
"github.com/prometheus/common/model"
"reflect"
"testing"

"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
)

func TestGetAllTargetsByCollectorAndJob(t *testing.T) {
Expand Down Expand Up @@ -41,7 +43,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 @@ -72,7 +74,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 @@ -85,7 +87,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 @@ -107,11 +109,69 @@ 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: 2,
},
},
},
"test-collectortest-job2": {
TargetItem{
JobName: "test-job",
Label: model.LabelSet{
"test-label": "test-value",
},
TargetURL: "test-url2",
Collector: &collector{
Name: "test-collector",
NumTargets: 2,
},
},
},
},
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) {
if got := GetAllTargetsByCollectorAndJob(tt.args.collector, tt.args.job, tt.args.cMap, tt.args.allocator); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllTargetsByCollectorAndJob() = %v, want %v", got, tt.want)
for _, groupJSON := range GetAllTargetsByCollectorAndJob(tt.args.collector, tt.args.job, tt.args.cMap, tt.args.allocator) {
exist := false
for _, wantGroupJson := range tt.want {
if groupJSON.Labels.String() == wantGroupJson.Labels.String() {
exist = reflect.DeepEqual(groupJSON, wantGroupJson)
}
}
assert.Equal(t, true, exist)
}
})
}
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
2 changes: 1 addition & 1 deletion cmd/otel-allocator/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (m *Manager) Watch(fn func(targets []allocation.TargetItem)) {
targets = append(targets, allocation.TargetItem{
JobName: jobName,
TargetURL: string(t[model.AddressLabel]),
Label: tg.Labels,
Label: t.Merge(tg.Labels),
})
}
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/otel-allocator/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (
gokitlog "github.com/go-kit/log"
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/allocation"
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/config"
allocatorWatcher "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/watcher"
"github.com/prometheus/common/model"
promconfig "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
"github.com/stretchr/testify/assert"
ctrl "sigs.k8s.io/controller-runtime"
)

var cfg config.Config
Expand All @@ -26,7 +29,7 @@ func TestMain(m *testing.M) {
fmt.Printf("failed to load config file: %v", err)
os.Exit(1)
}
manager = NewManager(context.Background(), gokitlog.NewNopLogger())
manager = NewManager(ctrl.Log.WithName("test"), context.Background(), gokitlog.NewNopLogger())

results = make(chan []string)
manager.Watch(func(targets []allocation.TargetItem) {
Expand All @@ -45,7 +48,7 @@ func TestMain(m *testing.M) {
}

func TestTargetDiscovery(t *testing.T) {
err := manager.ApplyConfig(cfg)
err := manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, &promconfig.Config{})
assert.NoError(t, err)

gotTargets := <-results
Expand All @@ -70,7 +73,7 @@ func TestTargetUpdate(t *testing.T) {
},
}

err := manager.ApplyConfig(cfg)
err := manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, &promconfig.Config{})
assert.NoError(t, err)

gotTargets := <-results
Expand Down
8 changes: 4 additions & 4 deletions cmd/otel-allocator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a // indirect
github.com/armon/go-metrics v0.3.9 // indirect
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
github.com/aws/aws-sdk-go v1.42.16 // indirect
github.com/aws/aws-sdk-go v1.44.41 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand Down Expand Up @@ -139,11 +139,11 @@ require (
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.1 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
Expand Down
12 changes: 8 additions & 4 deletions cmd/otel-allocator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,9 @@ github.com/aws/aws-sdk-go v1.38.35/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2z
github.com/aws/aws-sdk-go v1.40.11/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go v1.40.45/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go v1.42.8/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go v1.42.16 h1:jOUmYYpC77NZYQVHTOTFT4lwFBT1u3s8ETKciU4l6gQ=
github.com/aws/aws-sdk-go v1.42.16/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go v1.44.41 h1:FNW3Tb8vKvXLZ7lzGlg/dCAXhK4RC5fyFewD11oJhUM=
github.com/aws/aws-sdk-go v1.44.41/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/aws/aws-sdk-go-v2 v1.9.1/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4=
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.8.1/go.mod h1:CM+19rL1+4dFWnOQKwDc7H1KwXTz+h61oUSHyhV0b3o=
Expand Down Expand Up @@ -2165,8 +2166,9 @@ golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211020060615-d418f374d309/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 h1:0qxwC5n+ttVOINCBeRHO0nq9X7uy8SDsPoi5OaCdIEI=
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -2347,13 +2349,15 @@ golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5IqFdQieMLwwCJokV2pc=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit ab00e8c

Please sign in to comment.