doc.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /*
  15. Package metric provides the OpenTelemetry API used to measure metrics about
  16. source code operation.
  17. This API is separate from its implementation so the instrumentation built from
  18. it is reusable. See [go.opentelemetry.io/otel/sdk/metric] for the official
  19. OpenTelemetry implementation of this API.
  20. All measurements made with this package are made via instruments. These
  21. instruments are created by a [Meter] which itself is created by a
  22. [MeterProvider]. Applications need to accept a [MeterProvider] implementation
  23. as a starting point when instrumenting. This can be done directly, or by using
  24. the OpenTelemetry global MeterProvider via [GetMeterProvider]. Using an
  25. appropriately named [Meter] from the accepted [MeterProvider], instrumentation
  26. can then be built from the [Meter]'s instruments.
  27. # Instruments
  28. Each instrument is designed to make measurements of a particular type. Broadly,
  29. all instruments fall into two overlapping logical categories: asynchronous or
  30. synchronous, and int64 or float64.
  31. All synchronous instruments ([Int64Counter], [Int64UpDownCounter],
  32. [Int64Histogram], [Float64Counter], [Float64UpDownCounter], and
  33. [Float64Histogram]) are used to measure the operation and performance of source
  34. code during the source code execution. These instruments only make measurements
  35. when the source code they instrument is run.
  36. All asynchronous instruments ([Int64ObservableCounter],
  37. [Int64ObservableUpDownCounter], [Int64ObservableGauge],
  38. [Float64ObservableCounter], [Float64ObservableUpDownCounter], and
  39. [Float64ObservableGauge]) are used to measure metrics outside of the execution
  40. of source code. They are said to make "observations" via a callback function
  41. called once every measurement collection cycle.
  42. Each instrument is also grouped by the value type it measures. Either int64 or
  43. float64. The value being measured will dictate which instrument in these
  44. categories to use.
  45. Outside of these two broad categories, instruments are described by the
  46. function they are designed to serve. All Counters ([Int64Counter],
  47. [Float64Counter], [Int64ObservableCounter], and [Float64ObservableCounter]) are
  48. designed to measure values that never decrease in value, but instead only
  49. incrementally increase in value. UpDownCounters ([Int64UpDownCounter],
  50. [Float64UpDownCounter], [Int64ObservableUpDownCounter], and
  51. [Float64ObservableUpDownCounter]) on the other hand, are designed to measure
  52. values that can increase and decrease. When more information needs to be
  53. conveyed about all the synchronous measurements made during a collection cycle,
  54. a Histogram ([Int64Histogram] and [Float64Histogram]) should be used. Finally,
  55. when just the most recent measurement needs to be conveyed about an
  56. asynchronous measurement, a Gauge ([Int64ObservableGauge] and
  57. [Float64ObservableGauge]) should be used.
  58. See the [OpenTelemetry documentation] for more information about instruments
  59. and their intended use.
  60. # Measurements
  61. Measurements are made by recording values and information about the values with
  62. an instrument. How these measurements are recorded depends on the instrument.
  63. Measurements for synchronous instruments ([Int64Counter], [Int64UpDownCounter],
  64. [Int64Histogram], [Float64Counter], [Float64UpDownCounter], and
  65. [Float64Histogram]) are recorded using the instrument methods directly. All
  66. counter instruments have an Add method that is used to measure an increment
  67. value, and all histogram instruments have a Record method to measure a data
  68. point.
  69. Asynchronous instruments ([Int64ObservableCounter],
  70. [Int64ObservableUpDownCounter], [Int64ObservableGauge],
  71. [Float64ObservableCounter], [Float64ObservableUpDownCounter], and
  72. [Float64ObservableGauge]) record measurements within a callback function. The
  73. callback is registered with the Meter which ensures the callback is called once
  74. per collection cycle. A callback can be registered two ways: during the
  75. instrument's creation using an option, or later using the RegisterCallback
  76. method of the [Meter] that created the instrument.
  77. If the following criteria are met, an option ([WithInt64Callback] or
  78. [WithFloat64Callback]) can be used during the asynchronous instrument's
  79. creation to register a callback ([Int64Callback] or [Float64Callback],
  80. respectively):
  81. - The measurement process is known when the instrument is created
  82. - Only that instrument will make a measurement within the callback
  83. - The callback never needs to be unregistered
  84. If the criteria are not met, use the RegisterCallback method of the [Meter] that
  85. created the instrument to register a [Callback].
  86. # API Implementations
  87. This package does not conform to the standard Go versioning policy, all of its
  88. interfaces may have methods added to them without a package major version bump.
  89. This non-standard API evolution could surprise an uninformed implementation
  90. author. They could unknowingly build their implementation in a way that would
  91. result in a runtime panic for their users that update to the new API.
  92. The API is designed to help inform an instrumentation author about this
  93. non-standard API evolution. It requires them to choose a default behavior for
  94. unimplemented interface methods. There are three behavior choices they can
  95. make:
  96. - Compilation failure
  97. - Panic
  98. - Default to another implementation
  99. All interfaces in this API embed a corresponding interface from
  100. [go.opentelemetry.io/otel/metric/embedded]. If an author wants the default
  101. behavior of their implementations to be a compilation failure, signaling to
  102. their users they need to update to the latest version of that implementation,
  103. they need to embed the corresponding interface from
  104. [go.opentelemetry.io/otel/metric/embedded] in their implementation. For
  105. example,
  106. import "go.opentelemetry.io/otel/metric/embedded"
  107. type MeterProvider struct {
  108. embedded.MeterProvider
  109. // ...
  110. }
  111. If an author wants the default behavior of their implementations to a panic,
  112. they need to embed the API interface directly.
  113. import "go.opentelemetry.io/otel/metric"
  114. type MeterProvider struct {
  115. metric.MeterProvider
  116. // ...
  117. }
  118. This is not a recommended behavior as it could lead to publishing packages that
  119. contain runtime panics when users update other package that use newer versions
  120. of [go.opentelemetry.io/otel/metric].
  121. Finally, an author can embed another implementation in theirs. The embedded
  122. implementation will be used for methods not defined by the author. For example,
  123. an author who want to default to silently dropping the call can use
  124. [go.opentelemetry.io/otel/metric/noop]:
  125. import "go.opentelemetry.io/otel/metric/noop"
  126. type MeterProvider struct {
  127. noop.MeterProvider
  128. // ...
  129. }
  130. It is strongly recommended that authors only embed
  131. [go.opentelemetry.io/otel/metric/noop] if they choose this default behavior.
  132. That implementation is the only one OpenTelemetry authors can guarantee will
  133. fully implement all the API interfaces when a user updates their API.
  134. [OpenTelemetry documentation]: https://opentelemetry.io/docs/concepts/signals/metrics/
  135. [GetMeterProvider]: https://pkg.go.dev/go.opentelemetry.io/otel#GetMeterProvider
  136. */
  137. package metric // import "go.opentelemetry.io/otel/metric"