Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/loki/process/internal/metric/gauges.go
4096 views
1
package metric
2
3
import (
4
"fmt"
5
"time"
6
7
"github.com/prometheus/client_golang/prometheus"
8
"github.com/prometheus/common/model"
9
)
10
11
const (
12
GaugeSet = "set"
13
GaugeInc = "inc"
14
GaugeDec = "dec"
15
GaugeAdd = "add"
16
GaugeSub = "sub"
17
18
ErrGaugeActionRequired = "gauge action must be defined as `set`, `inc`, `dec`, `add`, or `sub`"
19
ErrGaugeInvalidAction = "action %s is not valid, action must be `set`, `inc`, `dec`, `add`, or `sub`"
20
)
21
22
// DefaultGaugeConfig sets the defaults for a Gauge.
23
var DefaultGaugeConfig = GaugeConfig{
24
MaxIdle: 5 * time.Minute,
25
}
26
27
// GaugeConfig defines a gauge metric whose value can go up or down.
28
type GaugeConfig struct {
29
// Shared fields
30
Name string `river:"name,attr"`
31
Description string `river:"description,attr,optional"`
32
Source string `river:"source,attr,optional"`
33
Prefix string `river:"prefix,attr,optional"`
34
MaxIdle time.Duration `river:"max_idle_duration,attr,optional"`
35
Value string `river:"value,attr,optional"`
36
37
// Gauge-specific fields
38
Action string `river:"action,attr"`
39
}
40
41
// UnmarshalRiver implements the unmarshaller
42
func (g *GaugeConfig) UnmarshalRiver(f func(v interface{}) error) error {
43
*g = DefaultGaugeConfig
44
type gauge GaugeConfig
45
err := f((*gauge)(g))
46
if err != nil {
47
return err
48
}
49
50
if g.MaxIdle < 1*time.Second {
51
return fmt.Errorf("max_idle_duration must be greater or equal than 1s")
52
}
53
54
if g.Source == "" {
55
g.Source = g.Name
56
}
57
58
// TODO (@tpaschalis) A better way to keep track of these?
59
if g.Action != "set" && g.Action != "inc" && g.Action != "dec" && g.Action != "add" && g.Action != "sub" {
60
return fmt.Errorf("the 'action' gauge field must be one of the following values: [set, inc, dec, add, sub]")
61
}
62
return nil
63
}
64
65
// Gauges is a vector of gauges for a log stream.
66
type Gauges struct {
67
*metricVec
68
Cfg *GaugeConfig
69
}
70
71
// NewGauges creates a new gauge vec.
72
func NewGauges(name string, config *GaugeConfig) (*Gauges, error) {
73
return &Gauges{
74
metricVec: newMetricVec(func(labels map[string]string) prometheus.Metric {
75
return &expiringGauge{prometheus.NewGauge(prometheus.GaugeOpts{
76
Help: config.Description,
77
Name: name,
78
ConstLabels: labels,
79
}),
80
0,
81
}
82
}, int64(config.MaxIdle.Seconds())),
83
Cfg: config,
84
}, nil
85
}
86
87
// With returns the gauge associated with a stream labelset.
88
func (g *Gauges) With(labels model.LabelSet) prometheus.Gauge {
89
return g.metricVec.With(labels).(prometheus.Gauge)
90
}
91
92
type expiringGauge struct {
93
prometheus.Gauge
94
lastModSec int64
95
}
96
97
// Set sets the Gauge to an arbitrary value.
98
func (g *expiringGauge) Set(val float64) {
99
g.Gauge.Set(val)
100
g.lastModSec = time.Now().Unix()
101
}
102
103
// Inc increments the Gauge by 1. Use Add to increment it by arbitrary
104
// values.
105
func (g *expiringGauge) Inc() {
106
g.Gauge.Inc()
107
g.lastModSec = time.Now().Unix()
108
}
109
110
// Dec decrements the Gauge by 1. Use Sub to decrement it by arbitrary
111
// values.
112
func (g *expiringGauge) Dec() {
113
g.Gauge.Dec()
114
g.lastModSec = time.Now().Unix()
115
}
116
117
// Add adds the given value to the Gauge. (The value can be negative,
118
// resulting in a decrease of the Gauge.)
119
func (g *expiringGauge) Add(val float64) {
120
g.Gauge.Add(val)
121
g.lastModSec = time.Now().Unix()
122
}
123
124
// Sub subtracts the given value from the Gauge. (The value can be
125
// negative, resulting in an increase of the Gauge.)
126
func (g *expiringGauge) Sub(val float64) {
127
g.Gauge.Sub(val)
128
g.lastModSec = time.Now().Unix()
129
}
130
131
// SetToCurrentTime sets the Gauge to the current Unix time in seconds.
132
func (g *expiringGauge) SetToCurrentTime() {
133
g.Gauge.SetToCurrentTime()
134
g.lastModSec = time.Now().Unix()
135
}
136
137
// HasExpired implements Expirable
138
func (g *expiringGauge) HasExpired(currentTimeSec int64, maxAgeSec int64) bool {
139
return currentTimeSec-g.lastModSec >= maxAgeSec
140
}
141
142