handler.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/atomic"
  19. "unsafe"
  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. // Compile-time check that delegator implements ErrorHandler.
  28. _ ErrorHandler = (*delegator)(nil)
  29. // Compile-time check that errLogger implements ErrorHandler.
  30. _ ErrorHandler = (*errLogger)(nil)
  31. )
  32. type delegator struct {
  33. delegate unsafe.Pointer
  34. }
  35. func (d *delegator) Handle(err error) {
  36. d.getDelegate().Handle(err)
  37. }
  38. func (d *delegator) getDelegate() ErrorHandler {
  39. return *(*ErrorHandler)(atomic.LoadPointer(&d.delegate))
  40. }
  41. // setDelegate sets the ErrorHandler delegate.
  42. func (d *delegator) setDelegate(eh ErrorHandler) {
  43. atomic.StorePointer(&d.delegate, unsafe.Pointer(&eh))
  44. }
  45. func defaultErrorHandler() *delegator {
  46. d := &delegator{}
  47. d.setDelegate(&errLogger{l: log.New(os.Stderr, "", log.LstdFlags)})
  48. return d
  49. }
  50. // errLogger logs errors if no delegate is set, otherwise they are delegated.
  51. type errLogger struct {
  52. l *log.Logger
  53. }
  54. // Handle logs err if no delegate is set, otherwise it is delegated.
  55. func (h *errLogger) Handle(err error) {
  56. h.l.Print(err)
  57. }
  58. // GetErrorHandler returns the global ErrorHandler instance.
  59. //
  60. // The default ErrorHandler instance returned will log all errors to STDERR
  61. // until an override ErrorHandler is set with SetErrorHandler. All
  62. // ErrorHandler returned prior to this will automatically forward errors to
  63. // the set instance instead of logging.
  64. //
  65. // Subsequent calls to SetErrorHandler after the first will not forward errors
  66. // to the new ErrorHandler for prior returned instances.
  67. func GetErrorHandler() ErrorHandler {
  68. return globalErrorHandler
  69. }
  70. // SetErrorHandler sets the global ErrorHandler to h.
  71. //
  72. // The first time this is called all ErrorHandler previously returned from
  73. // GetErrorHandler will send errors to h instead of the default logging
  74. // ErrorHandler. Subsequent calls will set the global ErrorHandler, but not
  75. // delegate errors to h.
  76. func SetErrorHandler(h ErrorHandler) {
  77. globalErrorHandler.setDelegate(h)
  78. }
  79. // Handle is a convenience function for ErrorHandler().Handle(err).
  80. func Handle(err error) {
  81. GetErrorHandler().Handle(err)
  82. }