| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 1 | // Copyright The OpenTelemetry Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package metric // import "go.opentelemetry.io/otel/metric" |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | |
| 9 | "go.opentelemetry.io/otel/metric/embedded" |
| 10 | ) |
| 11 | |
| 12 | // Float64Observable describes a set of instruments used asynchronously to |
| 13 | // record float64 measurements once per collection cycle. Observations of |
| 14 | // these instruments are only made within a callback. |
| 15 | // |
| 16 | // Warning: Methods may be added to this interface in minor releases. |
| 17 | type Float64Observable interface { |
| 18 | Observable |
| 19 | |
| 20 | float64Observable() |
| 21 | } |
| 22 | |
| 23 | // Float64ObservableCounter is an instrument used to asynchronously record |
| 24 | // increasing float64 measurements once per collection cycle. Observations are |
| 25 | // only made within a callback for this instrument. The value observed is |
| 26 | // assumed the to be the cumulative sum of the count. |
| 27 | // |
| 28 | // Warning: Methods may be added to this interface in minor releases. See |
| 29 | // package documentation on API implementation for information on how to set |
| 30 | // default behavior for |
| 31 | // unimplemented methods. |
| 32 | type Float64ObservableCounter interface { |
| 33 | // Users of the interface can ignore this. This embedded type is only used |
| 34 | // by implementations of this interface. See the "API Implementations" |
| 35 | // section of the package documentation for more information. |
| 36 | embedded.Float64ObservableCounter |
| 37 | |
| 38 | Float64Observable |
| 39 | } |
| 40 | |
| 41 | // Float64ObservableCounterConfig contains options for asynchronous counter |
| 42 | // instruments that record float64 values. |
| 43 | type Float64ObservableCounterConfig struct { |
| 44 | description string |
| 45 | unit string |
| 46 | callbacks []Float64Callback |
| 47 | } |
| 48 | |
| 49 | // NewFloat64ObservableCounterConfig returns a new |
| 50 | // [Float64ObservableCounterConfig] with all opts applied. |
| 51 | func NewFloat64ObservableCounterConfig(opts ...Float64ObservableCounterOption) Float64ObservableCounterConfig { |
| 52 | var config Float64ObservableCounterConfig |
| 53 | for _, o := range opts { |
| 54 | config = o.applyFloat64ObservableCounter(config) |
| 55 | } |
| 56 | return config |
| 57 | } |
| 58 | |
| 59 | // Description returns the configured description. |
| 60 | func (c Float64ObservableCounterConfig) Description() string { |
| 61 | return c.description |
| 62 | } |
| 63 | |
| 64 | // Unit returns the configured unit. |
| 65 | func (c Float64ObservableCounterConfig) Unit() string { |
| 66 | return c.unit |
| 67 | } |
| 68 | |
| 69 | // Callbacks returns the configured callbacks. |
| 70 | func (c Float64ObservableCounterConfig) Callbacks() []Float64Callback { |
| 71 | return c.callbacks |
| 72 | } |
| 73 | |
| 74 | // Float64ObservableCounterOption applies options to a |
| 75 | // [Float64ObservableCounterConfig]. See [Float64ObservableOption] and |
| 76 | // [InstrumentOption] for other options that can be used as a |
| 77 | // Float64ObservableCounterOption. |
| 78 | type Float64ObservableCounterOption interface { |
| 79 | applyFloat64ObservableCounter(Float64ObservableCounterConfig) Float64ObservableCounterConfig |
| 80 | } |
| 81 | |
| 82 | // Float64ObservableUpDownCounter is an instrument used to asynchronously |
| 83 | // record float64 measurements once per collection cycle. Observations are only |
| 84 | // made within a callback for this instrument. The value observed is assumed |
| 85 | // the to be the cumulative sum of the count. |
| 86 | // |
| 87 | // Warning: Methods may be added to this interface in minor releases. See |
| 88 | // package documentation on API implementation for information on how to set |
| 89 | // default behavior for unimplemented methods. |
| 90 | type Float64ObservableUpDownCounter interface { |
| 91 | // Users of the interface can ignore this. This embedded type is only used |
| 92 | // by implementations of this interface. See the "API Implementations" |
| 93 | // section of the package documentation for more information. |
| 94 | embedded.Float64ObservableUpDownCounter |
| 95 | |
| 96 | Float64Observable |
| 97 | } |
| 98 | |
| 99 | // Float64ObservableUpDownCounterConfig contains options for asynchronous |
| 100 | // counter instruments that record float64 values. |
| 101 | type Float64ObservableUpDownCounterConfig struct { |
| 102 | description string |
| 103 | unit string |
| 104 | callbacks []Float64Callback |
| 105 | } |
| 106 | |
| 107 | // NewFloat64ObservableUpDownCounterConfig returns a new |
| 108 | // [Float64ObservableUpDownCounterConfig] with all opts applied. |
| 109 | func NewFloat64ObservableUpDownCounterConfig( |
| 110 | opts ...Float64ObservableUpDownCounterOption, |
| 111 | ) Float64ObservableUpDownCounterConfig { |
| 112 | var config Float64ObservableUpDownCounterConfig |
| 113 | for _, o := range opts { |
| 114 | config = o.applyFloat64ObservableUpDownCounter(config) |
| 115 | } |
| 116 | return config |
| 117 | } |
| 118 | |
| 119 | // Description returns the configured description. |
| 120 | func (c Float64ObservableUpDownCounterConfig) Description() string { |
| 121 | return c.description |
| 122 | } |
| 123 | |
| 124 | // Unit returns the configured unit. |
| 125 | func (c Float64ObservableUpDownCounterConfig) Unit() string { |
| 126 | return c.unit |
| 127 | } |
| 128 | |
| 129 | // Callbacks returns the configured callbacks. |
| 130 | func (c Float64ObservableUpDownCounterConfig) Callbacks() []Float64Callback { |
| 131 | return c.callbacks |
| 132 | } |
| 133 | |
| 134 | // Float64ObservableUpDownCounterOption applies options to a |
| 135 | // [Float64ObservableUpDownCounterConfig]. See [Float64ObservableOption] and |
| 136 | // [InstrumentOption] for other options that can be used as a |
| 137 | // Float64ObservableUpDownCounterOption. |
| 138 | type Float64ObservableUpDownCounterOption interface { |
| 139 | applyFloat64ObservableUpDownCounter(Float64ObservableUpDownCounterConfig) Float64ObservableUpDownCounterConfig |
| 140 | } |
| 141 | |
| 142 | // Float64ObservableGauge is an instrument used to asynchronously record |
| 143 | // instantaneous float64 measurements once per collection cycle. Observations |
| 144 | // are only made within a callback for this instrument. |
| 145 | // |
| 146 | // Warning: Methods may be added to this interface in minor releases. See |
| 147 | // package documentation on API implementation for information on how to set |
| 148 | // default behavior for unimplemented methods. |
| 149 | type Float64ObservableGauge interface { |
| 150 | // Users of the interface can ignore this. This embedded type is only used |
| 151 | // by implementations of this interface. See the "API Implementations" |
| 152 | // section of the package documentation for more information. |
| 153 | embedded.Float64ObservableGauge |
| 154 | |
| 155 | Float64Observable |
| 156 | } |
| 157 | |
| 158 | // Float64ObservableGaugeConfig contains options for asynchronous counter |
| 159 | // instruments that record float64 values. |
| 160 | type Float64ObservableGaugeConfig struct { |
| 161 | description string |
| 162 | unit string |
| 163 | callbacks []Float64Callback |
| 164 | } |
| 165 | |
| 166 | // NewFloat64ObservableGaugeConfig returns a new [Float64ObservableGaugeConfig] |
| 167 | // with all opts applied. |
| 168 | func NewFloat64ObservableGaugeConfig(opts ...Float64ObservableGaugeOption) Float64ObservableGaugeConfig { |
| 169 | var config Float64ObservableGaugeConfig |
| 170 | for _, o := range opts { |
| 171 | config = o.applyFloat64ObservableGauge(config) |
| 172 | } |
| 173 | return config |
| 174 | } |
| 175 | |
| 176 | // Description returns the configured description. |
| 177 | func (c Float64ObservableGaugeConfig) Description() string { |
| 178 | return c.description |
| 179 | } |
| 180 | |
| 181 | // Unit returns the configured unit. |
| 182 | func (c Float64ObservableGaugeConfig) Unit() string { |
| 183 | return c.unit |
| 184 | } |
| 185 | |
| 186 | // Callbacks returns the configured callbacks. |
| 187 | func (c Float64ObservableGaugeConfig) Callbacks() []Float64Callback { |
| 188 | return c.callbacks |
| 189 | } |
| 190 | |
| 191 | // Float64ObservableGaugeOption applies options to a |
| 192 | // [Float64ObservableGaugeConfig]. See [Float64ObservableOption] and |
| 193 | // [InstrumentOption] for other options that can be used as a |
| 194 | // Float64ObservableGaugeOption. |
| 195 | type Float64ObservableGaugeOption interface { |
| 196 | applyFloat64ObservableGauge(Float64ObservableGaugeConfig) Float64ObservableGaugeConfig |
| 197 | } |
| 198 | |
| 199 | // Float64Observer is a recorder of float64 measurements. |
| 200 | // |
| 201 | // Warning: Methods may be added to this interface in minor releases. See |
| 202 | // package documentation on API implementation for information on how to set |
| 203 | // default behavior for unimplemented methods. |
| 204 | type Float64Observer interface { |
| 205 | // Users of the interface can ignore this. This embedded type is only used |
| 206 | // by implementations of this interface. See the "API Implementations" |
| 207 | // section of the package documentation for more information. |
| 208 | embedded.Float64Observer |
| 209 | |
| 210 | // Observe records the float64 value. |
| 211 | // |
| 212 | // Use the WithAttributeSet (or, if performance is not a concern, |
| 213 | // the WithAttributes) option to include measurement attributes. |
| 214 | Observe(value float64, options ...ObserveOption) |
| 215 | } |
| 216 | |
| 217 | // Float64Callback is a function registered with a Meter that makes |
| 218 | // observations for a Float64Observable instrument it is registered with. |
| 219 | // Calls to the Float64Observer record measurement values for the |
| 220 | // Float64Observable. |
| 221 | // |
| 222 | // The function needs to complete in a finite amount of time and the deadline |
| 223 | // of the passed context is expected to be honored. |
| 224 | // |
| 225 | // The function needs to make unique observations across all registered |
| 226 | // Float64Callbacks. Meaning, it should not report measurements with the same |
| 227 | // attributes as another Float64Callbacks also registered for the same |
| 228 | // instrument. |
| 229 | // |
| 230 | // The function needs to be concurrent safe. |
| 231 | type Float64Callback func(context.Context, Float64Observer) error |
| 232 | |
| 233 | // Float64ObservableOption applies options to float64 Observer instruments. |
| 234 | type Float64ObservableOption interface { |
| 235 | Float64ObservableCounterOption |
| 236 | Float64ObservableUpDownCounterOption |
| 237 | Float64ObservableGaugeOption |
| 238 | } |
| 239 | |
| 240 | type float64CallbackOpt struct { |
| 241 | cback Float64Callback |
| 242 | } |
| 243 | |
| 244 | func (o float64CallbackOpt) applyFloat64ObservableCounter( |
| 245 | cfg Float64ObservableCounterConfig, |
| 246 | ) Float64ObservableCounterConfig { |
| 247 | cfg.callbacks = append(cfg.callbacks, o.cback) |
| 248 | return cfg |
| 249 | } |
| 250 | |
| 251 | func (o float64CallbackOpt) applyFloat64ObservableUpDownCounter( |
| 252 | cfg Float64ObservableUpDownCounterConfig, |
| 253 | ) Float64ObservableUpDownCounterConfig { |
| 254 | cfg.callbacks = append(cfg.callbacks, o.cback) |
| 255 | return cfg |
| 256 | } |
| 257 | |
| 258 | func (o float64CallbackOpt) applyFloat64ObservableGauge(cfg Float64ObservableGaugeConfig) Float64ObservableGaugeConfig { |
| 259 | cfg.callbacks = append(cfg.callbacks, o.cback) |
| 260 | return cfg |
| 261 | } |
| 262 | |
| 263 | // WithFloat64Callback adds callback to be called for an instrument. |
| 264 | func WithFloat64Callback(callback Float64Callback) Float64ObservableOption { |
| 265 | return float64CallbackOpt{callback} |
| 266 | } |