Skip to content

Commit

Permalink
small contingency improvement in global error handler
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Dec 15, 2022
1 parent 7a60bc7 commit f28ea92
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add `Producer` interface and `Reader.RegisterProducer(Producer)` to `go.opentelemetry.io/otel/sdk/metric` to enable external metric Producers. (#3524)

### Changed

- Global error handler uses an atomic value instead of a mutex. (#3543)

## [1.11.2/0.34.0] 2022-12-05

### Added
Expand Down
24 changes: 11 additions & 13 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package otel // import "go.opentelemetry.io/otel"
import (
"log"
"os"
"sync"
"sync/atomic"
)

var (
Expand All @@ -34,28 +34,26 @@ var (
)

type delegator struct {
lock *sync.RWMutex
eh ErrorHandler
delegate atomic.Value
}

func (d *delegator) Handle(err error) {
d.lock.RLock()
defer d.lock.RUnlock()
d.eh.Handle(err)
d.getDelegate().Handle(err)
}

func (d *delegator) getDelegate() ErrorHandler {
return d.delegate.Load().(ErrorHandler)
}

// setDelegate sets the ErrorHandler delegate.
func (d *delegator) setDelegate(eh ErrorHandler) {
d.lock.Lock()
defer d.lock.Unlock()
d.eh = eh
d.delegate.Store(eh)
}

func defaultErrorHandler() *delegator {
return &delegator{
lock: &sync.RWMutex{},
eh: &errLogger{l: log.New(os.Stderr, "", log.LstdFlags)},
}
d := &delegator{}
d.delegate.Store(&errLogger{l: log.New(os.Stderr, "", log.LstdFlags)})
return d
}

// errLogger logs errors if no delegate is set, otherwise they are delegated.
Expand Down
6 changes: 3 additions & 3 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type HandlerTestSuite struct {

func (s *HandlerTestSuite) SetupSuite() {
s.errCatcher = new(testErrCatcher)
s.origHandler = globalErrorHandler.eh
s.origHandler = globalErrorHandler.getDelegate()

globalErrorHandler.setDelegate(&errLogger{l: log.New(s.errCatcher, "", 0)})
}
Expand Down Expand Up @@ -111,12 +111,12 @@ func (s *HandlerTestSuite) TestAllowMultipleSets() {
secondary := &errLogger{l: log.New(notUsed, "", 0)}
SetErrorHandler(secondary)
s.Require().Same(GetErrorHandler(), globalErrorHandler, "set changed globalErrorHandler")
s.Require().Same(globalErrorHandler.eh, secondary, "new Handler not set")
s.Require().Same(globalErrorHandler.getDelegate(), secondary, "new Handler not set")

tertiary := &errLogger{l: log.New(notUsed, "", 0)}
SetErrorHandler(tertiary)
s.Require().Same(GetErrorHandler(), globalErrorHandler, "set changed globalErrorHandler")
s.Assert().Same(globalErrorHandler.eh, tertiary, "user Handler not overridden")
s.Assert().Same(globalErrorHandler.getDelegate(), tertiary, "user Handler not overridden")
}

func TestHandlerTestSuite(t *testing.T) {
Expand Down

0 comments on commit f28ea92

Please sign in to comment.