diff --git a/api/global/handler.go b/api/global/handler.go index b40eb732046..83f3e523a9a 100644 --- a/api/global/handler.go +++ b/api/global/handler.go @@ -20,35 +20,35 @@ import ( "sync" "sync/atomic" - "go.opentelemetry.io/otel/api/oterror" + "go.opentelemetry.io/otel" ) var ( - // globalHandler provides an oterror.Handler that can be used throughout - // an OpenTelemetry instrumented project. When a user specified Handler - // is registered (`SetHandler`) all calls to `Handle` will be delegated - // to the registered Handler. - globalHandler = &handler{ + // globalErrorHandler provides an ErrorHandler that can be used + // 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 = &loggingErrorHandler{ l: log.New(os.Stderr, "", log.LstdFlags), } - // delegateHanderOnce ensures that a user provided Handler is only ever - // registered once. - delegateHanderOnce sync.Once + // delegateErrorHandlerOnce ensures that a user provided ErrorHandler is + // only ever registered once. + delegateErrorHandlerOnce sync.Once - // Ensure the handler implements oterror.Handle at build time. - _ oterror.Handler = (*handler)(nil) + // Comiple time check that loggingErrorHandler implements ErrorHandler. + _ otel.ErrorHandler = (*loggingErrorHandler)(nil) ) -// handler logs all errors to STDERR. -type handler struct { +// loggingErrorHandler logs all errors to STDERR. +type loggingErrorHandler struct { delegate atomic.Value l *log.Logger } -// setDelegate sets the handler delegate if one is not already set. -func (h *handler) setDelegate(d oterror.Handler) { +// setDelegate sets the ErrorHandler delegate if one is not already set. +func (h *loggingErrorHandler) setDelegate(d otel.ErrorHandler) { if h.delegate.Load() != nil { // Delegate already registered return @@ -56,37 +56,36 @@ func (h *handler) setDelegate(d oterror.Handler) { h.delegate.Store(d) } -// Handle implements oterror.Handler. -func (h *handler) Handle(err error) { +// Handle implements otel.ErrorHandler. +func (h *loggingErrorHandler) Handle(err error) { if d := h.delegate.Load(); d != nil { - d.(oterror.Handler).Handle(err) + d.(otel.ErrorHandler).Handle(err) return } h.l.Print(err) } -// Handler returns the global Handler instance. If no Handler instance has -// be explicitly set yet, a default Handler is returned that logs to STDERR -// until an Handler is set (all functionality is delegated to the set -// Handler once it is set). -func Handler() oterror.Handler { - return globalHandler +// ErrorHandler returns the global ErrorHandler instance. If no ErrorHandler +// instance has been set (`SetErrorHandler`), the default ErrorHandler which +// logs errors to STDERR is returned. +func ErrorHandler() otel.ErrorHandler { + return globalErrorHandler } -// SetHandler sets the global Handler to be h. -func SetHandler(h oterror.Handler) { - delegateHanderOnce.Do(func() { - current := Handler() +// SetErrorHandler sets the global ErrorHandler to be h. +func SetErrorHandler(h otel.ErrorHandler) { + delegateErrorHandlerOnce.Do(func() { + current := ErrorHandler() if current == h { return } - if internalHandler, ok := current.(*handler); ok { + if internalHandler, ok := current.(*loggingErrorHandler); ok { internalHandler.setDelegate(h) } }) } -// Handle is a convience function for Handler().Handle(err) +// Handle is a convience function for ErrorHandler().Handle(err) func Handle(err error) { - globalHandler.Handle(err) + ErrorHandler().Handle(err) } diff --git a/api/global/handler_test.go b/api/global/handler_test.go index 6e953dd8768..5713e0b8d36 100644 --- a/api/global/handler_test.go +++ b/api/global/handler_test.go @@ -44,20 +44,20 @@ func (l *errLogger) Got() []string { type HandlerTestSuite struct { suite.Suite - origHandler *handler + origHandler *loggingErrorHandler errLogger *errLogger } func (s *HandlerTestSuite) SetupSuite() { s.errLogger = new(errLogger) - s.origHandler = globalHandler - globalHandler = &handler{ + s.origHandler = globalErrorHandler + globalErrorHandler = &loggingErrorHandler{ l: log.New(s.errLogger, "", 0), } } func (s *HandlerTestSuite) TearDownSuite() { - globalHandler = s.origHandler + globalErrorHandler = s.origHandler } func (s *HandlerTestSuite) SetupTest() { @@ -66,7 +66,7 @@ func (s *HandlerTestSuite) SetupTest() { func (s *HandlerTestSuite) TestGlobalHandler() { errs := []string{"one", "two"} - Handler().Handle(errors.New(errs[0])) + ErrorHandler().Handle(errors.New(errs[0])) Handle(errors.New(errs[1])) s.Assert().Equal(errs, s.errLogger.Got()) } @@ -122,10 +122,10 @@ func (s *HandlerTestSuite) TestNoDropsOnDelegate() { // Change to another Handler. We are testing this is loss-less. newErrLogger := new(errLogger) - secondary := &handler{ + secondary := &loggingErrorHandler{ l: log.New(newErrLogger, "", 0), } - SetHandler(secondary) + SetErrorHandler(secondary) s.Require().NoError(wait(pause), "switched to new Handler") // Testing done, stop sending errors. diff --git a/api/metric/api_test.go b/api/metric/api_test.go index 4375a2b3197..66c6758eedd 100644 --- a/api/metric/api_test.go +++ b/api/metric/api_test.go @@ -22,7 +22,6 @@ import ( "go.opentelemetry.io/otel/api/kv" "go.opentelemetry.io/otel/api/metric" - "go.opentelemetry.io/otel/api/oterror" "go.opentelemetry.io/otel/api/unit" mockTest "go.opentelemetry.io/otel/internal/metric" @@ -392,7 +391,7 @@ func TestWrappedInstrumentError(t *testing.T) { valuerecorder, err := meter.NewInt64ValueRecorder("test.valuerecorder") - require.Equal(t, err, oterror.ErrSDKReturnedNilImpl) + require.Equal(t, err, metric.ErrSDKReturnedNilImpl) require.NotNil(t, valuerecorder.SyncImpl()) observer, err := meter.NewInt64ValueObserver("test.observer", func(_ context.Context, result metric.Int64ObserverResult) {}) diff --git a/api/metric/sync.go b/api/metric/sync.go index 46d8fa02c72..e98def2d2b4 100644 --- a/api/metric/sync.go +++ b/api/metric/sync.go @@ -16,11 +16,14 @@ package metric import ( "context" + "errors" "go.opentelemetry.io/otel/api/kv" - "go.opentelemetry.io/otel/api/oterror" ) +// ErrSDKReturnedNilImpl is returned when a new `MeterImpl` returns nil. +var ErrSDKReturnedNilImpl = errors.New("SDK returned a nil implementation") + // Measurement is used for reporting a synchronous batch of metric // values. Instances of this type should be created by synchronous // instruments (e.g., Int64Counter.Measurement()). @@ -110,7 +113,7 @@ func (h syncBoundInstrument) Unbind() { func checkNewAsync(instrument AsyncImpl, err error) (asyncInstrument, error) { if instrument == nil { if err == nil { - err = oterror.ErrSDKReturnedNilImpl + err = ErrSDKReturnedNilImpl } instrument = NoopAsync{} } @@ -125,7 +128,7 @@ func checkNewAsync(instrument AsyncImpl, err error) (asyncInstrument, error) { func checkNewSync(instrument SyncImpl, err error) (syncInstrument, error) { if instrument == nil { if err == nil { - err = oterror.ErrSDKReturnedNilImpl + err = ErrSDKReturnedNilImpl } // Note: an alternate behavior would be to synthesize a new name // or group all duplicately-named instruments of a certain type diff --git a/api/oterror/doc.go b/api/oterror/doc.go deleted file mode 100644 index 9d4b5f7756f..00000000000 --- a/api/oterror/doc.go +++ /dev/null @@ -1,26 +0,0 @@ -// 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. - -/* -* The oterror package provides unified error interactions in OpenTelemetry. -* This includes providing standardized errors common to OpenTelemetry (APIs, -* SDKs, and exporters). Additionally it provides an API for unified error -* handling in OpenTelemetry. -* -* The unified error handling interface is used for any error that -* OpenTelemetry component are not able to remediate on their own, instead -* handling them in a uniform and user-defined way. - */ - -package oterror diff --git a/api/oterror/errors.go b/api/oterror/errors.go deleted file mode 100644 index 06889425352..00000000000 --- a/api/oterror/errors.go +++ /dev/null @@ -1,22 +0,0 @@ -// 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 oterror - -import "errors" - -var ( - // ErrSDKReturnedNilImpl is returned when a new `MeterImpl` returns nil. - ErrSDKReturnedNilImpl = errors.New("SDK returned a nil implementation") -) diff --git a/api/oterror/handler.go b/error_handler.go similarity index 88% rename from api/oterror/handler.go rename to error_handler.go index 7f3e54e6462..3c5604917a2 100644 --- a/api/oterror/handler.go +++ b/error_handler.go @@ -12,10 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -package oterror +package otel -// Handler handles irremediable events. -type Handler interface { +// ErrorHandler handles irremediable events. +type ErrorHandler interface { // Handle handles any error deemed irremediable by an OpenTelemetry // component. Handle(error) diff --git a/sdk/metric/controller/push/push_test.go b/sdk/metric/controller/push/push_test.go index 6749f6f0ed3..ab9f4f55cbf 100644 --- a/sdk/metric/controller/push/push_test.go +++ b/sdk/metric/controller/push/push_test.go @@ -63,7 +63,7 @@ var testHandler *handler func init() { testHandler = new(handler) - global.SetHandler(testHandler) + global.SetErrorHandler(testHandler) } type testExporter struct { diff --git a/sdk/metric/correct_test.go b/sdk/metric/correct_test.go index 9c5292498dc..4809ee5fa44 100644 --- a/sdk/metric/correct_test.go +++ b/sdk/metric/correct_test.go @@ -67,7 +67,7 @@ var testHandler *handler func init() { testHandler = new(handler) - global.SetHandler(testHandler) + global.SetErrorHandler(testHandler) } type correctnessProcessor struct { diff --git a/sdk/trace/trace_test.go b/sdk/trace/trace_test.go index 752c80a6707..41fd30a5bcd 100644 --- a/sdk/trace/trace_test.go +++ b/sdk/trace/trace_test.go @@ -52,7 +52,7 @@ func init() { tid, _ = apitrace.IDFromHex("01020304050607080102040810203040") sid, _ = apitrace.SpanIDFromHex("0102040810203040") - global.SetHandler(new(discardHandler)) + global.SetErrorHandler(new(discardHandler)) } func TestTracerFollowsExpectedAPIBehaviour(t *testing.T) {