asyncint64.go 9.1 KB

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