meter.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. // MeterProvider provides access to named Meter instances, for instrumenting
  9. // an application or package.
  10. //
  11. // Warning: Methods may be added to this interface in minor releases. See
  12. // package documentation on API implementation for information on how to set
  13. // default behavior for unimplemented methods.
  14. type MeterProvider interface {
  15. // Users of the interface can ignore this. This embedded type is only used
  16. // by implementations of this interface. See the "API Implementations"
  17. // section of the package documentation for more information.
  18. embedded.MeterProvider
  19. // Meter returns a new Meter with the provided name and configuration.
  20. //
  21. // A Meter should be scoped at most to a single package. The name needs to
  22. // be unique so it does not collide with other names used by
  23. // an application, nor other applications. To achieve this, the import path
  24. // of the instrumentation package is recommended to be used as name.
  25. //
  26. // If the name is empty, then an implementation defined default name will
  27. // be used instead.
  28. //
  29. // Implementations of this method need to be safe for a user to call
  30. // concurrently.
  31. Meter(name string, opts ...MeterOption) Meter
  32. }
  33. // Meter provides access to instrument instances for recording metrics.
  34. //
  35. // Warning: Methods may be added to this interface in minor releases. See
  36. // package documentation on API implementation for information on how to set
  37. // default behavior for unimplemented methods.
  38. type Meter interface {
  39. // Users of the interface can ignore this. This embedded type is only used
  40. // by implementations of this interface. See the "API Implementations"
  41. // section of the package documentation for more information.
  42. embedded.Meter
  43. // Int64Counter returns a new Int64Counter instrument identified by name
  44. // and configured with options. The instrument is used to synchronously
  45. // record increasing int64 measurements during a computational operation.
  46. //
  47. // The name needs to conform to the OpenTelemetry instrument name syntax.
  48. // See the Instrument Name section of the package documentation for more
  49. // information.
  50. //
  51. // Implementations of this method need to be safe for a user to call
  52. // concurrently.
  53. Int64Counter(name string, options ...Int64CounterOption) (Int64Counter, error)
  54. // Int64UpDownCounter returns a new Int64UpDownCounter instrument
  55. // identified by name and configured with options. The instrument is used
  56. // to synchronously record int64 measurements during a computational
  57. // operation.
  58. //
  59. // The name needs to conform to the OpenTelemetry instrument name syntax.
  60. // See the Instrument Name section of the package documentation for more
  61. // information.
  62. //
  63. // Implementations of this method need to be safe for a user to call
  64. // concurrently.
  65. Int64UpDownCounter(name string, options ...Int64UpDownCounterOption) (Int64UpDownCounter, error)
  66. // Int64Histogram returns a new Int64Histogram instrument identified by
  67. // name and configured with options. The instrument is used to
  68. // synchronously record the distribution of int64 measurements during a
  69. // computational operation.
  70. //
  71. // The name needs to conform to the OpenTelemetry instrument name syntax.
  72. // See the Instrument Name section of the package documentation for more
  73. // information.
  74. //
  75. // Implementations of this method need to be safe for a user to call
  76. // concurrently.
  77. Int64Histogram(name string, options ...Int64HistogramOption) (Int64Histogram, error)
  78. // Int64Gauge returns a new Int64Gauge instrument identified by name and
  79. // configured with options. The instrument is used to synchronously record
  80. // instantaneous int64 measurements during a computational operation.
  81. //
  82. // The name needs to conform to the OpenTelemetry instrument name syntax.
  83. // See the Instrument Name section of the package documentation for more
  84. // information.
  85. //
  86. // Implementations of this method need to be safe for a user to call
  87. // concurrently.
  88. Int64Gauge(name string, options ...Int64GaugeOption) (Int64Gauge, error)
  89. // Int64ObservableCounter returns a new Int64ObservableCounter identified
  90. // by name and configured with options. The instrument is used to
  91. // asynchronously record increasing int64 measurements once per a
  92. // measurement collection cycle.
  93. //
  94. // Measurements for the returned instrument are made via a callback. Use
  95. // the WithInt64Callback option to register the callback here, or use the
  96. // RegisterCallback method of this Meter to register one later. See the
  97. // Measurements section of the package documentation for more information.
  98. //
  99. // The name needs to conform to the OpenTelemetry instrument name syntax.
  100. // See the Instrument Name section of the package documentation for more
  101. // information.
  102. //
  103. // Implementations of this method need to be safe for a user to call
  104. // concurrently.
  105. Int64ObservableCounter(name string, options ...Int64ObservableCounterOption) (Int64ObservableCounter, error)
  106. // Int64ObservableUpDownCounter returns a new Int64ObservableUpDownCounter
  107. // instrument identified by name and configured with options. The
  108. // instrument is used to asynchronously record int64 measurements once per
  109. // a measurement collection cycle.
  110. //
  111. // Measurements for the returned instrument are made via a callback. Use
  112. // the WithInt64Callback option to register the callback here, or use the
  113. // RegisterCallback method of this Meter to register one later. See the
  114. // Measurements section of the package documentation for more information.
  115. //
  116. // The name needs to conform to the OpenTelemetry instrument name syntax.
  117. // See the Instrument Name section of the package documentation for more
  118. // information.
  119. //
  120. // Implementations of this method need to be safe for a user to call
  121. // concurrently.
  122. Int64ObservableUpDownCounter(
  123. name string,
  124. options ...Int64ObservableUpDownCounterOption,
  125. ) (Int64ObservableUpDownCounter, error)
  126. // Int64ObservableGauge returns a new Int64ObservableGauge instrument
  127. // identified by name and configured with options. The instrument is used
  128. // to asynchronously record instantaneous int64 measurements once per a
  129. // measurement collection cycle.
  130. //
  131. // Measurements for the returned instrument are made via a callback. Use
  132. // the WithInt64Callback option to register the callback here, or use the
  133. // RegisterCallback method of this Meter to register one later. See the
  134. // Measurements section of the package documentation for more information.
  135. //
  136. // The name needs to conform to the OpenTelemetry instrument name syntax.
  137. // See the Instrument Name section of the package documentation for more
  138. // information.
  139. //
  140. // Implementations of this method need to be safe for a user to call
  141. // concurrently.
  142. Int64ObservableGauge(name string, options ...Int64ObservableGaugeOption) (Int64ObservableGauge, error)
  143. // Float64Counter returns a new Float64Counter instrument identified by
  144. // name and configured with options. The instrument is used to
  145. // synchronously record increasing float64 measurements during a
  146. // computational operation.
  147. //
  148. // The name needs to conform to the OpenTelemetry instrument name syntax.
  149. // See the Instrument Name section of the package documentation for more
  150. // information.
  151. Float64Counter(name string, options ...Float64CounterOption) (Float64Counter, error)
  152. // Float64UpDownCounter returns a new Float64UpDownCounter instrument
  153. // identified by name and configured with options. The instrument is used
  154. // to synchronously record float64 measurements during a computational
  155. // operation.
  156. //
  157. // The name needs to conform to the OpenTelemetry instrument name syntax.
  158. // See the Instrument Name section of the package documentation for more
  159. // information.
  160. //
  161. // Implementations of this method need to be safe for a user to call
  162. // concurrently.
  163. Float64UpDownCounter(name string, options ...Float64UpDownCounterOption) (Float64UpDownCounter, error)
  164. // Float64Histogram returns a new Float64Histogram instrument identified by
  165. // name and configured with options. The instrument is used to
  166. // synchronously record the distribution of float64 measurements during a
  167. // computational operation.
  168. //
  169. // The name needs to conform to the OpenTelemetry instrument name syntax.
  170. // See the Instrument Name section of the package documentation for more
  171. // information.
  172. //
  173. // Implementations of this method need to be safe for a user to call
  174. // concurrently.
  175. Float64Histogram(name string, options ...Float64HistogramOption) (Float64Histogram, error)
  176. // Float64Gauge returns a new Float64Gauge instrument identified by name and
  177. // configured with options. The instrument is used to synchronously record
  178. // instantaneous float64 measurements during a computational operation.
  179. //
  180. // The name needs to conform to the OpenTelemetry instrument name syntax.
  181. // See the Instrument Name section of the package documentation for more
  182. // information.
  183. //
  184. // Implementations of this method need to be safe for a user to call
  185. // concurrently.
  186. Float64Gauge(name string, options ...Float64GaugeOption) (Float64Gauge, error)
  187. // Float64ObservableCounter returns a new Float64ObservableCounter
  188. // instrument identified by name and configured with options. The
  189. // instrument is used to asynchronously record increasing float64
  190. // measurements once per a measurement collection cycle.
  191. //
  192. // Measurements for the returned instrument are made via a callback. Use
  193. // the WithFloat64Callback option to register the callback here, or use the
  194. // RegisterCallback method of this Meter to register one later. See the
  195. // Measurements section of the package documentation for more information.
  196. //
  197. // The name needs to conform to the OpenTelemetry instrument name syntax.
  198. // See the Instrument Name section of the package documentation for more
  199. // information.
  200. //
  201. // Implementations of this method need to be safe for a user to call
  202. // concurrently.
  203. Float64ObservableCounter(name string, options ...Float64ObservableCounterOption) (Float64ObservableCounter, error)
  204. // Float64ObservableUpDownCounter returns a new
  205. // Float64ObservableUpDownCounter instrument identified by name and
  206. // configured with options. The instrument is used to asynchronously record
  207. // float64 measurements once per a measurement collection cycle.
  208. //
  209. // Measurements for the returned instrument are made via a callback. Use
  210. // the WithFloat64Callback option to register the callback here, or use the
  211. // RegisterCallback method of this Meter to register one later. See the
  212. // Measurements section of the package documentation for more information.
  213. //
  214. // The name needs to conform to the OpenTelemetry instrument name syntax.
  215. // See the Instrument Name section of the package documentation for more
  216. // information.
  217. //
  218. // Implementations of this method need to be safe for a user to call
  219. // concurrently.
  220. Float64ObservableUpDownCounter(
  221. name string,
  222. options ...Float64ObservableUpDownCounterOption,
  223. ) (Float64ObservableUpDownCounter, error)
  224. // Float64ObservableGauge returns a new Float64ObservableGauge instrument
  225. // identified by name and configured with options. The instrument is used
  226. // to asynchronously record instantaneous float64 measurements once per a
  227. // measurement collection cycle.
  228. //
  229. // Measurements for the returned instrument are made via a callback. Use
  230. // the WithFloat64Callback option to register the callback here, or use the
  231. // RegisterCallback method of this Meter to register one later. See the
  232. // Measurements section of the package documentation for more information.
  233. //
  234. // The name needs to conform to the OpenTelemetry instrument name syntax.
  235. // See the Instrument Name section of the package documentation for more
  236. // information.
  237. //
  238. // Implementations of this method need to be safe for a user to call
  239. // concurrently.
  240. Float64ObservableGauge(name string, options ...Float64ObservableGaugeOption) (Float64ObservableGauge, error)
  241. // RegisterCallback registers f to be called during the collection of a
  242. // measurement cycle.
  243. //
  244. // If Unregister of the returned Registration is called, f needs to be
  245. // unregistered and not called during collection.
  246. //
  247. // The instruments f is registered with are the only instruments that f may
  248. // observe values for.
  249. //
  250. // If no instruments are passed, f should not be registered nor called
  251. // during collection.
  252. //
  253. // Implementations of this method need to be safe for a user to call
  254. // concurrently.
  255. //
  256. // The function f needs to be concurrent safe.
  257. RegisterCallback(f Callback, instruments ...Observable) (Registration, error)
  258. }
  259. // Callback is a function registered with a Meter that makes observations for
  260. // the set of instruments it is registered with. The Observer parameter is used
  261. // to record measurement observations for these instruments.
  262. //
  263. // The function needs to complete in a finite amount of time and the deadline
  264. // of the passed context is expected to be honored.
  265. //
  266. // The function needs to make unique observations across all registered
  267. // Callbacks. Meaning, it should not report measurements for an instrument with
  268. // the same attributes as another Callback will report.
  269. //
  270. // The function needs to be reentrant and concurrent safe.
  271. //
  272. // Note that Go's mutexes are not reentrant, and locking a mutex takes
  273. // an indefinite amount of time. It is therefore advised to avoid
  274. // using mutexes inside callbacks.
  275. type Callback func(context.Context, Observer) error
  276. // Observer records measurements for multiple instruments in a Callback.
  277. //
  278. // Warning: Methods may be added to this interface in minor releases. See
  279. // package documentation on API implementation for information on how to set
  280. // default behavior for unimplemented methods.
  281. type Observer interface {
  282. // Users of the interface can ignore this. This embedded type is only used
  283. // by implementations of this interface. See the "API Implementations"
  284. // section of the package documentation for more information.
  285. embedded.Observer
  286. // ObserveFloat64 records the float64 value for obsrv.
  287. //
  288. // Implementations of this method need to be safe for a user to call
  289. // concurrently.
  290. ObserveFloat64(obsrv Float64Observable, value float64, opts ...ObserveOption)
  291. // ObserveInt64 records the int64 value for obsrv.
  292. //
  293. // Implementations of this method need to be safe for a user to call
  294. // concurrently.
  295. ObserveInt64(obsrv Int64Observable, value int64, opts ...ObserveOption)
  296. }
  297. // Registration is an token representing the unique registration of a callback
  298. // for a set of instruments with a Meter.
  299. //
  300. // Warning: Methods may be added to this interface in minor releases. See
  301. // package documentation on API implementation for information on how to set
  302. // default behavior for unimplemented methods.
  303. type Registration interface {
  304. // Users of the interface can ignore this. This embedded type is only used
  305. // by implementations of this interface. See the "API Implementations"
  306. // section of the package documentation for more information.
  307. embedded.Registration
  308. // Unregister removes the callback registration from a Meter.
  309. //
  310. // Implementations of this method need to be idempotent and safe for a user
  311. // to call concurrently.
  312. Unregister() error
  313. }