asyncfloat64.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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(opts ...Float64ObservableUpDownCounterOption) Float64ObservableUpDownCounterConfig {
  93. var config Float64ObservableUpDownCounterConfig
  94. for _, o := range opts {
  95. config = o.applyFloat64ObservableUpDownCounter(config)
  96. }
  97. return config
  98. }
  99. // Description returns the configured description.
  100. func (c Float64ObservableUpDownCounterConfig) Description() string {
  101. return c.description
  102. }
  103. // Unit returns the configured unit.
  104. func (c Float64ObservableUpDownCounterConfig) Unit() string {
  105. return c.unit
  106. }
  107. // Callbacks returns the configured callbacks.
  108. func (c Float64ObservableUpDownCounterConfig) Callbacks() []Float64Callback {
  109. return c.callbacks
  110. }
  111. // Float64ObservableUpDownCounterOption applies options to a
  112. // [Float64ObservableUpDownCounterConfig]. See [Float64ObservableOption] and
  113. // [InstrumentOption] for other options that can be used as a
  114. // Float64ObservableUpDownCounterOption.
  115. type Float64ObservableUpDownCounterOption interface {
  116. applyFloat64ObservableUpDownCounter(Float64ObservableUpDownCounterConfig) Float64ObservableUpDownCounterConfig
  117. }
  118. // Float64ObservableGauge is an instrument used to asynchronously record
  119. // instantaneous float64 measurements once per collection cycle. Observations
  120. // are only made within a callback for this instrument.
  121. //
  122. // Warning: Methods may be added to this interface in minor releases. See
  123. // package documentation on API implementation for information on how to set
  124. // default behavior for unimplemented methods.
  125. type Float64ObservableGauge interface {
  126. // Users of the interface can ignore this. This embedded type is only used
  127. // by implementations of this interface. See the "API Implementations"
  128. // section of the package documentation for more information.
  129. embedded.Float64ObservableGauge
  130. Float64Observable
  131. }
  132. // Float64ObservableGaugeConfig contains options for asynchronous counter
  133. // instruments that record float64 values.
  134. type Float64ObservableGaugeConfig struct {
  135. description string
  136. unit string
  137. callbacks []Float64Callback
  138. }
  139. // NewFloat64ObservableGaugeConfig returns a new [Float64ObservableGaugeConfig]
  140. // with all opts applied.
  141. func NewFloat64ObservableGaugeConfig(opts ...Float64ObservableGaugeOption) Float64ObservableGaugeConfig {
  142. var config Float64ObservableGaugeConfig
  143. for _, o := range opts {
  144. config = o.applyFloat64ObservableGauge(config)
  145. }
  146. return config
  147. }
  148. // Description returns the configured description.
  149. func (c Float64ObservableGaugeConfig) Description() string {
  150. return c.description
  151. }
  152. // Unit returns the configured unit.
  153. func (c Float64ObservableGaugeConfig) Unit() string {
  154. return c.unit
  155. }
  156. // Callbacks returns the configured callbacks.
  157. func (c Float64ObservableGaugeConfig) Callbacks() []Float64Callback {
  158. return c.callbacks
  159. }
  160. // Float64ObservableGaugeOption applies options to a
  161. // [Float64ObservableGaugeConfig]. See [Float64ObservableOption] and
  162. // [InstrumentOption] for other options that can be used as a
  163. // Float64ObservableGaugeOption.
  164. type Float64ObservableGaugeOption interface {
  165. applyFloat64ObservableGauge(Float64ObservableGaugeConfig) Float64ObservableGaugeConfig
  166. }
  167. // Float64Observer is a recorder of float64 measurements.
  168. //
  169. // Warning: Methods may be added to this interface in minor releases. See
  170. // package documentation on API implementation for information on how to set
  171. // default behavior for unimplemented methods.
  172. type Float64Observer interface {
  173. // Users of the interface can ignore this. This embedded type is only used
  174. // by implementations of this interface. See the "API Implementations"
  175. // section of the package documentation for more information.
  176. embedded.Float64Observer
  177. // Observe records the float64 value.
  178. //
  179. // Use the WithAttributeSet (or, if performance is not a concern,
  180. // the WithAttributes) option to include measurement attributes.
  181. Observe(value float64, options ...ObserveOption)
  182. }
  183. // Float64Callback is a function registered with a Meter that makes
  184. // observations for a Float64Observable instrument it is registered with.
  185. // Calls to the Float64Observer record measurement values for the
  186. // Float64Observable.
  187. //
  188. // The function needs to complete in a finite amount of time and the deadline
  189. // of the passed context is expected to be honored.
  190. //
  191. // The function needs to make unique observations across all registered
  192. // Float64Callbacks. Meaning, it should not report measurements with the same
  193. // attributes as another Float64Callbacks also registered for the same
  194. // instrument.
  195. //
  196. // The function needs to be concurrent safe.
  197. type Float64Callback func(context.Context, Float64Observer) error
  198. // Float64ObservableOption applies options to float64 Observer instruments.
  199. type Float64ObservableOption interface {
  200. Float64ObservableCounterOption
  201. Float64ObservableUpDownCounterOption
  202. Float64ObservableGaugeOption
  203. }
  204. type float64CallbackOpt struct {
  205. cback Float64Callback
  206. }
  207. func (o float64CallbackOpt) applyFloat64ObservableCounter(cfg Float64ObservableCounterConfig) Float64ObservableCounterConfig {
  208. cfg.callbacks = append(cfg.callbacks, o.cback)
  209. return cfg
  210. }
  211. func (o float64CallbackOpt) applyFloat64ObservableUpDownCounter(cfg Float64ObservableUpDownCounterConfig) Float64ObservableUpDownCounterConfig {
  212. cfg.callbacks = append(cfg.callbacks, o.cback)
  213. return cfg
  214. }
  215. func (o float64CallbackOpt) applyFloat64ObservableGauge(cfg Float64ObservableGaugeConfig) Float64ObservableGaugeConfig {
  216. cfg.callbacks = append(cfg.callbacks, o.cback)
  217. return cfg
  218. }
  219. // WithFloat64Callback adds callback to be called for an instrument.
  220. func WithFloat64Callback(callback Float64Callback) Float64ObservableOption {
  221. return float64CallbackOpt{callback}
  222. }