-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgauge.go
217 lines (192 loc) · 5.87 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"fmt"
"sort"
promproto "github.com/prometheus/client_model/go"
"go.uber.org/net/metrics/push"
)
// A Gauge is a point-in-time measurement, like a car's speedometer. All its
// exported methods are safe to use concurrently, and nil *Gauges are safe
// no-op implementations.
type Gauge struct {
val value
pusher push.Gauge
}
func newGauge(m metadata) *Gauge {
return &Gauge{val: newValue(m)}
}
func newDynamicGauge(m metadata, variableTagPairs []string) metric {
return &Gauge{val: newDynamicValue(m, variableTagPairs)}
}
// Add increases the value of the gauge and returns the new value. Adding
// negative values is allowed, but using Sub may be simpler.
func (g *Gauge) Add(n int64) int64 {
if g == nil {
return 0
}
return g.val.Add(n)
}
// Sub decreases the value of the gauge and returns the new value. Subtracting
// negative values is allowed, but using Add may be simpler.
func (g *Gauge) Sub(n int64) int64 {
if g == nil {
return 0
}
return g.val.Sub(n)
}
// Inc increments the gauge's current value by one and returns the new value.
func (g *Gauge) Inc() int64 {
return g.Add(1)
}
// Dec decrements the gauge's current value by one and returns the new value.
func (g *Gauge) Dec() int64 {
return g.Sub(1)
}
// Swap replaces the gauge's current value and returns the previous value.
func (g *Gauge) Swap(n int64) int64 {
if g == nil {
return 0
}
return g.val.Swap(n)
}
// CAS is an atomic compare-and-swap. It compares the current value to the old
// value supplied, and if they match it stores the new value. The return value
// indicates whether the swap succeeded. To avoid endless CAS loops, no-op
// gauges always return true.
func (g *Gauge) CAS(old, new int64) bool {
if g == nil {
return true
}
return g.val.CAS(old, new)
}
// Store sets the gauge's value.
func (g *Gauge) Store(n int64) {
if g != nil {
g.val.Store(n)
}
}
// Load returns the gauge's current value.
func (g *Gauge) Load() int64 {
if g == nil {
return 0
}
return g.val.Load()
}
func (g *Gauge) describe() metadata {
return g.val.meta
}
func (g *Gauge) snapshot() Snapshot {
return g.val.snapshot()
}
func (g *Gauge) proto() *promproto.MetricFamily {
return &promproto.MetricFamily{
Name: g.val.meta.Name,
Help: g.val.meta.Help,
Type: promproto.MetricType_GAUGE.Enum(),
Metric: []*promproto.Metric{g.metric()},
}
}
func (g *Gauge) metric() *promproto.Metric {
n := float64(g.val.Load())
return &promproto.Metric{
Label: g.val.tagPairs,
Gauge: &promproto.Gauge{Value: &n},
}
}
func (g *Gauge) push(target push.Target) {
if g.val.meta.DisablePush {
return
}
if g.pusher == nil {
g.pusher = target.NewGauge(push.Spec{
Name: *g.val.meta.Name,
Tags: zip(g.val.tagPairs),
})
}
g.pusher.Set(g.Load())
}
// A GaugeVector is a collection of Gauges that share a name and some constant
// tags, but also have a consistent set of variable tags. All exported methods
// are safe to use concurrently. Nil *GaugeVectors are safe to use and always
// return no-op gauges.
//
// For a general description of vector types, see the package-level
// documentation.
type GaugeVector struct {
vector
}
func newGaugeVector(m metadata) *GaugeVector {
return &GaugeVector{vector{
meta: m,
factory: newDynamicGauge,
metrics: make(map[string]uint32, _defaultCollectionSize),
metricsStorage: make([]metric, 0, _defaultCollectionSize),
}}
}
// Get retrieves the gauge with the supplied variable tags names and values
// from the vector, creating one if necessary. The variable tags must be
// supplied in the same order used when creating the vector.
//
// Get returns an error if the number or order of tags is incorrect.
func (gv *GaugeVector) Get(variableTagPairs ...string) (*Gauge, error) {
if gv == nil {
return nil, nil
}
m, err := gv.getOrCreate(variableTagPairs)
if err != nil {
return nil, err
}
return m.(*Gauge), nil
}
// MustGet behaves exactly like Get, but panics on errors. If code using this
// method is covered by unit tests, this is safe.
func (gv *GaugeVector) MustGet(variableTagPairs ...string) *Gauge {
if gv == nil {
return nil
}
g, err := gv.Get(variableTagPairs...)
if err != nil {
panic(fmt.Sprintf("failed to get gauge: %v", err))
}
return g
}
func (gv *GaugeVector) describe() metadata {
return gv.meta
}
func (gv *GaugeVector) proto() *promproto.MetricFamily {
mf := &promproto.MetricFamily{
Name: gv.meta.Name,
Help: gv.meta.Help,
Type: promproto.MetricType_GAUGE.Enum(),
}
gv.metricsMu.RLock()
protos := make([]*promproto.Metric, 0, len(gv.metrics))
for _, metric := range gv.metricsStorage {
protos = append(protos, metric.(*Gauge).metric())
}
gv.metricsMu.RUnlock()
sort.Slice(protos, func(i, j int) bool {
return protos[i].String() < protos[j].String()
})
mf.Metric = protos
return mf
}