Skip to content

Commit

Permalink
Update use of logger and fix error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alexperez52 committed Aug 27, 2021
1 parent 6d73fa2 commit 902a212
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 66 deletions.
11 changes: 6 additions & 5 deletions cmd/otel-allocator/allocation/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (
"github.com/prometheus/common/model"
)

var (
log logr.Logger
)

/*
Load balancer will serve on an HTTP server exposing /jobs/<job_id>/targets <- these are configured using least connection
Load balancer will need information about the collectors in order to set the URLs
Expand Down Expand Up @@ -46,6 +42,8 @@ type Allocator struct {
collectors map[string]*collector // all current collectors

TargetItems map[string]*TargetItem

log logr.Logger
}

// findNextCollector finds the next collector with less number of targets.
Expand Down Expand Up @@ -82,6 +80,8 @@ func (allocator *Allocator) SetWaitingTargets(targets []TargetItem) {
// 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) {
log := allocator.log.WithValues("opentelemetry-targetallocator")

allocator.m.Lock()
defer allocator.m.Unlock()
if len(collectors) == 0 {
Expand Down Expand Up @@ -143,8 +143,9 @@ func (allocator *Allocator) processWaitingTargets() {
}
}

func NewAllocator() *Allocator {
func NewAllocator(log logr.Logger) *Allocator {
return &Allocator{
log: log,
targetsWaiting: make(map[string]TargetItem),
collectors: make(map[string]*collector),
TargetItems: make(map[string]*TargetItem),
Expand Down
1 change: 1 addition & 0 deletions cmd/otel-allocator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Load(file string) (Config, error) {
}

func unmarshal(cfg *Config, configFile string) error {

yamlFile, err := ioutil.ReadFile(configFile)
if err != nil {
return err
Expand Down
13 changes: 7 additions & 6 deletions cmd/otel-allocator/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ import (
"github.com/prometheus/prometheus/discovery"
)

var (
logger logr.Logger
)

type Manager struct {
log logr.Logger
manager *discovery.Manager
logger log.Logger
close chan struct{}
}

func NewManager(ctx context.Context, logger log.Logger, options ...func(*discovery.Manager)) *Manager {
func NewManager(log logr.Logger, ctx context.Context, logger log.Logger, options ...func(*discovery.Manager)) *Manager {
manager := discovery.NewManager(ctx, logger, options...)

go func() {
if err := manager.Run(); err != nil {
logger.Log("Discovery manager failed", err)
}
}()
return &Manager{
log: log,
manager: manager,
logger: logger,
close: make(chan struct{}),
Expand All @@ -45,11 +44,13 @@ func (m *Manager) ApplyConfig(cfg config.Config) error {
}

func (m *Manager) Watch(fn func(targets []allocation.TargetItem)) {
log := m.log.WithValues("opentelemetry-targetallocator")

go func() {
for {
select {
case <-m.close:
logger.Info("Service Discovery watch event stopped: discovery manager closed")
log.Info("Service Discovery watch event stopped: discovery manager closed")
return
case tsets := <-m.manager.SyncCh():
targets := []allocation.TargetItem{}
Expand Down
12 changes: 8 additions & 4 deletions cmd/otel-allocator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ module github.com/otel-allocator
go 1.17

require (
github.com/fsnotify/fsnotify v1.4.9
github.com/fsnotify/fsnotify v1.5.1
github.com/go-kit/log v0.1.0
github.com/go-logr/logr v0.4.0
github.com/gorilla/mux v1.8.0
github.com/prometheus/common v0.30.0
github.com/prometheus/prometheus v1.8.2-0.20210621150501-ff58416a0b02
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.19.0 // indirect
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.21.3
k8s.io/apimachinery v0.21.3
k8s.io/client-go v0.21.3
k8s.io/api v0.22.1
k8s.io/apimachinery v0.22.1
k8s.io/client-go v0.22.1
k8s.io/component-base v0.22.1 // indirect
sigs.k8s.io/controller-runtime v0.9.6
)
Loading

0 comments on commit 902a212

Please sign in to comment.