mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-24 20:14:40 +02:00
Reverted the changes to the error handler.
This commit is contained in:
parent
0c553a4ce3
commit
a14b53f9b8
@ -24,7 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||
- The following struct types moved and are replaced with type aliases, since they are exposed to the user: `Observation`, `Measurement`.
|
||||
- The No-op implementations of sync and async instruments are no longer exported, new functions `sdkapi.NewNoopAsyncInstrument()` and `sdkapi.NewNoopSyncInstrument()` are provided instead. (#2271)
|
||||
- Update the SDK `BatchSpanProcessor` to export all queued spans when `ForceFlush` is called. (#2080, #2335)
|
||||
- The default ErrorHandler now uses the internal logger. The default of logging to stderr is unchanged.
|
||||
- The default ErrorHandler now uses the internal logger. The default of logging to stderr is unchanged. (#2343)
|
||||
- Change `resource.Default` to be evaluated the first time it is called, rather than on import. This allows the caller the option to update `OTEL_RESOURCE_ATTRIBUTES` first, such as with `os.Setenv`. (#2371)
|
||||
|
||||
### Added
|
||||
@ -32,7 +32,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||
- Add the `"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc".WithGRPCConn` option so the exporter can reuse an existing gRPC connection. (#2002)
|
||||
- Added a new `schema` module to help parse Schema Files in OTEP 0152 format. (#2267)
|
||||
- Added a new `MapCarrier` to the `go.opentelemetry.io/otel/propagation` package to hold propagated cross-cutting concerns as a `map[string]string` held in memory. (#2334)
|
||||
- Added an internal Logger. This can be used by the SDK and API to provide users with feedback of the internal state. To enable verbose logs configure the logger which will print V(1) logs. For debugging information confgure to print V(5) logs.
|
||||
- Added an internal Logger.
|
||||
This can be used by the SDK and API to provide users with feedback of the internal state.
|
||||
To enable verbose logs configure the logger which will print V(1) logs. For debugging information configure to print V(5) logs. (#2343)
|
||||
|
||||
### Removed
|
||||
|
||||
|
@ -5,9 +5,7 @@ github.com/benbjohnson/clock v1.2.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I=
|
||||
|
@ -1,8 +1,6 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
|
@ -21,9 +21,7 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
|
||||
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
|
@ -24,9 +24,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
|
||||
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
|
@ -24,9 +24,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
|
||||
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
|
@ -23,9 +23,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
|
||||
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
|
@ -21,9 +21,7 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
|
||||
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
|
@ -2,9 +2,7 @@ github.com/benbjohnson/clock v1.2.0 h1:9Re3G2TWxkE06LdMWMpcY6KV81GLXMGiYpPYUPkFA
|
||||
github.com/benbjohnson/clock v1.2.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
|
84
handler.go
84
handler.go
@ -15,7 +15,10 @@
|
||||
package otel // import "go.opentelemetry.io/otel"
|
||||
|
||||
import (
|
||||
"go.opentelemetry.io/otel/internal/debug"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -23,58 +26,79 @@ var (
|
||||
// throughout an OpenTelemetry instrumented project. When a user
|
||||
// specified ErrorHandler is registered (`SetErrorHandler`) all calls to
|
||||
// `Handle` and will be delegated to the registered ErrorHandler.
|
||||
globalErrorHandler = &errorHandlerDelegate{
|
||||
delegate: &defaultErrorHandler{},
|
||||
}
|
||||
globalErrorHandler = defaultErrorHandler()
|
||||
|
||||
// delegateErrorHandlerOnce ensures that a user provided ErrorHandler is
|
||||
// only ever registered once.
|
||||
delegateErrorHandlerOnce sync.Once
|
||||
|
||||
// Compile-time check that delegator implements ErrorHandler.
|
||||
_ ErrorHandler = (*errorHandlerDelegate)(nil)
|
||||
_ ErrorHandler = (*defaultErrorHandler)(nil)
|
||||
_ ErrorHandler = (*delegator)(nil)
|
||||
)
|
||||
|
||||
// errorHandlerDelegate is a box type to enable updating of all handlers returned by GetErrorHandler()
|
||||
type errorHandlerDelegate struct {
|
||||
delegate ErrorHandler
|
||||
type holder struct {
|
||||
eh ErrorHandler
|
||||
}
|
||||
|
||||
func (h *errorHandlerDelegate) setDelegate(d ErrorHandler) {
|
||||
h.delegate = d
|
||||
func defaultErrorHandler() *atomic.Value {
|
||||
v := &atomic.Value{}
|
||||
v.Store(holder{eh: &delegator{l: log.New(os.Stderr, "", log.LstdFlags)}})
|
||||
return v
|
||||
}
|
||||
|
||||
// Handle handles any error deemed irremediable by an OpenTelemetry component.
|
||||
func (h *errorHandlerDelegate) Handle(err error) {
|
||||
if h == nil {
|
||||
// delegator logs errors if no delegate is set, otherwise they are delegated.
|
||||
type delegator struct {
|
||||
delegate atomic.Value
|
||||
|
||||
l *log.Logger
|
||||
}
|
||||
|
||||
// setDelegate sets the ErrorHandler delegate.
|
||||
func (h *delegator) setDelegate(d ErrorHandler) {
|
||||
// It is critical this is guarded with delegateErrorHandlerOnce, if it is
|
||||
// called again with a different concrete type it will panic.
|
||||
h.delegate.Store(d)
|
||||
}
|
||||
|
||||
// Handle logs err if no delegate is set, otherwise it is delegated.
|
||||
func (h *delegator) Handle(err error) {
|
||||
if d := h.delegate.Load(); d != nil {
|
||||
d.(ErrorHandler).Handle(err)
|
||||
return
|
||||
}
|
||||
h.delegate.Handle(err)
|
||||
h.l.Print(err)
|
||||
}
|
||||
|
||||
// defaultErrorHandler utilizes the internal logger to manage the messages.
|
||||
type defaultErrorHandler struct{}
|
||||
|
||||
func (h *defaultErrorHandler) Handle(err error) {
|
||||
debug.Error(err, "")
|
||||
}
|
||||
|
||||
// DiscardErrorHandler drops all errors.
|
||||
// Use `SetErrorHandler(DiscardErrorHandler{})` to disable error handling
|
||||
type DiscardErrorHandler struct{}
|
||||
|
||||
func (DiscardErrorHandler) Handle(error) {}
|
||||
|
||||
// GetErrorHandler returns the global ErrorHandler instance.
|
||||
//
|
||||
// The default ErrorHandler instance returned will log all errors to STDERR
|
||||
// until an override ErrorHandler is set with SetErrorHandler. All
|
||||
// ErrorHandler returned prior to this will automatically forward errors to
|
||||
// the set instance instead of logging.
|
||||
//
|
||||
// Subsequent calls to SetErrorHandler after the first will not forward errors
|
||||
// to the new ErrorHandler for prior returned instances.
|
||||
func GetErrorHandler() ErrorHandler {
|
||||
return globalErrorHandler
|
||||
return globalErrorHandler.Load().(holder).eh
|
||||
}
|
||||
|
||||
// SetErrorHandler sets the global ErrorHandler to h.
|
||||
//
|
||||
// The first time this is called all ErrorHandler previously returned from
|
||||
// GetErrorHandler will send errors to h instead of the default logging
|
||||
// ErrorHandler. Subsequent calls will set the global ErrorHandler, but not
|
||||
// delegate errors to h.
|
||||
func SetErrorHandler(h ErrorHandler) {
|
||||
globalErrorHandler.setDelegate(h)
|
||||
delegateErrorHandlerOnce.Do(func() {
|
||||
current := GetErrorHandler()
|
||||
if current == h {
|
||||
return
|
||||
}
|
||||
if internalHandler, ok := current.(*delegator); ok {
|
||||
internalHandler.setDelegate(h)
|
||||
}
|
||||
})
|
||||
globalErrorHandler.Store(holder{eh: h})
|
||||
}
|
||||
|
||||
// Handle is a convenience function for ErrorHandler().Handle(err)
|
||||
|
127
handler_test.go
127
handler_test.go
@ -15,18 +15,39 @@
|
||||
package otel
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type errLogger struct {
|
||||
errs []string
|
||||
type errLogger []string
|
||||
|
||||
func (l *errLogger) Write(p []byte) (int, error) {
|
||||
msg := bytes.TrimRight(p, "\n")
|
||||
(*l) = append(*l, string(msg))
|
||||
return len(msg), nil
|
||||
}
|
||||
|
||||
func (l *errLogger) Handle(err error) {
|
||||
l.errs = append(l.errs, err.Error())
|
||||
func (l *errLogger) Reset() {
|
||||
*l = errLogger([]string{})
|
||||
}
|
||||
|
||||
func (l *errLogger) Got() []string {
|
||||
return []string(*l)
|
||||
}
|
||||
|
||||
type logger struct {
|
||||
l *log.Logger
|
||||
}
|
||||
|
||||
func (l *logger) Handle(err error) {
|
||||
l.l.Print(err)
|
||||
}
|
||||
|
||||
func causeErr(text string) {
|
||||
@ -41,73 +62,76 @@ type HandlerTestSuite struct {
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) SetupSuite() {
|
||||
s.errLogger = &errLogger{errs: []string{}}
|
||||
s.origHandler = globalErrorHandler.delegate
|
||||
s.errLogger = new(errLogger)
|
||||
s.origHandler = globalErrorHandler.Load().(holder).eh
|
||||
|
||||
globalErrorHandler.delegate = s.errLogger
|
||||
globalErrorHandler.Store(holder{eh: &delegator{l: log.New(s.errLogger, "", 0)}})
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TearDownSuite() {
|
||||
globalErrorHandler.delegate = s.origHandler
|
||||
globalErrorHandler.Store(holder{eh: s.origHandler})
|
||||
delegateErrorHandlerOnce = sync.Once{}
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) SetupTest() {
|
||||
s.errLogger.errs = []string{}
|
||||
SetErrorHandler(s.errLogger)
|
||||
s.errLogger.Reset()
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TearDownTest() {}
|
||||
func (s *HandlerTestSuite) TearDownTest() {
|
||||
globalErrorHandler.Store(holder{eh: &delegator{l: log.New(s.errLogger, "", 0)}})
|
||||
delegateErrorHandlerOnce = sync.Once{}
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TestGlobalHandler() {
|
||||
errs := []string{"one", "two"}
|
||||
GetErrorHandler().Handle(errors.New(errs[0]))
|
||||
Handle(errors.New(errs[1]))
|
||||
s.Assert().Equal(errs, s.errLogger.errs)
|
||||
s.Assert().Equal(errs, s.errLogger.Got())
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TestDelegatedHandler() {
|
||||
eh := GetErrorHandler()
|
||||
|
||||
newErrLogger := &errLogger{errs: []string{}}
|
||||
SetErrorHandler(newErrLogger)
|
||||
newErrLogger := new(errLogger)
|
||||
SetErrorHandler(&logger{l: log.New(newErrLogger, "", 0)})
|
||||
|
||||
errs := []string{"TestDelegatedHandler"}
|
||||
eh.Handle(errors.New(errs[0]))
|
||||
s.Assert().Equal(errs, newErrLogger.errs)
|
||||
s.Assert().Equal(errs, newErrLogger.Got())
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TestSettingDefaultIsANoOp() {
|
||||
SetErrorHandler(GetErrorHandler())
|
||||
d := globalErrorHandler.Load().(holder).eh.(*delegator)
|
||||
s.Assert().Nil(d.delegate.Load())
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TestNoDropsOnDelegate() {
|
||||
causeErr("")
|
||||
s.Require().Len(s.errLogger.errs, 1)
|
||||
s.Require().Len(s.errLogger.Got(), 1)
|
||||
|
||||
// Change to another Handler. We are testing this is loss-less.
|
||||
newErrLogger := &errLogger{errs: []string{}}
|
||||
SetErrorHandler(newErrLogger)
|
||||
newErrLogger := new(errLogger)
|
||||
secondary := &logger{
|
||||
l: log.New(newErrLogger, "", 0),
|
||||
}
|
||||
SetErrorHandler(secondary)
|
||||
|
||||
causeErr("")
|
||||
s.Assert().Len(s.errLogger.errs, 1, "original Handler used after delegation")
|
||||
s.Assert().Len(newErrLogger.errs, 1, "new Handler not used after delegation")
|
||||
s.Assert().Len(s.errLogger.Got(), 1, "original Handler used after delegation")
|
||||
s.Assert().Len(newErrLogger.Got(), 1, "new Handler not used after delegation")
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TestAllowMultipleSets() {
|
||||
secondary := &errLogger{errs: []string{}}
|
||||
notUsed := new(errLogger)
|
||||
|
||||
secondary := &logger{l: log.New(notUsed, "", 0)}
|
||||
SetErrorHandler(secondary)
|
||||
s.Require().Same(GetErrorHandler().(*errorHandlerDelegate).delegate, secondary, "new Handler not set")
|
||||
s.Require().Same(GetErrorHandler(), secondary, "new Handler not set")
|
||||
|
||||
tertiary := &errLogger{errs: []string{}}
|
||||
tertiary := &logger{l: log.New(notUsed, "", 0)}
|
||||
SetErrorHandler(tertiary)
|
||||
s.Assert().Same(GetErrorHandler().(*errorHandlerDelegate).delegate, tertiary, "user Handler not overridden")
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TestGetErrorHandlerAlwaysIsCurrent() {
|
||||
orig := GetErrorHandler()
|
||||
|
||||
newErrLogger := &errLogger{errs: []string{}}
|
||||
SetErrorHandler(newErrLogger)
|
||||
|
||||
orig.Handle(errors.New("error"))
|
||||
|
||||
s.Assert().Len(newErrLogger.errs, 1, "original Handler did not update")
|
||||
s.Assert().Same(GetErrorHandler(), tertiary, "user Handler not overridden")
|
||||
}
|
||||
|
||||
func TestHandlerTestSuite(t *testing.T) {
|
||||
@ -115,16 +139,17 @@ func TestHandlerTestSuite(t *testing.T) {
|
||||
}
|
||||
|
||||
func BenchmarkErrorHandler(b *testing.B) {
|
||||
primary := DiscardErrorHandler{}
|
||||
secondary := DiscardErrorHandler{}
|
||||
tertiary := DiscardErrorHandler{}
|
||||
primary := &delegator{l: log.New(ioutil.Discard, "", 0)}
|
||||
secondary := &logger{l: log.New(ioutil.Discard, "", 0)}
|
||||
tertiary := &logger{l: log.New(ioutil.Discard, "", 0)}
|
||||
|
||||
globalErrorHandler.Store(holder{eh: primary})
|
||||
|
||||
err := errors.New("BenchmarkErrorHandler")
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
SetErrorHandler(primary)
|
||||
GetErrorHandler().Handle(err)
|
||||
Handle(err)
|
||||
|
||||
@ -136,6 +161,11 @@ func BenchmarkErrorHandler(b *testing.B) {
|
||||
GetErrorHandler().Handle(err)
|
||||
Handle(err)
|
||||
|
||||
b.StopTimer()
|
||||
primary.delegate = atomic.Value{}
|
||||
globalErrorHandler.Store(holder{eh: primary})
|
||||
delegateErrorHandlerOnce = sync.Once{}
|
||||
b.StartTimer()
|
||||
}
|
||||
|
||||
b.StopTimer()
|
||||
@ -152,7 +182,7 @@ func BenchmarkGetDefaultErrorHandler(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkGetDelegatedErrorHandler(b *testing.B) {
|
||||
SetErrorHandler(DiscardErrorHandler{})
|
||||
SetErrorHandler(&logger{l: log.New(ioutil.Discard, "", 0)})
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
@ -165,7 +195,9 @@ func BenchmarkGetDelegatedErrorHandler(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkDefaultErrorHandlerHandle(b *testing.B) {
|
||||
SetErrorHandler(DiscardErrorHandler{})
|
||||
globalErrorHandler.Store(holder{
|
||||
eh: &delegator{l: log.New(ioutil.Discard, "", 0)},
|
||||
})
|
||||
|
||||
eh := GetErrorHandler()
|
||||
err := errors.New("BenchmarkDefaultErrorHandlerHandle")
|
||||
@ -182,7 +214,7 @@ func BenchmarkDefaultErrorHandlerHandle(b *testing.B) {
|
||||
|
||||
func BenchmarkDelegatedErrorHandlerHandle(b *testing.B) {
|
||||
eh := GetErrorHandler()
|
||||
SetErrorHandler(DiscardErrorHandler{})
|
||||
SetErrorHandler(&logger{l: log.New(ioutil.Discard, "", 0)})
|
||||
err := errors.New("BenchmarkDelegatedErrorHandlerHandle")
|
||||
|
||||
b.ReportAllocs()
|
||||
@ -196,7 +228,7 @@ func BenchmarkDelegatedErrorHandlerHandle(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkSetErrorHandlerDelegation(b *testing.B) {
|
||||
alt := DiscardErrorHandler{}
|
||||
alt := &logger{l: log.New(ioutil.Discard, "", 0)}
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
@ -211,8 +243,8 @@ func BenchmarkSetErrorHandlerDelegation(b *testing.B) {
|
||||
|
||||
func BenchmarkSetErrorHandlerNoDelegation(b *testing.B) {
|
||||
eh := []ErrorHandler{
|
||||
DiscardErrorHandler{},
|
||||
DiscardErrorHandler{},
|
||||
&logger{l: log.New(ioutil.Discard, "", 0)},
|
||||
&logger{l: log.New(ioutil.Discard, "", 0)},
|
||||
}
|
||||
mod := len(eh)
|
||||
// Do not measure delegation.
|
||||
@ -229,7 +261,6 @@ func BenchmarkSetErrorHandlerNoDelegation(b *testing.B) {
|
||||
}
|
||||
|
||||
func reset() {
|
||||
globalErrorHandler = &errorHandlerDelegate{
|
||||
delegate: &defaultErrorHandler{},
|
||||
}
|
||||
globalErrorHandler = defaultErrorHandler()
|
||||
delegateErrorHandlerOnce = sync.Once{}
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
|
@ -1,8 +1,6 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
|
@ -1,8 +1,6 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
|
@ -2,9 +2,7 @@ github.com/benbjohnson/clock v1.2.0 h1:9Re3G2TWxkE06LdMWMpcY6KV81GLXMGiYpPYUPkFA
|
||||
github.com/benbjohnson/clock v1.2.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
|
||||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.0 h1:j4LrlVXgrbIWO83mmQUnK0Hi+YnbD+vzrE1z/EphbFE=
|
||||
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
|
||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
|
Loading…
Reference in New Issue
Block a user