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