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
125 lines (105 loc) · 4.01 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
package labeled
import (
"context"
"github.com/lyft/flytestdlib/contextutils"
"github.com/lyft/flytestdlib/promutils"
"github.com/prometheus/client_golang/prometheus"
)
// Represents a gauge labeled with values from the context. See labeled.SetMetricsKeys for more information
type Gauge struct {
*prometheus.GaugeVec
prometheus.Gauge
additionalLabels []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 to configure that.
func (g Gauge) Inc(ctx context.Context) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...))
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 to configure that.
func (g Gauge) Add(ctx context.Context, v float64) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...))
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 to configure that.
func (g Gauge) Set(ctx context.Context, v float64) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...))
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 to configure that.
func (g Gauge) Dec(ctx context.Context) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...))
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 to configure that.
func (g Gauge) Sub(ctx context.Context, v float64) {
gauge, err := g.GaugeVec.GetMetricWith(contextutils.Values(ctx, append(metricKeys, g.additionalLabels...)...))
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()
}
}
// 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 to configure that.
func NewGauge(name, description string, scope promutils.Scope, opts ...MetricOption) Gauge {
if len(metricKeys) == 0 {
panic(ErrNeverSet)
}
g := Gauge{}
for _, opt := range opts {
if _, emitUnlabeledMetric := opt.(EmitUnlabeledMetricOption); emitUnlabeledMetric {
g.Gauge = scope.MustNewGauge(GetUnlabeledMetricName(name), description)
} else if additionalLabels, casted := opt.(AdditionalLabelsOption); casted {
g.GaugeVec = scope.MustNewGaugeVec(name, description, append(metricStringKeys, additionalLabels.Labels...)...)
g.additionalLabels = contextutils.MetricKeysFromStrings(additionalLabels.Labels)
}
}
if g.GaugeVec == nil {
g.GaugeVec = scope.MustNewGaugeVec(name, description, metricStringKeys...)
}
return g
}