handler.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. )
  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 = (*delegator)(nil)
  28. // Compile-time check that errLogger implements ErrorHandler.
  29. _ ErrorHandler = (*errLogger)(nil)
  30. )
  31. type delegator struct {
  32. lock *sync.RWMutex
  33. eh ErrorHandler
  34. }
  35. func (d *delegator) Handle(err error) {
  36. d.lock.RLock()
  37. defer d.lock.RUnlock()
  38. d.eh.Handle(err)
  39. }
  40. // setDelegate sets the ErrorHandler delegate.
  41. func (d *delegator) setDelegate(eh ErrorHandler) {
  42. d.lock.Lock()
  43. defer d.lock.Unlock()
  44. d.eh = eh
  45. }
  46. func defaultErrorHandler() *delegator {
  47. return &delegator{
  48. lock: &sync.RWMutex{},
  49. eh: &errLogger{l: log.New(os.Stderr, "", log.LstdFlags)},
  50. }
  51. }
  52. // errLogger logs errors if no delegate is set, otherwise they are delegated.
  53. type errLogger struct {
  54. l *log.Logger
  55. }
  56. // Handle logs err if no delegate is set, otherwise it is delegated.
  57. func (h *errLogger) Handle(err error) {
  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
  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. globalErrorHandler.setDelegate(h)
  80. }
  81. // Handle is a convenience function for ErrorHandler().Handle(err).
  82. func Handle(err error) {
  83. GetErrorHandler().Handle(err)
  84. }