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