asyncfloat64.go 9.3 KB

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