doc.go 7.7 KB

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