Skip to content

Commit

Permalink
Added fix for re-assigning targets
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronoff97 committed Aug 17, 2022
1 parent 594462d commit 5f8f3de
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/otel-allocator/allocation/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ func (allocator *Allocator) SetWaitingTargets(targets []TargetItem) {
//allocator.log.Info(fmt.Sprintf("len(targetsWaiting): %d\nlen(targets): %d", len(allocator.targetsWaiting), len(targets)))
}

// allCollectorsPresent checks if all of the collectors provided are in the allocator's map
func (allocator *Allocator) allCollectorsPresent(collectors []string) bool {
if len(collectors) != len(allocator.collectors) {
return false
}
for _, s := range collectors {
if _, ok := allocator.collectors[s]; !ok {
return false
}
}
return true
}

// SetCollectors sets the set of collectors with key=collectorName, value=Collector object.
// SetCollectors is called when Collectors are added or removed
func (allocator *Allocator) SetCollectors(collectors []string) {
Expand All @@ -114,6 +127,9 @@ func (allocator *Allocator) SetCollectors(collectors []string) {
if len(collectors) == 0 {
log.Info("No collector instances present")
return
} else if allocator.allCollectorsPresent(collectors) {
log.Info("No changes to the collectors found")
return
}
for k := range allocator.collectors {
delete(allocator.collectors, k)
Expand Down
36 changes: 36 additions & 0 deletions cmd/otel-allocator/allocation/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,42 @@ func TestAddingAndRemovingTargets(t *testing.T) {
}
}

func TestNoCollectorReassignment(t *testing.T) {
s := NewAllocator(logger)

cols := []string{"col-1", "col-2", "col-3"}
s.SetCollectors(cols)
labels := model.LabelSet{}

excpectedColLen := len(cols)
assert.Len(t, s.collectors, excpectedColLen)

for _, i := range cols {
assert.NotNil(t, s.collectors[i])
}
initTargets := []string{"prometheus:1000", "prometheus:1001", "prometheus:1002", "prometheus:1003", "prometheus:1004", "prometheus:1005"}
var targetList []TargetItem
for _, i := range initTargets {
targetList = append(targetList, TargetItem{JobName: "sample-name", TargetURL: i, Label: labels})
}
// test that targets and collectors are added properly
s.SetWaitingTargets(targetList)
s.AllocateTargets()

// verify
expectedTargetLen := len(initTargets)
targetItems := s.TargetItems()
assert.Len(t, targetItems, expectedTargetLen)

// assign new set of collectors with the same names
newCols := []string{"col-1", "col-2", "col-3"}
s.SetCollectors(newCols)

newTargetItems := s.TargetItems()
assert.Equal(t, targetItems, newTargetItems)

}

// Tests that two targets with the same target url and job name but different label set are both added
func TestAllocationCollision(t *testing.T) {
// prepare allocator with initial targets and collectors
Expand Down

0 comments on commit 5f8f3de

Please sign in to comment.