2013-04-03 20:33:32 +04:00
|
|
|
// Copyright (c) 2013, Prometheus Team
|
2013-02-12 05:36:06 +04:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
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
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2013-02-12 05:36:06 +04:00
|
|
|
// A gauge metric merely provides an instantaneous representation of a scalar
|
|
|
|
// value or an accumulation. For instance, if one wants to expose the current
|
|
|
|
// temperature or the hitherto bandwidth used, this would be the metric for such
|
|
|
|
// circumstances.
|
2013-01-19 17:48:30 +04:00
|
|
|
type Gauge interface {
|
|
|
|
AsMarshallable() map[string]interface{}
|
|
|
|
ResetAll()
|
|
|
|
Set(labels map[string]string, value float64) float64
|
|
|
|
String() string
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-02-12 05:36:06 +04:00
|
|
|
type gaugeVector struct {
|
2013-01-19 17:48:30 +04:00
|
|
|
labels map[string]string
|
|
|
|
value float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGauge() Gauge {
|
|
|
|
return &gauge{
|
2013-02-12 05:36:06 +04:00
|
|
|
values: map[string]*gaugeVector{},
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type gauge struct {
|
|
|
|
mutex sync.RWMutex
|
2013-02-12 05:36:06 +04:00
|
|
|
values map[string]*gaugeVector
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
2013-04-01 20:41:50 +04:00
|
|
|
func (metric gauge) String() string {
|
2013-01-19 17:48:30 +04:00
|
|
|
formatString := "[Gauge %s]"
|
2012-05-20 01:59:25 +04:00
|
|
|
|
|
|
|
metric.mutex.RLock()
|
|
|
|
defer metric.mutex.RUnlock()
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
return fmt.Sprintf(formatString, metric.values)
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
func (metric *gauge) Set(labels map[string]string, value float64) float64 {
|
2012-05-20 01:59:25 +04:00
|
|
|
metric.mutex.Lock()
|
|
|
|
defer metric.mutex.Unlock()
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
if labels == nil {
|
|
|
|
labels = map[string]string{}
|
|
|
|
}
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
signature := labelsToSignature(labels)
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
if original, ok := metric.values[signature]; ok {
|
|
|
|
original.value = value
|
|
|
|
} else {
|
2013-02-12 05:36:06 +04:00
|
|
|
metric.values[signature] = &gaugeVector{
|
2013-01-19 17:48:30 +04:00
|
|
|
labels: labels,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
func (metric *gauge) ResetAll() {
|
|
|
|
metric.mutex.Lock()
|
|
|
|
defer metric.mutex.Unlock()
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
for key, value := range metric.values {
|
|
|
|
for label := range value.labels {
|
|
|
|
delete(value.labels, label)
|
|
|
|
}
|
|
|
|
delete(metric.values, key)
|
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-04-01 20:41:50 +04:00
|
|
|
func (metric gauge) AsMarshallable() map[string]interface{} {
|
2012-05-20 01:59:25 +04:00
|
|
|
metric.mutex.RLock()
|
|
|
|
defer metric.mutex.RUnlock()
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
values := make([]map[string]interface{}, 0, len(metric.values))
|
|
|
|
for _, value := range metric.values {
|
|
|
|
values = append(values, map[string]interface{}{
|
|
|
|
labelsKey: value.labels,
|
|
|
|
valueKey: value.value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]interface{}{
|
|
|
|
typeKey: gaugeTypeValue,
|
|
|
|
valueKey: values,
|
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|