asyncint64.go 9.6 KB

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