asyncfloat64.go 9.8 KB

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