| 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 |
|
| 14 |
package prometheus |
| 15 |
|
| 16 |
import ( |
| 17 |
"math" |
| 18 |
"sync/atomic" |
| 19 |
"time" |
| 20 |
|
| 21 |
dto "github.com/prometheus/client_model/go" |
| 22 |
) |
| 23 |
|
| 24 |
// Gauge is a Metric that represents a single numerical value that can |
| 25 |
// arbitrarily go up and down. |
| 26 |
// |
| 27 |
// A Gauge is typically used for measured values like temperatures or current |
| 28 |
// memory usage, but also "counts" that can go up and down, like the number of |
| 29 |
// running goroutines. |
| 30 |
// |
| 31 |
// To create Gauge instances, use NewGauge. |
| 32 |
type Gauge interface { |
| 33 |
Metric |
| 34 |
Collector |
| 35 |
|
| 36 |
// Set sets the Gauge to an arbitrary value. |
| 37 |
Set(float64) |
| 38 |
// Inc increments the Gauge by 1. Use Add to increment it by arbitrary |
| 39 |
// values. |
| 40 |
Inc() |
| 41 |
// Dec decrements the Gauge by 1. Use Sub to decrement it by arbitrary |
| 42 |
// values. |
| 43 |
Dec() |
| 44 |
// Add adds the given value to the Gauge. (The value can be negative, |
| 45 |
// resulting in a decrease of the Gauge.) |
| 46 |
Add(float64) |
| 47 |
// Sub subtracts the given value from the Gauge. (The value can be |
| 48 |
// negative, resulting in an increase of the Gauge.) |
| 49 |
Sub(float64) |
| 50 |
|
| 51 |
// SetToCurrentTime sets the Gauge to the current Unix time in seconds. |
| 52 |
SetToCurrentTime() |
| 53 |
} |
| 54 |
|
| 55 |
// GaugeOpts is an alias for Opts. See there for doc comments. |
| 56 |
type GaugeOpts Opts |
| 57 |
|
| 58 |
// GaugeVecOpts bundles the options to create a GaugeVec metric. |
| 59 |
// It is mandatory to set GaugeOpts, see there for mandatory fields. VariableLabels |
| 60 |
// is optional and can safely be left to its default value. |
| 61 |
type GaugeVecOpts struct { |
| 62 |
GaugeOpts |
| 63 |
|
| 64 |
// VariableLabels are used to partition the metric vector by the given set |
| 65 |
// of labels. Each label value will be constrained with the optional Constraint |
| 66 |
// function, if provided. |
| 67 |
VariableLabels ConstrainableLabels |
| 68 |
} |
| 69 |
|
| 70 |
// NewGauge creates a new Gauge based on the provided GaugeOpts. |
| 71 |
// |
| 72 |
// The returned implementation is optimized for a fast Set method. If you have a |
| 73 |
// choice for managing the value of a Gauge via Set vs. Inc/Dec/Add/Sub, pick |
| 74 |
// the former. For example, the Inc method of the returned Gauge is slower than |
| 75 |
// the Inc method of a Counter returned by NewCounter. This matches the typical |
| 76 |
// scenarios for Gauges and Counters, where the former tends to be Set-heavy and |
| 77 |
// the latter Inc-heavy. |
| 78 |
func NewGauge(opts GaugeOpts) Gauge { |
| 79 |
desc := NewDesc( |
| 80 |
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name), |
| 81 |
opts.Help, |
| 82 |
nil, |
| 83 |
opts.ConstLabels, |
| 84 |
) |
| 85 |
result := &gauge{desc: desc, labelPairs: desc.constLabelPairs} |
| 86 |
result.init(result) // Init self-collection. |
| 87 |
return result |
| 88 |
} |
| 89 |
|
| 90 |
type gauge struct { |
| 91 |
// valBits contains the bits of the represented float64 value. It has |
| 92 |
// to go first in the struct to guarantee alignment for atomic |
| 93 |
// operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG |
| 94 |
valBits uint64 |
| 95 |
|
| 96 |
selfCollector |
| 97 |
|
| 98 |
desc *Desc |
| 99 |
labelPairs []*dto.LabelPair |
| 100 |
} |
| 101 |
|
| 102 |
func (g *gauge) Desc() *Desc { |
| 103 |
return g.desc |
| 104 |
} |
| 105 |
|
| 106 |
func (g *gauge) Set(val float64) { |
| 107 |
atomic.StoreUint64(&g.valBits, math.Float64bits(val)) |
| 108 |
} |
| 109 |
|
| 110 |
func (g *gauge) SetToCurrentTime() { |
| 111 |
g.Set(float64(time.Now().UnixNano()) / 1e9) |
| 112 |
} |
| 113 |
|
| 114 |
func (g *gauge) Inc() { |
| 115 |
g.Add(1) |
| 116 |
} |
| 117 |
|
| 118 |
func (g *gauge) Dec() { |
| 119 |
g.Add(-1) |
| 120 |
} |
| 121 |
|
| 122 |
func (g *gauge) Add(val float64) { |
| 123 |
for { |
| 124 |
oldBits := atomic.LoadUint64(&g.valBits) |
| 125 |
newBits := math.Float64bits(math.Float64frombits(oldBits) + val) |
| 126 |
if atomic.CompareAndSwapUint64(&g.valBits, oldBits, newBits) { |
| 127 |
return |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func (g *gauge) Sub(val float64) { |
| 133 |
g.Add(val * -1) |
| 134 |
} |
| 135 |
|
| 136 |
func (g *gauge) Write(out *dto.Metric) error { |
| 137 |
val := math.Float64frombits(atomic.LoadUint64(&g.valBits)) |
| 138 |
return populateMetric(GaugeValue, val, g.labelPairs, nil, out, nil) |
| 139 |
} |
| 140 |
|
| 141 |
// GaugeVec is a Collector that bundles a set of Gauges that all share the same |
| 142 |
// Desc, but have different values for their variable labels. This is used if |
| 143 |
// you want to count the same thing partitioned by various dimensions |
| 144 |
// (e.g. number of operations queued, partitioned by user and operation |
| 145 |
// type). Create instances with NewGaugeVec. |
| 146 |
type GaugeVec struct { |
| 147 |
*MetricVec |
| 148 |
} |
| 149 |
|
| 150 |
// NewGaugeVec creates a new GaugeVec based on the provided GaugeOpts and |
| 151 |
// partitioned by the given label names. |
| 152 |
func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec { |
| 153 |
return V2.NewGaugeVec(GaugeVecOpts{ |
| 154 |
GaugeOpts: opts, |
| 155 |
VariableLabels: UnconstrainedLabels(labelNames), |
| 156 |
}) |
| 157 |
} |
| 158 |
|
| 159 |
// NewGaugeVec creates a new GaugeVec based on the provided GaugeVecOpts. |
| 160 |
func (v2) NewGaugeVec(opts GaugeVecOpts) *GaugeVec { |
| 161 |
desc := V2.NewDesc( |
| 162 |
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name), |
| 163 |
opts.Help, |
| 164 |
opts.VariableLabels, |
| 165 |
opts.ConstLabels, |
| 166 |
) |
| 167 |
return &GaugeVec{ |
| 168 |
MetricVec: NewMetricVec(desc, func(lvs ...string) Metric { |
| 169 |
if len(lvs) != len(desc.variableLabels.names) { |
| 170 |
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels.names, lvs)) |
| 171 |
} |
| 172 |
result := &gauge{desc: desc, labelPairs: MakeLabelPairs(desc, lvs)} |
| 173 |
result.init(result) // Init self-collection. |
| 174 |
return result |
| 175 |
}), |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// GetMetricWithLabelValues returns the Gauge for the given slice of label |
| 180 |
// values (same order as the variable labels in Desc). If that combination of |
| 181 |
// label values is accessed for the first time, a new Gauge is created. |
| 182 |
// |
| 183 |
// It is possible to call this method without using the returned Gauge to only |
| 184 |
// create the new Gauge but leave it at its starting value 0. See also the |
| 185 |
// SummaryVec example. |
| 186 |
// |
| 187 |
// Keeping the Gauge for later use is possible (and should be considered if |
| 188 |
// performance is critical), but keep in mind that Reset, DeleteLabelValues and |
| 189 |
// Delete can be used to delete the Gauge from the GaugeVec. In that case, the |
| 190 |
// Gauge will still exist, but it will not be exported anymore, even if a |
| 191 |
// Gauge with the same label values is created later. See also the CounterVec |
| 192 |
// example. |
| 193 |
// |
| 194 |
// An error is returned if the number of label values is not the same as the |
| 195 |
// number of variable labels in Desc (minus any curried labels). |
| 196 |
// |
| 197 |
// Note that for more than one label value, this method is prone to mistakes |
| 198 |
// caused by an incorrect order of arguments. Consider GetMetricWith(Labels) as |
| 199 |
// an alternative to avoid that type of mistake. For higher label numbers, the |
| 200 |
// latter has a much more readable (albeit more verbose) syntax, but it comes |
| 201 |
// with a performance overhead (for creating and processing the Labels map). |
| 202 |
func (v *GaugeVec) GetMetricWithLabelValues(lvs ...string) (Gauge, error) { |
| 203 |
metric, err := v.MetricVec.GetMetricWithLabelValues(lvs...) |
| 204 |
if metric != nil { |
| 205 |
return metric.(Gauge), err |
| 206 |
} |
| 207 |
return nil, err |
| 208 |
} |
| 209 |
|
| 210 |
// GetMetricWith returns the Gauge for the given Labels map (the label names |
| 211 |
// must match those of the variable labels in Desc). If that label map is |
| 212 |
// accessed for the first time, a new Gauge is created. Implications of |
| 213 |
// creating a Gauge without using it and keeping the Gauge for later use are |
| 214 |
// the same as for GetMetricWithLabelValues. |
| 215 |
// |
| 216 |
// An error is returned if the number and names of the Labels are inconsistent |
| 217 |
// with those of the variable labels in Desc (minus any curried labels). |
| 218 |
// |
| 219 |
// This method is used for the same purpose as |
| 220 |
// GetMetricWithLabelValues(...string). See there for pros and cons of the two |
| 221 |
// methods. |
| 222 |
func (v *GaugeVec) GetMetricWith(labels Labels) (Gauge, error) { |
| 223 |
metric, err := v.MetricVec.GetMetricWith(labels) |
| 224 |
if metric != nil { |
| 225 |
return metric.(Gauge), err |
| 226 |
} |
| 227 |
return nil, err |
| 228 |
} |
| 229 |
|
| 230 |
// WithLabelValues works as GetMetricWithLabelValues, but panics where |
| 231 |
// GetMetricWithLabelValues would have returned an error. Not returning an |
| 232 |
// error allows shortcuts like |
| 233 |
// |
| 234 |
// myVec.WithLabelValues("404", "GET").Add(42) |
| 235 |
func (v *GaugeVec) WithLabelValues(lvs ...string) Gauge { |
| 236 |
g, err := v.GetMetricWithLabelValues(lvs...) |
| 237 |
if err != nil { |
| 238 |
panic(err) |
| 239 |
} |
| 240 |
return g |
| 241 |
} |
| 242 |
|
| 243 |
// With works as GetMetricWith, but panics where GetMetricWithLabels would have |
| 244 |
// returned an error. Not returning an error allows shortcuts like |
| 245 |
// |
| 246 |
// myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Add(42) |
| 247 |
func (v *GaugeVec) With(labels Labels) Gauge { |
| 248 |
g, err := v.GetMetricWith(labels) |
| 249 |
if err != nil { |
| 250 |
panic(err) |
| 251 |
} |
| 252 |
return g |
| 253 |
} |
| 254 |
|
| 255 |
// CurryWith returns a vector curried with the provided labels, i.e. the |
| 256 |
// returned vector has those labels pre-set for all labeled operations performed |
| 257 |
// on it. The cardinality of the curried vector is reduced accordingly. The |
| 258 |
// order of the remaining labels stays the same (just with the curried labels |
| 259 |
// taken out of the sequence – which is relevant for the |
| 260 |
// (GetMetric)WithLabelValues methods). It is possible to curry a curried |
| 261 |
// vector, but only with labels not yet used for currying before. |
| 262 |
// |
| 263 |
// The metrics contained in the GaugeVec are shared between the curried and |
| 264 |
// uncurried vectors. They are just accessed differently. Curried and uncurried |
| 265 |
// vectors behave identically in terms of collection. Only one must be |
| 266 |
// registered with a given registry (usually the uncurried version). The Reset |
| 267 |
// method deletes all metrics, even if called on a curried vector. |
| 268 |
func (v *GaugeVec) CurryWith(labels Labels) (*GaugeVec, error) { |
| 269 |
vec, err := v.MetricVec.CurryWith(labels) |
| 270 |
if vec != nil { |
| 271 |
return &GaugeVec{vec}, err |
| 272 |
} |
| 273 |
return nil, err |
| 274 |
} |
| 275 |
|
| 276 |
// MustCurryWith works as CurryWith but panics where CurryWith would have |
| 277 |
// returned an error. |
| 278 |
func (v *GaugeVec) MustCurryWith(labels Labels) *GaugeVec { |
| 279 |
vec, err := v.CurryWith(labels) |
| 280 |
if err != nil { |
| 281 |
panic(err) |
| 282 |
} |
| 283 |
return vec |
| 284 |
} |
| 285 |
|
| 286 |
// GaugeFunc is a Gauge whose value is determined at collect time by calling a |
| 287 |
// provided function. |
| 288 |
// |
| 289 |
// To create GaugeFunc instances, use NewGaugeFunc. |
| 290 |
type GaugeFunc interface { |
| 291 |
Metric |
| 292 |
Collector |
| 293 |
} |
| 294 |
|
| 295 |
// NewGaugeFunc creates a new GaugeFunc based on the provided GaugeOpts. The |
| 296 |
// value reported is determined by calling the given function from within the |
| 297 |
// Write method. Take into account that metric collection may happen |
| 298 |
// concurrently. Therefore, it must be safe to call the provided function |
| 299 |
// concurrently. |
| 300 |
// |
| 301 |
// NewGaugeFunc is a good way to create an “info” style metric with a constant |
| 302 |
// value of 1. Example: |
| 303 |
// https://github.com/prometheus/common/blob/8558a5b7db3c84fa38b4766966059a7bd5bfa2ee/version/info.go#L36-L56 |
| 304 |
func NewGaugeFunc(opts GaugeOpts, function func() float64) GaugeFunc { |
| 305 |
return newValueFunc(NewDesc( |
| 306 |
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name), |
| 307 |
opts.Help, |
| 308 |
nil, |
| 309 |
opts.ConstLabels, |
| 310 |
), GaugeValue, function) |
| 311 |
} |