handler.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 = defaultErrorHandler()
  27. // delegateErrorHandlerOnce ensures that a user provided ErrorHandler is
  28. // only ever registered once.
  29. delegateErrorHandlerOnce sync.Once
  30. // Compile-time check that delegator implements ErrorHandler.
  31. _ ErrorHandler = (*delegator)(nil)
  32. )
  33. type holder struct {
  34. eh ErrorHandler
  35. }
  36. func defaultErrorHandler() *atomic.Value {
  37. v := &atomic.Value{}
  38. v.Store(holder{eh: &delegator{l: log.New(os.Stderr, "", log.LstdFlags)}})
  39. return v
  40. }
  41. // delegator logs errors if no delegate is set, otherwise they are delegated.
  42. type delegator struct {
  43. delegate atomic.Value
  44. l *log.Logger
  45. }
  46. // setDelegate sets the ErrorHandler delegate.
  47. func (h *delegator) setDelegate(d ErrorHandler) {
  48. // It is critical this is guarded with delegateErrorHandlerOnce, if it is
  49. // called again with a different concrete type it will panic.
  50. h.delegate.Store(d)
  51. }
  52. // Handle logs err if no delegate is set, otherwise it is delegated.
  53. func (h *delegator) Handle(err error) {
  54. if d := h.delegate.Load(); d != nil {
  55. d.(ErrorHandler).Handle(err)
  56. return
  57. }
  58. h.l.Print(err)
  59. }
  60. // GetErrorHandler returns the global ErrorHandler instance.
  61. //
  62. // The default ErrorHandler instance returned will log all errors to STDERR
  63. // until an override ErrorHandler is set with SetErrorHandler. All
  64. // ErrorHandler returned prior to this will automatically forward errors to
  65. // the set instance instead of logging.
  66. //
  67. // Subsequent calls to SetErrorHandler after the first will not forward errors
  68. // to the new ErrorHandler for prior returned instances.
  69. func GetErrorHandler() ErrorHandler {
  70. return globalErrorHandler.Load().(holder).eh
  71. }
  72. // SetErrorHandler sets the global ErrorHandler to h.
  73. //
  74. // The first time this is called all ErrorHandler previously returned from
  75. // GetErrorHandler will send errors to h instead of the default logging
  76. // ErrorHandler. Subsequent calls will set the global ErrorHandler, but not
  77. // delegate errors to h.
  78. func SetErrorHandler(h ErrorHandler) {
  79. delegateErrorHandlerOnce.Do(func() {
  80. current := GetErrorHandler()
  81. if current == h {
  82. return
  83. }
  84. if internalHandler, ok := current.(*delegator); ok {
  85. internalHandler.setDelegate(h)
  86. }
  87. })
  88. globalErrorHandler.Store(holder{eh: h})
  89. }
  90. // Handle is a convenience function for ErrorHandler().Handle(err)
  91. func Handle(err error) {
  92. GetErrorHandler().Handle(err)
  93. }