gauge.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2014 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package prometheus
  14. // Gauge is a Metric that represents a single numerical value that can
  15. // arbitrarily go up and down.
  16. //
  17. // A Gauge is typically used for measured values like temperatures or current
  18. // memory usage, but also "counts" that can go up and down, like the number of
  19. // running goroutines.
  20. //
  21. // To create Gauge instances, use NewGauge.
  22. type Gauge interface {
  23. Metric
  24. Collector
  25. // Set sets the Gauge to an arbitrary value.
  26. Set(float64)
  27. // Inc increments the Gauge by 1. Use Add to increment it by arbitrary
  28. // values.
  29. Inc()
  30. // Dec decrements the Gauge by 1. Use Sub to decrement it by arbitrary
  31. // values.
  32. Dec()
  33. // Add adds the given value to the Gauge. (The value can be negative,
  34. // resulting in a decrease of the Gauge.)
  35. Add(float64)
  36. // Sub subtracts the given value from the Gauge. (The value can be
  37. // negative, resulting in an increase of the Gauge.)
  38. Sub(float64)
  39. // SetToCurrentTime sets the Gauge to the current Unix time in seconds.
  40. SetToCurrentTime()
  41. }
  42. // GaugeOpts is an alias for Opts. See there for doc comments.
  43. type GaugeOpts Opts
  44. // NewGauge creates a new Gauge based on the provided GaugeOpts.
  45. func NewGauge(opts GaugeOpts) Gauge {
  46. return newValue(NewDesc(
  47. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  48. opts.Help,
  49. nil,
  50. opts.ConstLabels,
  51. ), GaugeValue, 0)
  52. }
  53. // GaugeVec is a Collector that bundles a set of Gauges that all share the same
  54. // Desc, but have different values for their variable labels. This is used if
  55. // you want to count the same thing partitioned by various dimensions
  56. // (e.g. number of operations queued, partitioned by user and operation
  57. // type). Create instances with NewGaugeVec.
  58. type GaugeVec struct {
  59. *metricVec
  60. }
  61. // NewGaugeVec creates a new GaugeVec based on the provided GaugeOpts and
  62. // partitioned by the given label names.
  63. func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec {
  64. desc := NewDesc(
  65. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  66. opts.Help,
  67. labelNames,
  68. opts.ConstLabels,
  69. )
  70. return &GaugeVec{
  71. metricVec: newMetricVec(desc, func(lvs ...string) Metric {
  72. return newValue(desc, GaugeValue, 0, lvs...)
  73. }),
  74. }
  75. }
  76. // GetMetricWithLabelValues returns the Gauge for the given slice of label
  77. // values (same order as the VariableLabels in Desc). If that combination of
  78. // label values is accessed for the first time, a new Gauge is created.
  79. //
  80. // It is possible to call this method without using the returned Gauge to only
  81. // create the new Gauge but leave it at its starting value 0. See also the
  82. // SummaryVec example.
  83. //
  84. // Keeping the Gauge for later use is possible (and should be considered if
  85. // performance is critical), but keep in mind that Reset, DeleteLabelValues and
  86. // Delete can be used to delete the Gauge from the GaugeVec. In that case, the
  87. // Gauge will still exist, but it will not be exported anymore, even if a
  88. // Gauge with the same label values is created later. See also the CounterVec
  89. // example.
  90. //
  91. // An error is returned if the number of label values is not the same as the
  92. // number of VariableLabels in Desc.
  93. //
  94. // Note that for more than one label value, this method is prone to mistakes
  95. // caused by an incorrect order of arguments. Consider GetMetricWith(Labels) as
  96. // an alternative to avoid that type of mistake. For higher label numbers, the
  97. // latter has a much more readable (albeit more verbose) syntax, but it comes
  98. // with a performance overhead (for creating and processing the Labels map).
  99. func (m *GaugeVec) GetMetricWithLabelValues(lvs ...string) (Gauge, error) {
  100. metric, err := m.metricVec.getMetricWithLabelValues(lvs...)
  101. if metric != nil {
  102. return metric.(Gauge), err
  103. }
  104. return nil, err
  105. }
  106. // GetMetricWith returns the Gauge for the given Labels map (the label names
  107. // must match those of the VariableLabels in Desc). If that label map is
  108. // accessed for the first time, a new Gauge is created. Implications of
  109. // creating a Gauge without using it and keeping the Gauge for later use are
  110. // the same as for GetMetricWithLabelValues.
  111. //
  112. // An error is returned if the number and names of the Labels are inconsistent
  113. // with those of the VariableLabels in Desc.
  114. //
  115. // This method is used for the same purpose as
  116. // GetMetricWithLabelValues(...string). See there for pros and cons of the two
  117. // methods.
  118. func (m *GaugeVec) GetMetricWith(labels Labels) (Gauge, error) {
  119. metric, err := m.metricVec.getMetricWith(labels)
  120. if metric != nil {
  121. return metric.(Gauge), err
  122. }
  123. return nil, err
  124. }
  125. // WithLabelValues works as GetMetricWithLabelValues, but panics where
  126. // GetMetricWithLabelValues would have returned an error. By not returning an
  127. // error, WithLabelValues allows shortcuts like
  128. // myVec.WithLabelValues("404", "GET").Add(42)
  129. func (m *GaugeVec) WithLabelValues(lvs ...string) Gauge {
  130. return m.metricVec.withLabelValues(lvs...).(Gauge)
  131. }
  132. // With works as GetMetricWith, but panics where GetMetricWithLabels would have
  133. // returned an error. By not returning an error, With allows shortcuts like
  134. // myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
  135. func (m *GaugeVec) With(labels Labels) Gauge {
  136. return m.metricVec.with(labels).(Gauge)
  137. }
  138. // GaugeFunc is a Gauge whose value is determined at collect time by calling a
  139. // provided function.
  140. //
  141. // To create GaugeFunc instances, use NewGaugeFunc.
  142. type GaugeFunc interface {
  143. Metric
  144. Collector
  145. }
  146. // NewGaugeFunc creates a new GaugeFunc based on the provided GaugeOpts. The
  147. // value reported is determined by calling the given function from within the
  148. // Write method. Take into account that metric collection may happen
  149. // concurrently. If that results in concurrent calls to Write, like in the case
  150. // where a GaugeFunc is directly registered with Prometheus, the provided
  151. // function must be concurrency-safe.
  152. func NewGaugeFunc(opts GaugeOpts, function func() float64) GaugeFunc {
  153. return newValueFunc(NewDesc(
  154. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  155. opts.Help,
  156. nil,
  157. opts.ConstLabels,
  158. ), GaugeValue, function)
  159. }