-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcounter.go
184 lines (164 loc) · 5.11 KB
/
counter.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
// 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 Counter is a monotonically increasing value, like a car's odometer. All
// its exported methods are safe to use concurrently, and nil *Counters are
// safe no-op implementations.
type Counter struct {
val value
pusher push.Counter
}
func newCounter(m metadata) *Counter {
return &Counter{val: newValue(m)}
}
func newDynamicCounter(m metadata, variableTagPairs []string) metric {
return &Counter{val: newDynamicValue(m, variableTagPairs)}
}
// Add increases the value of the counter and returns the new value. Since
// counters must be monotonically increasing, passing a negative number just
// returns the current value (without modifying it).
func (c *Counter) Add(n int64) int64 {
if c == nil {
return 0
}
if n <= 0 {
return c.val.Load()
}
return c.val.Add(n)
}
// Inc increments the counter's value by one and returns the new value.
func (c *Counter) Inc() int64 {
if c == nil {
return 0
}
return c.val.Inc()
}
// Load returns the counter's current value.
func (c *Counter) Load() int64 {
if c == nil {
return 0
}
return c.val.Load()
}
func (c *Counter) describe() metadata {
return c.val.meta
}
func (c *Counter) snapshot() Snapshot {
return c.val.snapshot()
}
func (c *Counter) proto() *promproto.MetricFamily {
return &promproto.MetricFamily{
Name: c.val.meta.Name,
Help: c.val.meta.Help,
Type: promproto.MetricType_COUNTER.Enum(),
Metric: []*promproto.Metric{c.metric()},
}
}
func (c *Counter) metric() *promproto.Metric {
n := float64(c.val.Load())
return &promproto.Metric{
Label: c.val.tagPairs,
Counter: &promproto.Counter{Value: &n},
}
}
func (c *Counter) push(target push.Target) {
if c.val.meta.DisablePush {
return
}
if c.pusher == nil {
c.pusher = target.NewCounter(push.Spec{
Name: *c.val.meta.Name,
Tags: zip(c.val.tagPairs),
})
}
c.pusher.Set(c.Load())
}
// A CounterVector is a collection of Counters 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 *CounterVectors are safe
// to use and always return no-op counters.
//
// For a general description of vector types, see the package-level
// documentation.
type CounterVector struct {
vector
}
func newCounterVector(m metadata) *CounterVector {
return &CounterVector{vector{
meta: m,
factory: newDynamicCounter,
metrics: make(map[string]uint32, _defaultCollectionSize),
metricsStorage: make([]metric, 0, _defaultCollectionSize),
}}
}
// Get retrieves the counter with the supplied variable tag 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 (cv *CounterVector) Get(variableTagPairs ...string) (*Counter, error) {
if cv == nil {
return nil, nil
}
m, err := cv.getOrCreate(variableTagPairs)
if err != nil {
return nil, err
}
return m.(*Counter), nil
}
// MustGet behaves exactly like Get, but panics on errors. If code using this
// method is covered by unit tests, this is safe.
func (cv *CounterVector) MustGet(variableTagPairs ...string) *Counter {
if cv == nil {
return nil
}
c, err := cv.Get(variableTagPairs...)
if err != nil {
panic(fmt.Sprintf("failed to get counter: %v", err))
}
return c
}
func (cv *CounterVector) describe() metadata {
return cv.meta
}
func (cv *CounterVector) proto() *promproto.MetricFamily {
mf := &promproto.MetricFamily{
Name: cv.meta.Name,
Help: cv.meta.Help,
Type: promproto.MetricType_COUNTER.Enum(),
}
cv.metricsMu.RLock()
protos := make([]*promproto.Metric, 0, len(cv.metrics))
for _, metric := range cv.metricsStorage {
protos = append(protos, metric.(*Counter).metric())
}
cv.metricsMu.RUnlock()
sort.Slice(protos, func(i, j int) bool {
return protos[i].String() < protos[j].String()
})
mf.Metric = protos
return mf
}