-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding prehook allocator filter to reduce assigned targets
- Loading branch information
1 parent
10404eb
commit cbc48bf
Showing
15 changed files
with
527 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
cmd/otel-allocator/prehooktargetfilter/allocator_prehook.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package prehooktargetfilter | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/allocation" | ||
) | ||
|
||
const ( | ||
noOpTargetFilterName = "no-op" | ||
relabelConfigTargetFilterName = "relabel-config" | ||
) | ||
|
||
type AllocatorPrehook interface { | ||
SetTargets(targets map[string]*allocation.TargetItem) | ||
TargetItems() map[string]*allocation.TargetItem | ||
} | ||
|
||
type AllocatorPrehookProvider func(log logr.Logger, allocator allocation.Allocator) AllocatorPrehook | ||
|
||
var ( | ||
registry = map[string]AllocatorPrehookProvider{} | ||
) | ||
|
||
func New(name string, log logr.Logger, allocator allocation.Allocator) (AllocatorPrehook, error) { | ||
if p, ok := registry[name]; ok { | ||
return p(log, allocator), nil | ||
} | ||
return nil, fmt.Errorf("unregistered filtering strategy: %s", name) | ||
} | ||
|
||
func Register(name string, provider AllocatorPrehookProvider) error { | ||
if _, ok := registry[name]; ok { | ||
return errors.New("already registered") | ||
} | ||
registry[name] = provider | ||
return nil | ||
} | ||
|
||
func init() { | ||
err := Register(noOpTargetFilterName, NewNoOpTargetFilter) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = Register(relabelConfigTargetFilterName, NewRelabelConfigTargetFilter) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package prehooktargetfilter | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/allocation" | ||
) | ||
|
||
type NoOpTargetFilter struct { | ||
log logr.Logger | ||
allocator allocation.Allocator | ||
} | ||
|
||
func NewNoOpTargetFilter(log logr.Logger, allocator allocation.Allocator) AllocatorPrehook { | ||
return &NoOpTargetFilter{ | ||
log: log, | ||
allocator: allocator, | ||
} | ||
} | ||
|
||
func (tf *NoOpTargetFilter) SetTargets(targets map[string]*allocation.TargetItem) { | ||
tf.allocator.SetTargets(targets) | ||
} | ||
|
||
func (tf *NoOpTargetFilter) TargetItems() map[string]*allocation.TargetItem { | ||
return tf.allocator.TargetItems() | ||
} |
57 changes: 57 additions & 0 deletions
57
cmd/otel-allocator/prehooktargetfilter/no_op_filter_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package prehooktargetfilter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/allocation" | ||
) | ||
|
||
var _ allocation.Allocator = &mockAllocator{} | ||
|
||
type mockAllocator struct { | ||
targetItems map[string]*allocation.TargetItem | ||
} | ||
|
||
func (allocator mockAllocator) SetTargets(targets map[string]*allocation.TargetItem) { | ||
for k, v := range targets { | ||
allocator.targetItems[k] = v | ||
} | ||
} | ||
|
||
func (allocator mockAllocator) TargetItems() map[string]*allocation.TargetItem { | ||
return allocator.targetItems | ||
} | ||
|
||
func (allocator mockAllocator) SetCollectors(collectors map[string]*allocation.Collector) { | ||
} | ||
func (allocator mockAllocator) Collectors() map[string]*allocation.Collector { | ||
return nil | ||
} | ||
|
||
func TestNoOpSetTargets(t *testing.T) { | ||
allocator := mockAllocator{targetItems: make(map[string]*allocation.TargetItem)} | ||
|
||
allocatorPrehook, err := New("no-op", logger, allocator) | ||
assert.Nil(t, err) | ||
|
||
targets, _ := makeNNewTargets(numTargets, 3, 0) | ||
allocatorPrehook.SetTargets(targets) | ||
remainingTargetItems := allocatorPrehook.TargetItems() | ||
assert.Len(t, remainingTargetItems, numTargets) | ||
} |
Oops, something went wrong.