2020-05-30 00:34:53 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
package otel
|
2020-05-30 00:34:53 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-11-17 18:17:04 +02:00
|
|
|
"strings"
|
2021-08-06 19:05:32 +02:00
|
|
|
"sync"
|
2020-05-30 00:34:53 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
2021-11-17 18:17:04 +02:00
|
|
|
type errLogger struct {
|
|
|
|
errs []string
|
2021-08-06 19:05:32 +02:00
|
|
|
}
|
|
|
|
|
2021-11-17 18:17:04 +02:00
|
|
|
func (l *errLogger) Handle(err error) {
|
|
|
|
l.errs = append(l.errs, err.Error())
|
2021-08-06 19:05:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func causeErr(text string) {
|
|
|
|
Handle(errors.New(text))
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:34:53 +02:00
|
|
|
type HandlerTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2021-08-06 19:05:32 +02:00
|
|
|
origHandler ErrorHandler
|
2020-06-01 20:50:35 +02:00
|
|
|
errLogger *errLogger
|
2020-05-30 00:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HandlerTestSuite) SetupSuite() {
|
2021-11-17 18:17:04 +02:00
|
|
|
s.errLogger = &errLogger{errs: []string{}}
|
|
|
|
s.origHandler = globalErrorHandler.delegate
|
2021-08-06 19:05:32 +02:00
|
|
|
|
2021-11-17 18:17:04 +02:00
|
|
|
globalErrorHandler.delegate = s.errLogger
|
2020-05-30 00:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HandlerTestSuite) TearDownSuite() {
|
2021-11-17 18:17:04 +02:00
|
|
|
globalErrorHandler.delegate = s.origHandler
|
2021-08-06 19:05:32 +02:00
|
|
|
delegateErrorHandlerOnce = sync.Once{}
|
2020-05-30 00:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HandlerTestSuite) SetupTest() {
|
2021-11-17 18:17:04 +02:00
|
|
|
s.errLogger.errs = []string{}
|
|
|
|
SetErrorHandler(s.errLogger)
|
2020-05-30 00:34:53 +02:00
|
|
|
}
|
|
|
|
|
2021-11-17 18:17:04 +02:00
|
|
|
func (s *HandlerTestSuite) TearDownTest() {}
|
|
|
|
|
|
|
|
type bufferedErrorHandler struct {
|
|
|
|
buf *strings.Builder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *bufferedErrorHandler) Handle(err error) {
|
|
|
|
if h.buf != nil {
|
|
|
|
h.buf = &strings.Builder{}
|
|
|
|
}
|
|
|
|
h.buf.WriteString(err.Error())
|
2021-08-06 19:05:32 +02:00
|
|
|
}
|
|
|
|
|
2020-06-01 18:28:27 +02:00
|
|
|
func (s *HandlerTestSuite) TestGlobalHandler() {
|
2020-06-01 19:38:05 +02:00
|
|
|
errs := []string{"one", "two"}
|
2020-11-16 19:30:54 +02:00
|
|
|
GetErrorHandler().Handle(errors.New(errs[0]))
|
2020-06-01 19:38:05 +02:00
|
|
|
Handle(errors.New(errs[1]))
|
2021-11-17 18:17:04 +02:00
|
|
|
s.Assert().Equal(errs, s.errLogger.errs)
|
2020-05-30 00:34:53 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 19:05:32 +02:00
|
|
|
func (s *HandlerTestSuite) TestDelegatedHandler() {
|
|
|
|
eh := GetErrorHandler()
|
|
|
|
|
2021-11-17 18:17:04 +02:00
|
|
|
newErrLogger := &errLogger{errs: []string{}}
|
|
|
|
SetErrorHandler(newErrLogger)
|
2021-08-06 19:05:32 +02:00
|
|
|
|
|
|
|
errs := []string{"TestDelegatedHandler"}
|
|
|
|
eh.Handle(errors.New(errs[0]))
|
2021-11-17 18:17:04 +02:00
|
|
|
s.Assert().Equal(errs, newErrLogger.errs)
|
2021-08-06 19:05:32 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 00:34:53 +02:00
|
|
|
func (s *HandlerTestSuite) TestNoDropsOnDelegate() {
|
2021-08-06 19:05:32 +02:00
|
|
|
causeErr("")
|
2021-11-17 18:17:04 +02:00
|
|
|
s.Require().Len(s.errLogger.errs, 1)
|
2020-05-30 00:34:53 +02:00
|
|
|
|
|
|
|
// Change to another Handler. We are testing this is loss-less.
|
2021-11-17 18:17:04 +02:00
|
|
|
newErrLogger := &errLogger{errs: []string{}}
|
|
|
|
SetErrorHandler(newErrLogger)
|
2020-05-30 00:34:53 +02:00
|
|
|
|
2021-08-06 19:05:32 +02:00
|
|
|
causeErr("")
|
2021-11-17 18:17:04 +02:00
|
|
|
s.Assert().Len(s.errLogger.errs, 1, "original Handler used after delegation")
|
|
|
|
s.Assert().Len(newErrLogger.errs, 1, "new Handler not used after delegation")
|
2020-05-30 00:34:53 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 19:05:32 +02:00
|
|
|
func (s *HandlerTestSuite) TestAllowMultipleSets() {
|
2021-11-17 18:17:04 +02:00
|
|
|
secondary := &errLogger{errs: []string{}}
|
2021-08-06 19:05:32 +02:00
|
|
|
SetErrorHandler(secondary)
|
2021-11-17 18:17:04 +02:00
|
|
|
s.Require().Same(GetErrorHandler().(*errorHandlerDelegate).delegate, secondary, "new Handler not set")
|
2021-08-06 19:05:32 +02:00
|
|
|
|
2021-11-17 18:17:04 +02:00
|
|
|
tertiary := &errLogger{errs: []string{}}
|
2021-08-06 19:05:32 +02:00
|
|
|
SetErrorHandler(tertiary)
|
2021-11-17 18:17:04 +02:00
|
|
|
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")
|
2021-08-06 19:05:32 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 00:34:53 +02:00
|
|
|
func TestHandlerTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(HandlerTestSuite))
|
|
|
|
}
|
2021-08-06 19:05:32 +02:00
|
|
|
|
|
|
|
func BenchmarkErrorHandler(b *testing.B) {
|
2021-11-17 18:17:04 +02:00
|
|
|
primary := DiscardErrorHandler{}
|
|
|
|
secondary := DiscardErrorHandler{}
|
|
|
|
tertiary := DiscardErrorHandler{}
|
2021-08-06 19:05:32 +02:00
|
|
|
|
|
|
|
err := errors.New("BenchmarkErrorHandler")
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-11-17 18:17:04 +02:00
|
|
|
SetErrorHandler(primary)
|
2021-08-06 19:05:32 +02:00
|
|
|
GetErrorHandler().Handle(err)
|
|
|
|
Handle(err)
|
|
|
|
|
|
|
|
SetErrorHandler(secondary)
|
|
|
|
GetErrorHandler().Handle(err)
|
|
|
|
Handle(err)
|
|
|
|
|
|
|
|
SetErrorHandler(tertiary)
|
|
|
|
GetErrorHandler().Handle(err)
|
|
|
|
Handle(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
var eh ErrorHandler
|
|
|
|
|
|
|
|
func BenchmarkGetDefaultErrorHandler(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
eh = GetErrorHandler()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGetDelegatedErrorHandler(b *testing.B) {
|
2021-11-17 18:17:04 +02:00
|
|
|
SetErrorHandler(DiscardErrorHandler{})
|
2021-08-06 19:05:32 +02:00
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
eh = GetErrorHandler()
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDefaultErrorHandlerHandle(b *testing.B) {
|
2021-11-17 18:17:04 +02:00
|
|
|
SetErrorHandler(DiscardErrorHandler{})
|
2021-08-06 19:05:32 +02:00
|
|
|
|
|
|
|
eh := GetErrorHandler()
|
|
|
|
err := errors.New("BenchmarkDefaultErrorHandlerHandle")
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
eh.Handle(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDelegatedErrorHandlerHandle(b *testing.B) {
|
|
|
|
eh := GetErrorHandler()
|
2021-11-17 18:17:04 +02:00
|
|
|
SetErrorHandler(DiscardErrorHandler{})
|
2021-08-06 19:05:32 +02:00
|
|
|
err := errors.New("BenchmarkDelegatedErrorHandlerHandle")
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
eh.Handle(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSetErrorHandlerDelegation(b *testing.B) {
|
2021-11-17 18:17:04 +02:00
|
|
|
alt := DiscardErrorHandler{}
|
2021-08-06 19:05:32 +02:00
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
SetErrorHandler(alt)
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
reset()
|
|
|
|
b.StartTimer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSetErrorHandlerNoDelegation(b *testing.B) {
|
|
|
|
eh := []ErrorHandler{
|
2021-11-17 18:17:04 +02:00
|
|
|
DiscardErrorHandler{},
|
|
|
|
DiscardErrorHandler{},
|
2021-08-06 19:05:32 +02:00
|
|
|
}
|
|
|
|
mod := len(eh)
|
|
|
|
// Do not measure delegation.
|
|
|
|
SetErrorHandler(eh[1])
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
SetErrorHandler(eh[i%mod])
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func reset() {
|
2021-11-17 18:17:04 +02:00
|
|
|
globalErrorHandler = &errorHandlerDelegate{
|
|
|
|
delegate: &defaultErrorHandler{},
|
|
|
|
}
|
2021-08-06 19:05:32 +02:00
|
|
|
}
|