Skip to content

Commit

Permalink
config: move onChange callback list methods into a thread-safe wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
  • Loading branch information
frzifus committed Nov 30, 2022
1 parent 6f9d7df commit 3031879
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 10 deletions.
64 changes: 64 additions & 0 deletions internal/config/change_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 config contains the operator's runtime configuration.
package config

import (
"sync"

"github.com/go-logr/logr"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// ChangeHandler is implemented by any structure that is able to register callbacks
// and call them using one single method.
type ChangeHandler interface {
// Change will call every registered callback.
Change() error
// Register the given function as a callback that will be executed on Change
// is called.
Register(f func() error)
}

// NewOnChange returns thread-safe ChangeHandler.
func NewOnChange() ChangeHandler {
return &onChange{
logger: logf.Log.WithName("change-handler"),
}
}

type onChange struct {
logger logr.Logger

callbacks []func() error
muCallbacks sync.Mutex
}

func (o *onChange) Change() error {
o.muCallbacks.Lock()
defer o.muCallbacks.Unlock()
for _, fn := range o.callbacks {
if err := fn(); err != nil {
o.logger.Error(err, "change callback failed")
}
}
return nil
}

func (o *onChange) Register(f func() error) {
o.muCallbacks.Lock()
defer o.muCallbacks.Unlock()
o.callbacks = append(o.callbacks, f)
}
42 changes: 42 additions & 0 deletions internal/config/change_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 config contains the operator's runtime configuration.
package config_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/open-telemetry/opentelemetry-operator/internal/config"
)

func TestChangeHandler(t *testing.T) {
// prepare
internal := 0
callback := func() error {
internal += 1
return nil
}
h := config.NewOnChange()

h.Register(callback)

for i := 0; i < 5; i++ {
require.NoError(t, h.Change())
assert.Equal(t, i, internal)
}
}
17 changes: 10 additions & 7 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Config struct {
targetAllocatorConfigMapEntry string
autoInstrumentationNodeJSImage string
autoInstrumentationJavaImage string
onChange []func() error
onChange ChangeHandler
labelsFilter []string
platform platform.Platform
autoDetectFrequency time.Duration
Expand Down Expand Up @@ -125,12 +125,10 @@ func (c *Config) AutoDetect() error {
}

if changed {
for _, callback := range c.onChange {
if err := callback(); err != nil {
// we don't fail if the callback failed, as the auto-detection itself
// did work
c.logger.Error(err, "configuration change notification failed for callback")
}
if err := c.onChange.Change(); err != nil {
// we don't fail if the callback failed, as the auto-detection itself
// did work
c.logger.Error(err, "configuration change notification failed for callback")
}
}

Expand Down Expand Up @@ -198,3 +196,8 @@ func (c *Config) AutoInstrumentationDotNetImage() string {
func (c *Config) LabelsFilter() []string {
return c.labelsFilter
}

// ChangeHandler the internal used ChangeHandler.
func (c *Config) ChangeHandler() ChangeHandler {
return c.onChange
}
11 changes: 8 additions & 3 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type options struct {
collectorConfigMapEntry string
targetAllocatorConfigMapEntry string
targetAllocatorImage string
onChange []func() error
onChange ChangeHandler
labelsFilter []string
platform platform.Platform
autoDetectFrequency time.Duration
Expand Down Expand Up @@ -84,12 +84,17 @@ func WithLogger(logger logr.Logger) Option {
o.logger = logger
}
}
func WithOnChangeHandler(h ChangeHandler) Option {
return func(o *options) {
o.onChange = h
}
}
func WithOnChange(f func() error) Option {
return func(o *options) {
if o.onChange == nil {
o.onChange = []func() error{}
o.onChange = NewOnChange()
}
o.onChange = append(o.onChange, f)
o.onChange.Register(f)
}
}
func WithPlatform(plt platform.Platform) Option {
Expand Down

0 comments on commit 3031879

Please sign in to comment.