2015-02-02 17:14:36 +03:00
|
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-05-07 22:08:33 +04:00
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
2013-02-12 05:36:06 +04:00
|
|
|
|
//
|
2014-05-07 22:08:33 +04:00
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
2012-05-20 01:59:25 +04:00
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
|
package prometheus
|
2012-05-20 01:59:25 +04:00
|
|
|
|
|
2018-01-19 18:21:07 +03:00
|
|
|
|
import (
|
|
|
|
|
"math"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
|
)
|
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
|
// Gauge is a Metric that represents a single numerical value that can
|
|
|
|
|
// arbitrarily go up and down.
|
|
|
|
|
//
|
|
|
|
|
// A Gauge is typically used for measured values like temperatures or current
|
|
|
|
|
// memory usage, but also "counts" that can go up and down, like the number of
|
|
|
|
|
// running goroutines.
|
|
|
|
|
//
|
|
|
|
|
// To create Gauge instances, use NewGauge.
|
2013-01-19 17:48:30 +04:00
|
|
|
|
type Gauge interface {
|
2013-04-19 15:22:59 +04:00
|
|
|
|
Metric
|
2014-05-07 22:08:33 +04:00
|
|
|
|
Collector
|
|
|
|
|
|
|
|
|
|
// Set sets the Gauge to an arbitrary value.
|
|
|
|
|
Set(float64)
|
2016-11-18 18:29:59 +03:00
|
|
|
|
// Inc increments the Gauge by 1. Use Add to increment it by arbitrary
|
|
|
|
|
// values.
|
2014-05-07 22:08:33 +04:00
|
|
|
|
Inc()
|
2016-11-18 18:29:59 +03:00
|
|
|
|
// Dec decrements the Gauge by 1. Use Sub to decrement it by arbitrary
|
|
|
|
|
// values.
|
2014-05-07 22:08:33 +04:00
|
|
|
|
Dec()
|
2016-11-18 18:29:59 +03:00
|
|
|
|
// Add adds the given value to the Gauge. (The value can be negative,
|
|
|
|
|
// resulting in a decrease of the Gauge.)
|
2014-05-07 22:08:33 +04:00
|
|
|
|
Add(float64)
|
|
|
|
|
// Sub subtracts the given value from the Gauge. (The value can be
|
|
|
|
|
// negative, resulting in an increase of the Gauge.)
|
|
|
|
|
Sub(float64)
|
2016-11-18 18:29:59 +03:00
|
|
|
|
|
|
|
|
|
// SetToCurrentTime sets the Gauge to the current Unix time in seconds.
|
|
|
|
|
SetToCurrentTime()
|
2013-01-19 17:48:30 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
|
// GaugeOpts is an alias for Opts. See there for doc comments.
|
|
|
|
|
type GaugeOpts Opts
|
|
|
|
|
|
|
|
|
|
// NewGauge creates a new Gauge based on the provided GaugeOpts.
|
2018-01-19 18:21:07 +03:00
|
|
|
|
//
|
|
|
|
|
// The returned implementation is optimized for a fast Set method. If you have a
|
|
|
|
|
// choice for managing the value of a Gauge via Set vs. Inc/Dec/Add/Sub, pick
|
|
|
|
|
// the former. For example, the Inc method of the returned Gauge is slower than
|
|
|
|
|
// the Inc method of a Counter returned by NewCounter. This matches the typical
|
|
|
|
|
// scenarios for Gauges and Counters, where the former tends to be Set-heavy and
|
|
|
|
|
// the latter Inc-heavy.
|
2014-05-07 22:08:33 +04:00
|
|
|
|
func NewGauge(opts GaugeOpts) Gauge {
|
2018-01-19 18:21:07 +03:00
|
|
|
|
desc := NewDesc(
|
2014-05-07 22:08:33 +04:00
|
|
|
|
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
|
|
|
|
|
opts.Help,
|
|
|
|
|
nil,
|
|
|
|
|
opts.ConstLabels,
|
2018-01-19 18:21:07 +03:00
|
|
|
|
)
|
|
|
|
|
result := &gauge{desc: desc, labelPairs: desc.constLabelPairs}
|
|
|
|
|
result.init(result) // Init self-collection.
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type gauge struct {
|
|
|
|
|
// valBits contains the bits of the represented float64 value. It has
|
|
|
|
|
// to go first in the struct to guarantee alignment for atomic
|
|
|
|
|
// operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG
|
|
|
|
|
valBits uint64
|
|
|
|
|
|
|
|
|
|
selfCollector
|
|
|
|
|
|
|
|
|
|
desc *Desc
|
|
|
|
|
labelPairs []*dto.LabelPair
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *gauge) Desc() *Desc {
|
|
|
|
|
return g.desc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *gauge) Set(val float64) {
|
|
|
|
|
atomic.StoreUint64(&g.valBits, math.Float64bits(val))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *gauge) SetToCurrentTime() {
|
|
|
|
|
g.Set(float64(time.Now().UnixNano()) / 1e9)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *gauge) Inc() {
|
|
|
|
|
g.Add(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *gauge) Dec() {
|
|
|
|
|
g.Add(-1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *gauge) Add(val float64) {
|
|
|
|
|
for {
|
|
|
|
|
oldBits := atomic.LoadUint64(&g.valBits)
|
|
|
|
|
newBits := math.Float64bits(math.Float64frombits(oldBits) + val)
|
|
|
|
|
if atomic.CompareAndSwapUint64(&g.valBits, oldBits, newBits) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *gauge) Sub(val float64) {
|
|
|
|
|
g.Add(val * -1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *gauge) Write(out *dto.Metric) error {
|
|
|
|
|
val := math.Float64frombits(atomic.LoadUint64(&g.valBits))
|
2020-01-14 21:22:19 +03:00
|
|
|
|
return populateMetric(GaugeValue, val, g.labelPairs, nil, out)
|
2013-01-19 17:48:30 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
|
// GaugeVec is a Collector that bundles a set of Gauges that all share the same
|
|
|
|
|
// Desc, but have different values for their variable labels. This is used if
|
|
|
|
|
// you want to count the same thing partitioned by various dimensions
|
|
|
|
|
// (e.g. number of operations queued, partitioned by user and operation
|
|
|
|
|
// type). Create instances with NewGaugeVec.
|
|
|
|
|
type GaugeVec struct {
|
2020-09-10 19:05:44 +03:00
|
|
|
|
*MetricVec
|
2012-05-20 01:59:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
|
// NewGaugeVec creates a new GaugeVec based on the provided GaugeOpts and
|
2017-06-28 18:55:59 +03:00
|
|
|
|
// partitioned by the given label names.
|
2014-05-07 22:08:33 +04:00
|
|
|
|
func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec {
|
|
|
|
|
desc := NewDesc(
|
|
|
|
|
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
|
|
|
|
|
opts.Help,
|
|
|
|
|
labelNames,
|
|
|
|
|
opts.ConstLabels,
|
|
|
|
|
)
|
|
|
|
|
return &GaugeVec{
|
2020-09-10 19:05:44 +03:00
|
|
|
|
MetricVec: NewMetricVec(desc, func(lvs ...string) Metric {
|
2018-01-19 18:21:07 +03:00
|
|
|
|
if len(lvs) != len(desc.variableLabels) {
|
2018-11-02 19:01:14 +03:00
|
|
|
|
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels, lvs))
|
2018-01-19 18:21:07 +03:00
|
|
|
|
}
|
2020-09-10 19:05:44 +03:00
|
|
|
|
result := &gauge{desc: desc, labelPairs: MakeLabelPairs(desc, lvs)}
|
2018-01-19 18:21:07 +03:00
|
|
|
|
result.init(result) // Init self-collection.
|
|
|
|
|
return result
|
2016-08-11 06:03:15 +03:00
|
|
|
|
}),
|
2013-01-19 17:48:30 +04:00
|
|
|
|
}
|
2014-02-19 17:53:34 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
|
// GetMetricWithLabelValues returns the Gauge for the given slice of label
|
2020-09-10 19:05:44 +03:00
|
|
|
|
// values (same order as the variable labels in Desc). If that combination of
|
2017-06-28 18:55:59 +03:00
|
|
|
|
// label values is accessed for the first time, a new Gauge is created.
|
|
|
|
|
//
|
|
|
|
|
// It is possible to call this method without using the returned Gauge to only
|
|
|
|
|
// create the new Gauge but leave it at its starting value 0. See also the
|
|
|
|
|
// SummaryVec example.
|
|
|
|
|
//
|
|
|
|
|
// Keeping the Gauge for later use is possible (and should be considered if
|
|
|
|
|
// performance is critical), but keep in mind that Reset, DeleteLabelValues and
|
|
|
|
|
// Delete can be used to delete the Gauge from the GaugeVec. In that case, the
|
|
|
|
|
// Gauge will still exist, but it will not be exported anymore, even if a
|
|
|
|
|
// Gauge with the same label values is created later. See also the CounterVec
|
|
|
|
|
// example.
|
|
|
|
|
//
|
|
|
|
|
// An error is returned if the number of label values is not the same as the
|
2020-09-10 19:05:44 +03:00
|
|
|
|
// number of variable labels in Desc (minus any curried labels).
|
2017-06-28 18:55:59 +03:00
|
|
|
|
//
|
|
|
|
|
// Note that for more than one label value, this method is prone to mistakes
|
|
|
|
|
// caused by an incorrect order of arguments. Consider GetMetricWith(Labels) as
|
|
|
|
|
// an alternative to avoid that type of mistake. For higher label numbers, the
|
|
|
|
|
// latter has a much more readable (albeit more verbose) syntax, but it comes
|
|
|
|
|
// with a performance overhead (for creating and processing the Labels map).
|
2017-08-29 15:51:49 +03:00
|
|
|
|
func (v *GaugeVec) GetMetricWithLabelValues(lvs ...string) (Gauge, error) {
|
2020-09-10 19:05:44 +03:00
|
|
|
|
metric, err := v.MetricVec.GetMetricWithLabelValues(lvs...)
|
2014-05-07 22:08:33 +04:00
|
|
|
|
if metric != nil {
|
|
|
|
|
return metric.(Gauge), err
|
2013-01-19 17:48:30 +04:00
|
|
|
|
}
|
2014-05-07 22:08:33 +04:00
|
|
|
|
return nil, err
|
2012-05-20 01:59:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
|
// GetMetricWith returns the Gauge for the given Labels map (the label names
|
2020-09-10 19:05:44 +03:00
|
|
|
|
// must match those of the variable labels in Desc). If that label map is
|
2017-06-28 18:55:59 +03:00
|
|
|
|
// accessed for the first time, a new Gauge is created. Implications of
|
|
|
|
|
// creating a Gauge without using it and keeping the Gauge for later use are
|
|
|
|
|
// the same as for GetMetricWithLabelValues.
|
|
|
|
|
//
|
|
|
|
|
// An error is returned if the number and names of the Labels are inconsistent
|
2020-09-10 19:05:44 +03:00
|
|
|
|
// with those of the variable labels in Desc (minus any curried labels).
|
2017-06-28 18:55:59 +03:00
|
|
|
|
//
|
|
|
|
|
// This method is used for the same purpose as
|
|
|
|
|
// GetMetricWithLabelValues(...string). See there for pros and cons of the two
|
|
|
|
|
// methods.
|
2017-08-29 15:51:49 +03:00
|
|
|
|
func (v *GaugeVec) GetMetricWith(labels Labels) (Gauge, error) {
|
2020-09-10 19:05:44 +03:00
|
|
|
|
metric, err := v.MetricVec.GetMetricWith(labels)
|
2014-05-07 22:08:33 +04:00
|
|
|
|
if metric != nil {
|
|
|
|
|
return metric.(Gauge), err
|
2013-01-19 17:48:30 +04:00
|
|
|
|
}
|
2014-05-07 22:08:33 +04:00
|
|
|
|
return nil, err
|
2012-05-20 01:59:25 +04:00
|
|
|
|
}
|
2013-06-27 20:46:16 +04:00
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
|
// WithLabelValues works as GetMetricWithLabelValues, but panics where
|
2017-08-30 02:05:29 +03:00
|
|
|
|
// GetMetricWithLabelValues would have returned an error. Not returning an
|
|
|
|
|
// error allows shortcuts like
|
2014-05-07 22:08:33 +04:00
|
|
|
|
// myVec.WithLabelValues("404", "GET").Add(42)
|
2017-08-29 15:51:49 +03:00
|
|
|
|
func (v *GaugeVec) WithLabelValues(lvs ...string) Gauge {
|
|
|
|
|
g, err := v.GetMetricWithLabelValues(lvs...)
|
2017-08-29 15:43:37 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return g
|
2014-05-07 22:08:33 +04:00
|
|
|
|
}
|
2013-06-27 20:46:16 +04:00
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
|
// With works as GetMetricWith, but panics where GetMetricWithLabels would have
|
2017-08-30 02:05:29 +03:00
|
|
|
|
// returned an error. Not returning an error allows shortcuts like
|
|
|
|
|
// myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Add(42)
|
2017-08-29 15:51:49 +03:00
|
|
|
|
func (v *GaugeVec) With(labels Labels) Gauge {
|
|
|
|
|
g, err := v.GetMetricWith(labels)
|
2017-08-29 15:43:37 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return g
|
2013-06-27 20:46:16 +04:00
|
|
|
|
}
|
2014-06-23 16:15:35 +04:00
|
|
|
|
|
2017-08-30 02:05:29 +03:00
|
|
|
|
// CurryWith returns a vector curried with the provided labels, i.e. the
|
|
|
|
|
// returned vector has those labels pre-set for all labeled operations performed
|
|
|
|
|
// on it. The cardinality of the curried vector is reduced accordingly. The
|
|
|
|
|
// order of the remaining labels stays the same (just with the curried labels
|
|
|
|
|
// taken out of the sequence – which is relevant for the
|
|
|
|
|
// (GetMetric)WithLabelValues methods). It is possible to curry a curried
|
|
|
|
|
// vector, but only with labels not yet used for currying before.
|
|
|
|
|
//
|
|
|
|
|
// The metrics contained in the GaugeVec are shared between the curried and
|
|
|
|
|
// uncurried vectors. They are just accessed differently. Curried and uncurried
|
|
|
|
|
// vectors behave identically in terms of collection. Only one must be
|
|
|
|
|
// registered with a given registry (usually the uncurried version). The Reset
|
|
|
|
|
// method deletes all metrics, even if called on a curried vector.
|
|
|
|
|
func (v *GaugeVec) CurryWith(labels Labels) (*GaugeVec, error) {
|
2020-09-10 19:05:44 +03:00
|
|
|
|
vec, err := v.MetricVec.CurryWith(labels)
|
2017-08-30 02:05:29 +03:00
|
|
|
|
if vec != nil {
|
|
|
|
|
return &GaugeVec{vec}, err
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MustCurryWith works as CurryWith but panics where CurryWith would have
|
|
|
|
|
// returned an error.
|
|
|
|
|
func (v *GaugeVec) MustCurryWith(labels Labels) *GaugeVec {
|
|
|
|
|
vec, err := v.CurryWith(labels)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return vec
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-23 16:15:35 +04:00
|
|
|
|
// GaugeFunc is a Gauge whose value is determined at collect time by calling a
|
|
|
|
|
// provided function.
|
|
|
|
|
//
|
|
|
|
|
// To create GaugeFunc instances, use NewGaugeFunc.
|
|
|
|
|
type GaugeFunc interface {
|
|
|
|
|
Metric
|
|
|
|
|
Collector
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewGaugeFunc creates a new GaugeFunc based on the provided GaugeOpts. The
|
|
|
|
|
// value reported is determined by calling the given function from within the
|
|
|
|
|
// Write method. Take into account that metric collection may happen
|
2019-12-09 20:20:27 +03:00
|
|
|
|
// concurrently. Therefore, it must be safe to call the provided function
|
|
|
|
|
// concurrently.
|
|
|
|
|
//
|
|
|
|
|
// NewGaugeFunc is a good way to create an “info” style metric with a constant
|
|
|
|
|
// value of 1. Example:
|
|
|
|
|
// https://github.com/prometheus/common/blob/8558a5b7db3c84fa38b4766966059a7bd5bfa2ee/version/info.go#L36-L56
|
2014-06-23 16:15:35 +04:00
|
|
|
|
func NewGaugeFunc(opts GaugeOpts, function func() float64) GaugeFunc {
|
|
|
|
|
return newValueFunc(NewDesc(
|
|
|
|
|
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
|
|
|
|
|
opts.Help,
|
|
|
|
|
nil,
|
|
|
|
|
opts.ConstLabels,
|
|
|
|
|
), GaugeValue, function)
|
|
|
|
|
}
|