asyncint64.go 9.1 KB

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