Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] remove init function from prehook package #3625

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions cmd/otel-allocator/prehook/prehook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@
package prehook

import (
"errors"

"github.com/go-logr/logr"
"github.com/prometheus/prometheus/model/relabel"

"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
)

const (
relabelConfigTargetFilterName = "relabel-config"
)

type Hook interface {
Apply(map[string]*target.Item) map[string]*target.Item
SetConfig(map[string][]*relabel.Config)
Expand All @@ -36,7 +30,9 @@ type Hook interface {
type HookProvider func(log logr.Logger) Hook

var (
registry = map[string]HookProvider{}
registry = map[string]HookProvider{
relabelConfigTargetFilterName: newRelabelConfigTargetFilter,
}
)

func New(name string, log logr.Logger) Hook {
Expand All @@ -47,18 +43,3 @@ func New(name string, log logr.Logger) Hook {
log.Info("Unrecognized filter strategy; filtering disabled")
return nil
}

func Register(name string, provider HookProvider) error {
if _, ok := registry[name]; ok {
return errors.New("already registered")
}
registry[name] = provider
return nil
}

func init() {
err := Register(relabelConfigTargetFilterName, NewRelabelConfigTargetFilter)
if err != nil {
panic(err)
}
}
18 changes: 11 additions & 7 deletions cmd/otel-allocator/prehook/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ import (
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
)

type RelabelConfigTargetFilter struct {
const (
relabelConfigTargetFilterName = "relabel-config"
)

type relabelConfigTargetFilter struct {
log logr.Logger
relabelCfg map[string][]*relabel.Config
}

func NewRelabelConfigTargetFilter(log logr.Logger) Hook {
return &RelabelConfigTargetFilter{
func newRelabelConfigTargetFilter(log logr.Logger) Hook {
return &relabelConfigTargetFilter{
log: log,
relabelCfg: make(map[string][]*relabel.Config),
}
}

func (tf *RelabelConfigTargetFilter) Apply(targets map[string]*target.Item) map[string]*target.Item {
func (tf *relabelConfigTargetFilter) Apply(targets map[string]*target.Item) map[string]*target.Item {
numTargets := len(targets)

// need to wait until relabelCfg is set
Expand All @@ -58,7 +62,7 @@ func (tf *RelabelConfigTargetFilter) Apply(targets map[string]*target.Item) map[
return targets
}

func (tf *RelabelConfigTargetFilter) SetConfig(cfgs map[string][]*relabel.Config) {
func (tf *relabelConfigTargetFilter) SetConfig(cfgs map[string][]*relabel.Config) {
relabelCfgCopy := make(map[string][]*relabel.Config)
for key, val := range cfgs {
relabelCfgCopy[key] = tf.replaceRelabelConfig(val)
Expand All @@ -71,7 +75,7 @@ func (tf *RelabelConfigTargetFilter) SetConfig(cfgs map[string][]*relabel.Config
// for why SHARD == 0 is a necessary substitution. Otherwise the keep action that uses this env variable,
// would not match the regex and all targets end up dropped. Also note, $(SHARD) will always be 0 and it
// does not make sense to read from the environment because it is never set in the allocator.
func (tf *RelabelConfigTargetFilter) replaceRelabelConfig(cfg []*relabel.Config) []*relabel.Config {
func (tf *relabelConfigTargetFilter) replaceRelabelConfig(cfg []*relabel.Config) []*relabel.Config {
for i := range cfg {
str := cfg[i].Regex.String()
if str == "$(SHARD)" {
Expand All @@ -82,7 +86,7 @@ func (tf *RelabelConfigTargetFilter) replaceRelabelConfig(cfg []*relabel.Config)
return cfg
}

func (tf *RelabelConfigTargetFilter) GetConfig() map[string][]*relabel.Config {
func (tf *relabelConfigTargetFilter) GetConfig() map[string][]*relabel.Config {
relabelCfgCopy := make(map[string][]*relabel.Config)
for k, v := range tf.relabelCfg {
relabelCfgCopy[k] = v
Expand Down
Loading