handler.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 global // import "go.opentelemetry.io/otel/internal/global"
  15. import (
  16. "log"
  17. "os"
  18. "sync/atomic"
  19. )
  20. var (
  21. // GlobalErrorHandler provides an ErrorHandler that can be used
  22. // throughout an OpenTelemetry instrumented project. When a user
  23. // specified ErrorHandler is registered (`SetErrorHandler`) all calls to
  24. // `Handle` and will be delegated to the registered ErrorHandler.
  25. GlobalErrorHandler = defaultErrorHandler()
  26. // Compile-time check that delegator implements ErrorHandler.
  27. _ ErrorHandler = (*ErrDelegator)(nil)
  28. // Compile-time check that errLogger implements ErrorHandler.
  29. _ ErrorHandler = (*ErrLogger)(nil)
  30. )
  31. // ErrorHandler handles irremediable events.
  32. type ErrorHandler interface {
  33. // Handle handles any error deemed irremediable by an OpenTelemetry
  34. // component.
  35. Handle(error)
  36. }
  37. type ErrDelegator struct {
  38. delegate atomic.Pointer[ErrorHandler]
  39. }
  40. func (d *ErrDelegator) Handle(err error) {
  41. d.getDelegate().Handle(err)
  42. }
  43. func (d *ErrDelegator) getDelegate() ErrorHandler {
  44. return *d.delegate.Load()
  45. }
  46. // setDelegate sets the ErrorHandler delegate.
  47. func (d *ErrDelegator) setDelegate(eh ErrorHandler) {
  48. d.delegate.Store(&eh)
  49. }
  50. func defaultErrorHandler() *ErrDelegator {
  51. d := &ErrDelegator{}
  52. d.setDelegate(&ErrLogger{l: log.New(os.Stderr, "", log.LstdFlags)})
  53. return d
  54. }
  55. // ErrLogger logs errors if no delegate is set, otherwise they are delegated.
  56. type ErrLogger struct {
  57. l *log.Logger
  58. }
  59. // Handle logs err if no delegate is set, otherwise it is delegated.
  60. func (h *ErrLogger) Handle(err error) {
  61. h.l.Print(err)
  62. }
  63. // GetErrorHandler returns the global ErrorHandler instance.
  64. //
  65. // The default ErrorHandler instance returned will log all errors to STDERR
  66. // until an override ErrorHandler is set with SetErrorHandler. All
  67. // ErrorHandler returned prior to this will automatically forward errors to
  68. // the set instance instead of logging.
  69. //
  70. // Subsequent calls to SetErrorHandler after the first will not forward errors
  71. // to the new ErrorHandler for prior returned instances.
  72. func GetErrorHandler() ErrorHandler {
  73. return GlobalErrorHandler
  74. }
  75. // SetErrorHandler sets the global ErrorHandler to h.
  76. //
  77. // The first time this is called all ErrorHandler previously returned from
  78. // GetErrorHandler will send errors to h instead of the default logging
  79. // ErrorHandler. Subsequent calls will set the global ErrorHandler, but not
  80. // delegate errors to h.
  81. func SetErrorHandler(h ErrorHandler) {
  82. GlobalErrorHandler.setDelegate(h)
  83. }
  84. // Handle is a convenience function for ErrorHandler().Handle(err).
  85. func Handle(err error) {
  86. GetErrorHandler().Handle(err)
  87. }