| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package componentstatus is an experimental module that defines how components should |
| 5 |
// report health statues, how collector hosts should facilitate component status reporting, |
| 6 |
// and how extensions should watch for new component statuses. |
| 7 |
// |
| 8 |
// This package is currently under development and is exempt from the Collector SIG's |
| 9 |
// breaking change policy. |
| 10 |
package componentstatus // import "go.opentelemetry.io/collector/component/componentstatus" |
| 11 |
|
| 12 |
import ( |
| 13 |
"time" |
| 14 |
|
| 15 |
"go.opentelemetry.io/collector/component" |
| 16 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 17 |
) |
| 18 |
|
| 19 |
// Reporter is an extra interface for `component.Host` implementations. |
| 20 |
// A Reporter defines how to report a `componentstatus.Event`. |
| 21 |
type Reporter interface { |
| 22 |
// Report allows a component to report runtime changes in status. The service |
| 23 |
// will automatically report status for a component during startup and shutdown. Components can |
| 24 |
// use this method to report status after start and before shutdown. For more details about |
| 25 |
// component status reporting see: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-status.md |
| 26 |
Report(*Event) |
| 27 |
} |
| 28 |
|
| 29 |
// Watcher is an extra interface for Extension hosted by the OpenTelemetry |
| 30 |
// Collector that is to be implemented by extensions interested in changes to component |
| 31 |
// status. |
| 32 |
// |
| 33 |
// TODO: consider moving this interface to a new package/module like `extension/statuswatcher` |
| 34 |
// https://github.com/open-telemetry/opentelemetry-collector/issues/10764 |
| 35 |
type Watcher interface { |
| 36 |
// ComponentStatusChanged notifies about a change in the source component status. |
| 37 |
// Extensions that implement this interface must be ready that the ComponentStatusChanged |
| 38 |
// may be called before, after or concurrently with calls to Component.Start() and Component.Shutdown(). |
| 39 |
// The function may be called concurrently with itself. |
| 40 |
ComponentStatusChanged(source *InstanceID, event *Event) |
| 41 |
} |
| 42 |
|
| 43 |
type Status int32 |
| 44 |
|
| 45 |
// Enumeration of possible component statuses |
| 46 |
const ( |
| 47 |
// StatusNone indicates absence of component status. |
| 48 |
StatusNone Status = iota |
| 49 |
// StatusStarting indicates the component is starting. |
| 50 |
StatusStarting |
| 51 |
// StatusOK indicates the component is running without issues. |
| 52 |
StatusOK |
| 53 |
// StatusRecoverableError indicates that the component has experienced a transient error and may recover. |
| 54 |
StatusRecoverableError |
| 55 |
// StatusPermanentError indicates that the component has detected a condition at runtime that will need human intervention to fix. The collector will continue to run in a degraded mode. |
| 56 |
StatusPermanentError |
| 57 |
// StatusFatalError indicates that the collector has experienced a fatal runtime error and will shut down. |
| 58 |
StatusFatalError |
| 59 |
// StatusStopping indicates that the component is in the process of shutting down. |
| 60 |
StatusStopping |
| 61 |
// StatusStopped indicates that the component has completed shutdown. |
| 62 |
StatusStopped |
| 63 |
) |
| 64 |
|
| 65 |
// String returns a string representation of a Status |
| 66 |
func (s Status) String() string { |
| 67 |
switch s { |
| 68 |
case StatusStarting: |
| 69 |
return "StatusStarting" |
| 70 |
case StatusOK: |
| 71 |
return "StatusOK" |
| 72 |
case StatusRecoverableError: |
| 73 |
return "StatusRecoverableError" |
| 74 |
case StatusPermanentError: |
| 75 |
return "StatusPermanentError" |
| 76 |
case StatusFatalError: |
| 77 |
return "StatusFatalError" |
| 78 |
case StatusStopping: |
| 79 |
return "StatusStopping" |
| 80 |
case StatusStopped: |
| 81 |
return "StatusStopped" |
| 82 |
} |
| 83 |
return "StatusNone" |
| 84 |
} |
| 85 |
|
| 86 |
// Event contains a status, a timestamp, optional error information, and additional attributes |
| 87 |
type Event struct { |
| 88 |
// attributes provides additional context or metadata for the event. |
| 89 |
attributes pcommon.Map |
| 90 |
|
| 91 |
status Status |
| 92 |
err error |
| 93 |
// TODO: consider if a timestamp is necessary in the default Event struct or is needed only for the healthcheckv2 extension |
| 94 |
// https://github.com/open-telemetry/opentelemetry-collector/issues/10763 |
| 95 |
timestamp time.Time |
| 96 |
} |
| 97 |
|
| 98 |
// Status returns the Status (enum) associated with the Event |
| 99 |
func (ev *Event) Status() Status { |
| 100 |
return ev.status |
| 101 |
} |
| 102 |
|
| 103 |
// Err returns the error associated with the Event. |
| 104 |
func (ev *Event) Err() error { |
| 105 |
return ev.err |
| 106 |
} |
| 107 |
|
| 108 |
// Attributes returns the attributes (pcommon.Map) associated with the Event. |
| 109 |
func (ev *Event) Attributes() pcommon.Map { |
| 110 |
return ev.attributes |
| 111 |
} |
| 112 |
|
| 113 |
// Timestamp returns the timestamp associated with the Event |
| 114 |
func (ev *Event) Timestamp() time.Time { |
| 115 |
return ev.timestamp |
| 116 |
} |
| 117 |
|
| 118 |
// EventBuilderOption is a sealed interface wrapping options for [NewEvent]. |
| 119 |
type EventBuilderOption interface { |
| 120 |
applyOption(*Event) |
| 121 |
} |
| 122 |
|
| 123 |
type eventOptionFunc func(*Event) |
| 124 |
|
| 125 |
func (f eventOptionFunc) applyOption(event *Event) { |
| 126 |
f(event) |
| 127 |
} |
| 128 |
|
| 129 |
// NewEvent creates and returns an Event with the specified options and sets the timestamp |
| 130 |
// time.Now(). To set an error on the event for an error status use the |
| 131 |
// WithError with one of the dedicated status (e.g. StatusRecoverableError, StatusPermanentError, StatusFatalError) |
| 132 |
func NewEvent(st Status, opts ...EventBuilderOption) *Event { |
| 133 |
event := &Event{ |
| 134 |
timestamp: time.Now(), |
| 135 |
attributes: pcommon.NewMap(), |
| 136 |
status: st, |
| 137 |
} |
| 138 |
|
| 139 |
for _, opt := range opts { |
| 140 |
opt.applyOption(event) |
| 141 |
} |
| 142 |
|
| 143 |
return event |
| 144 |
} |
| 145 |
|
| 146 |
// WithAttributes sets the Event attributes. |
| 147 |
func WithAttributes(attributes pcommon.Map) EventBuilderOption { |
| 148 |
return eventOptionFunc(func(e *Event) { |
| 149 |
e.attributes = attributes |
| 150 |
}) |
| 151 |
} |
| 152 |
|
| 153 |
// WithError sets the Event error. |
| 154 |
func WithError(err error) EventBuilderOption { |
| 155 |
return eventOptionFunc(func(e *Event) { |
| 156 |
e.err = err |
| 157 |
}) |
| 158 |
} |
| 159 |
|
| 160 |
// NewRecoverableErrorEvent wraps a transient error |
| 161 |
// passed as argument as a Event with a status StatusRecoverableError |
| 162 |
// and a timestamp set to time.Now(). |
| 163 |
func NewRecoverableErrorEvent(err error) *Event { |
| 164 |
return NewEvent(StatusRecoverableError, WithError(err)) |
| 165 |
} |
| 166 |
|
| 167 |
// NewPermanentErrorEvent wraps an error requiring human intervention to fix |
| 168 |
// passed as argument as a Event with a status StatusPermanentError |
| 169 |
// and a timestamp set to time.Now(). |
| 170 |
func NewPermanentErrorEvent(err error) *Event { |
| 171 |
return NewEvent(StatusPermanentError, WithError(err)) |
| 172 |
} |
| 173 |
|
| 174 |
// NewFatalErrorEvent wraps the fatal runtime error passed as argument as a Event |
| 175 |
// with a status StatusFatalError and a timestamp set to time.Now(). |
| 176 |
func NewFatalErrorEvent(err error) *Event { |
| 177 |
return NewEvent(StatusFatalError, WithError(err)) |
| 178 |
} |
| 179 |
|
| 180 |
// StatusIsError returns true for error statuses (e.g. StatusRecoverableError, |
| 181 |
// StatusPermanentError, or StatusFatalError) |
| 182 |
func StatusIsError(status Status) bool { |
| 183 |
return status == StatusRecoverableError || |
| 184 |
status == StatusPermanentError || |
| 185 |
status == StatusFatalError |
| 186 |
} |
| 187 |
|
| 188 |
// ReportStatus is a helper function that handles checking if the component.Host has implemented Reporter. |
| 189 |
// If it has, the Event is reported. Otherwise, nothing happens. |
| 190 |
func ReportStatus(host component.Host, e *Event) { |
| 191 |
statusReporter, ok := host.(Reporter) |
| 192 |
if ok { |
| 193 |
statusReporter.Report(e) |
| 194 |
} |
| 195 |
} |