gauge.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.
  28. Inc()
  29. // Dec decrements the Gauge by 1.
  30. Dec()
  31. // Add adds the given value to the Gauge. (The value can be
  32. // negative, resulting in a decrease of the Gauge.)
  33. Add(float64)
  34. // Sub subtracts the given value from the Gauge. (The value can be
  35. // negative, resulting in an increase of the Gauge.)
  36. Sub(float64)
  37. }
  38. // GaugeOpts is an alias for Opts. See there for doc comments.
  39. type GaugeOpts Opts
  40. // NewGauge creates a new Gauge based on the provided GaugeOpts.
  41. func NewGauge(opts GaugeOpts) Gauge {
  42. return newValue(NewDesc(
  43. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  44. opts.Help,
  45. nil,
  46. opts.ConstLabels,
  47. ), GaugeValue, 0)
  48. }
  49. // GaugeVec is a Collector that bundles a set of Gauges that all share the same
  50. // Desc, but have different values for their variable labels. This is used if
  51. // you want to count the same thing partitioned by various dimensions
  52. // (e.g. number of operations queued, partitioned by user and operation
  53. // type). Create instances with NewGaugeVec.
  54. type GaugeVec struct {
  55. *MetricVec
  56. }
  57. // NewGaugeVec creates a new GaugeVec based on the provided GaugeOpts and
  58. // partitioned by the given label names. At least one label name must be
  59. // provided.
  60. func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec {
  61. desc := NewDesc(
  62. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  63. opts.Help,
  64. labelNames,
  65. opts.ConstLabels,
  66. )
  67. return &GaugeVec{
  68. MetricVec: newMetricVec(desc, func(lvs ...string) Metric {
  69. return newValue(desc, GaugeValue, 0, lvs...)
  70. }),
  71. }
  72. }
  73. // GetMetricWithLabelValues replaces the method of the same name in
  74. // MetricVec. The difference is that this method returns a Gauge and not a
  75. // Metric so that no type conversion is required.
  76. func (m *GaugeVec) GetMetricWithLabelValues(lvs ...string) (Gauge, error) {
  77. metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...)
  78. if metric != nil {
  79. return metric.(Gauge), err
  80. }
  81. return nil, err
  82. }
  83. // GetMetricWith replaces the method of the same name in MetricVec. The
  84. // difference is that this method returns a Gauge and not a Metric so that no
  85. // type conversion is required.
  86. func (m *GaugeVec) GetMetricWith(labels Labels) (Gauge, error) {
  87. metric, err := m.MetricVec.GetMetricWith(labels)
  88. if metric != nil {
  89. return metric.(Gauge), err
  90. }
  91. return nil, err
  92. }
  93. // WithLabelValues works as GetMetricWithLabelValues, but panics where
  94. // GetMetricWithLabelValues would have returned an error. By not returning an
  95. // error, WithLabelValues allows shortcuts like
  96. // myVec.WithLabelValues("404", "GET").Add(42)
  97. func (m *GaugeVec) WithLabelValues(lvs ...string) Gauge {
  98. return m.MetricVec.WithLabelValues(lvs...).(Gauge)
  99. }
  100. // With works as GetMetricWith, but panics where GetMetricWithLabels would have
  101. // returned an error. By not returning an error, With allows shortcuts like
  102. // myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
  103. func (m *GaugeVec) With(labels Labels) Gauge {
  104. return m.MetricVec.With(labels).(Gauge)
  105. }
  106. // GaugeFunc is a Gauge whose value is determined at collect time by calling a
  107. // provided function.
  108. //
  109. // To create GaugeFunc instances, use NewGaugeFunc.
  110. type GaugeFunc interface {
  111. Metric
  112. Collector
  113. }
  114. // NewGaugeFunc creates a new GaugeFunc based on the provided GaugeOpts. The
  115. // value reported is determined by calling the given function from within the
  116. // Write method. Take into account that metric collection may happen
  117. // concurrently. If that results in concurrent calls to Write, like in the case
  118. // where a GaugeFunc is directly registered with Prometheus, the provided
  119. // function must be concurrency-safe.
  120. func NewGaugeFunc(opts GaugeOpts, function func() float64) GaugeFunc {
  121. return newValueFunc(NewDesc(
  122. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  123. opts.Help,
  124. nil,
  125. opts.ConstLabels,
  126. ), GaugeValue, function)
  127. }