handler.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package otel // import "go.opentelemetry.io/otel"
  15. import (
  16. "log"
  17. "os"
  18. "sync"
  19. "sync/atomic"
  20. )
  21. var (
  22. // globalErrorHandler provides an ErrorHandler that can be used
  23. // throughout an OpenTelemetry instrumented project. When a user
  24. // specified ErrorHandler is registered (`SetErrorHandler`) all calls to
  25. // `Handle` and will be delegated to the registered ErrorHandler.
  26. globalErrorHandler = &loggingErrorHandler{
  27. l: log.New(os.Stderr, "", log.LstdFlags),
  28. }
  29. // delegateErrorHandlerOnce ensures that a user provided ErrorHandler is
  30. // only ever registered once.
  31. delegateErrorHandlerOnce sync.Once
  32. // Comiple time check that loggingErrorHandler implements ErrorHandler.
  33. _ ErrorHandler = (*loggingErrorHandler)(nil)
  34. )
  35. // loggingErrorHandler logs all errors to STDERR.
  36. type loggingErrorHandler struct {
  37. delegate atomic.Value
  38. l *log.Logger
  39. }
  40. // setDelegate sets the ErrorHandler delegate if one is not already set.
  41. func (h *loggingErrorHandler) setDelegate(d ErrorHandler) {
  42. if h.delegate.Load() != nil {
  43. // Delegate already registered
  44. return
  45. }
  46. h.delegate.Store(d)
  47. }
  48. // Handle implements ErrorHandler.
  49. func (h *loggingErrorHandler) Handle(err error) {
  50. if d := h.delegate.Load(); d != nil {
  51. d.(ErrorHandler).Handle(err)
  52. return
  53. }
  54. h.l.Print(err)
  55. }
  56. // GetErrorHandler returns the global ErrorHandler instance. If no ErrorHandler
  57. // instance has been set (`SetErrorHandler`), the default ErrorHandler which
  58. // logs errors to STDERR is returned.
  59. func GetErrorHandler() ErrorHandler {
  60. return globalErrorHandler
  61. }
  62. // SetErrorHandler sets the global ErrorHandler to be h.
  63. func SetErrorHandler(h ErrorHandler) {
  64. delegateErrorHandlerOnce.Do(func() {
  65. current := GetErrorHandler()
  66. if current == h {
  67. return
  68. }
  69. if internalHandler, ok := current.(*loggingErrorHandler); ok {
  70. internalHandler.setDelegate(h)
  71. }
  72. })
  73. }
  74. // Handle is a convience function for ErrorHandler().Handle(err)
  75. func Handle(err error) {
  76. GetErrorHandler().Handle(err)
  77. }