This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
gauge.go
134 lines (112 loc) · 4.07 KB
/
gauge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package labeled
import (
"context"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/flyteorg/flytestdlib/contextutils"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/prometheus/client_golang/prometheus"
)
// Gauge represents a gauge labeled with values from the context. See labeled.SetMetricsKeys for more information
type Gauge struct {
*prometheus.GaugeVec
prometheus.Gauge
labels []contextutils.Key
}
// Inc increments the gauge by 1. Use Add to increment by arbitrary values. The data point will be
// labeled with values from context. See labeled.SetMetricsKeys for information about how to configure that.
func (g Gauge) Inc(ctx context.Context) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, g.labels...))
if err != nil {
panic(err.Error())
}
gauge.Inc()
if g.Gauge != nil {
g.Gauge.Inc()
}
}
// Add adds the given value to the Gauge. (The value can be negative, resulting in a decrease of the Gauge.)
// The data point will be labeled with values from context. See labeled.SetMetricsKeys for information about how to configure that.
func (g Gauge) Add(ctx context.Context, v float64) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, g.labels...))
if err != nil {
panic(err.Error())
}
gauge.Add(v)
if g.Gauge != nil {
g.Gauge.Add(v)
}
}
// Set sets the Gauge to an arbitrary value.
// The data point will be labeled with values from context. See labeled.SetMetricsKeys for information about how to configure that.
func (g Gauge) Set(ctx context.Context, v float64) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, g.labels...))
if err != nil {
panic(err.Error())
}
gauge.Set(v)
if g.Gauge != nil {
g.Gauge.Set(v)
}
}
// Dec decrements the level by 1. Use Sub to decrement by arbitrary values. The data point will be
// labeled with values from context. See labeled.SetMetricsKeys for information about how to configure that.
func (g Gauge) Dec(ctx context.Context) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, g.labels...))
if err != nil {
panic(err.Error())
}
gauge.Dec()
if g.Gauge != nil {
g.Gauge.Dec()
}
}
// Sub adds the given value to the Gauge. The value can be negative, resulting in an increase of the Gauge.
// The data point will be labeled with values from context. See labeled.SetMetricsKeys for information about how to configure that.
func (g Gauge) Sub(ctx context.Context, v float64) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, g.labels...))
if err != nil {
panic(err.Error())
}
gauge.Sub(v)
if g.Gauge != nil {
g.Gauge.Sub(v)
}
}
// SetToCurrentTime sets the Gauge to the current Unix time in seconds.
func (g Gauge) SetToCurrentTime(ctx context.Context) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, metricKeys...))
if err != nil {
panic(err.Error())
}
gauge.SetToCurrentTime()
if g.Gauge != nil {
g.Gauge.SetToCurrentTime()
}
}
// NewGauge creates a new labeled gauge. Label keys must be set before instantiating. If the unlabeled option is given,
// this object will also instantiate and emit another gauge with the given name with an _unlabeled suffix.
// See labeled.SetMetricsKeys for information about how to configure that.
func NewGauge(name, description string, scope promutils.Scope, opts ...MetricOption) Gauge {
if len(metricKeys) == 0 {
panic(ErrNeverSet)
}
g := Gauge{}
name = promutils.SanitizeMetricName(name)
for _, opt := range opts {
if _, emitUnlabeledMetric := opt.(EmitUnlabeledMetricOption); emitUnlabeledMetric {
g.Gauge = scope.MustNewGauge(GetUnlabeledMetricName(name), description)
} else if additionalLabels, casted := opt.(AdditionalLabelsOption); casted {
// compute unique labels
labelSet := sets.NewString(metricStringKeys...)
labelSet.Insert(additionalLabels.Labels...)
labels := labelSet.List()
g.GaugeVec = scope.MustNewGaugeVec(name, description, labels...)
g.labels = contextutils.MetricKeysFromStrings(labels)
}
}
if g.GaugeVec == nil {
g.GaugeVec = scope.MustNewGaugeVec(name, description, metricStringKeys...)
g.labels = metricKeys
}
return g
}