2023-06-05 09:15:05 +01:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-04-11 10:46:18 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2023-06-05 09:15:05 +01:00
|
|
|
|
|
|
|
package global // import "go.opentelemetry.io/otel/internal/global"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrorHandler handles irremediable events.
|
|
|
|
type ErrorHandler interface {
|
|
|
|
// Handle handles any error deemed irremediable by an OpenTelemetry
|
|
|
|
// component.
|
|
|
|
Handle(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrDelegator struct {
|
2023-09-07 12:20:37 +01:00
|
|
|
delegate atomic.Pointer[ErrorHandler]
|
2023-06-05 09:15:05 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 10:46:18 +01:00
|
|
|
// Compile-time check that delegator implements ErrorHandler.
|
|
|
|
var _ ErrorHandler = (*ErrDelegator)(nil)
|
2023-06-05 09:15:05 +01:00
|
|
|
|
2024-04-11 10:46:18 +01:00
|
|
|
func (d *ErrDelegator) Handle(err error) {
|
|
|
|
if eh := d.delegate.Load(); eh != nil {
|
|
|
|
(*eh).Handle(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Print(err)
|
2023-06-05 09:15:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// setDelegate sets the ErrorHandler delegate.
|
|
|
|
func (d *ErrDelegator) setDelegate(eh ErrorHandler) {
|
2023-09-07 12:20:37 +01:00
|
|
|
d.delegate.Store(&eh)
|
2023-06-05 09:15:05 +01:00
|
|
|
}
|