-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathint64.go
250 lines (205 loc) · 6 KB
/
int64.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package metrics
import (
"context"
"sync"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
// Int64 wraps an opencensus int64 measure that is uses as a gauge.
type Int64 struct {
value int64
measureCt *stats.Int64Measure
view *view.View
mux sync.Mutex
}
// Set sets the value to `v`.
func (i *Int64) Set(ctx context.Context, v int64) {
i.mux.Lock()
defer i.mux.Unlock()
i.value = v
i.record(ctx)
}
// Inc increments the inner value by value `v`.
func (i *Int64) Inc(ctx context.Context, v int64) {
i.mux.Lock()
defer i.mux.Unlock()
i.value += v
i.record(ctx)
}
// Set sets the value of the gauge to value `v`.
func (i *Int64) record(ctx context.Context) {
stats.Record(ctx, i.measureCt.M(i.value))
}
// NewInt64 creates a new Int64 Gauge
func NewInt64(name, desc string, unit string, keys ...tag.Key) *Int64 {
if unit == "" {
unit = stats.UnitDimensionless
}
iMeasure := stats.Int64(name, desc, unit)
iView := &view.View{
Name: name,
Measure: iMeasure,
Description: desc,
Aggregation: view.LastValue(),
TagKeys: keys,
}
if err := view.Register(iView); err != nil {
// a panic here indicates a developer error when creating a view.
// Since this method is called in init() methods, this panic when hit
// will cause running the program to fail immediately.
panic(err)
}
return &Int64{
measureCt: iMeasure,
view: iView,
}
}
// NewInt64Gauge is just the alias of NewInt64
func NewInt64Gauge(name, desc string, unit string, keys ...tag.Key) *Int64 {
return NewInt64(name, desc, unit, keys...)
}
// NewInt64Gauge creates a new Int64 with buckets.
func NewInt64WithBuckets(name, desc string, unit string, bounds []float64, tagKeys ...tag.Key) *Int64 {
if unit == "" {
unit = stats.UnitDimensionless
}
iMeasure := stats.Int64(name, desc, unit)
iView := &view.View{
Name: name,
Measure: iMeasure,
Description: desc,
Aggregation: view.Distribution(bounds...),
TagKeys: tagKeys,
}
if err := view.Register(iView); err != nil {
// a panic here indicates a developer error when creating a view.
// Since this method is called in init() methods, this panic when hit
// will cause running the program to fail immediately.
panic(err)
}
return &Int64{
measureCt: iMeasure,
view: iView,
}
}
// NewInt64Counter creates a new Int64 with counter vie
// if what you want is just a counter please use NewCounter instead
func NewInt64WithCounter(name, desc string, unit string, keys ...tag.Key) *Int64 {
if unit == "" {
unit = stats.UnitDimensionless
}
iMeasure := stats.Int64(name, desc, unit)
iView := &view.View{
Name: name,
Measure: iMeasure,
Description: desc,
TagKeys: keys,
Aggregation: view.Count(),
}
if err := view.Register(iView); err != nil {
// a panic here indicates a developer error when creating a view.
// Since this method is called in init() methods, this panic when hit
// will cause running the program to fail immediately.
panic(err)
}
return &Int64{
measureCt: iMeasure,
view: iView,
}
}
// NewInt64WithSummarizer creates a new Int64 with Summarizer
func NewInt64WithSummarizer(name, desc string, unit string, keys ...tag.Key) *Int64 {
if unit == "" {
unit = stats.UnitDimensionless
}
iMeasure := stats.Int64(name, desc, unit)
iView := &view.View{
Name: name,
Measure: iMeasure,
Description: desc,
TagKeys: keys,
Aggregation: view.Count(),
}
if err := view.Register(iView); err != nil {
// a panic here indicates a developer error when creating a view.
// Since this method is called in init() methods, this panic when hit
// will cause running the program to fail immediately.
panic(err)
}
return &Int64{
measureCt: iMeasure,
view: iView,
}
}
type Counter Int64
// Tick triggers a record with value 1
func (c *Counter) Tick(ctx context.Context) {
(*Int64)(c).Set(ctx, 1)
}
func NewCounter(name, desc string, keys ...tag.Key) *Counter {
return (*Counter)(NewInt64WithCounter(name, desc, "", keys...))
}
var tagCategory = tag.MustNewKey("category")
type Int64WithCategory struct {
value map[string]int64
measureCt *stats.Int64Measure
view *view.View
mux sync.Mutex
}
// Set sets the value to `v`.
func (i *Int64WithCategory) Set(ctx context.Context, category string, v int64) {
i.mux.Lock()
defer i.mux.Unlock()
ctx, _ = tag.New(ctx, tag.Insert(tagCategory, category))
if _, ok := i.value[category]; !ok {
i.value[category] = 0
}
i.value[category] = v
stats.Record(ctx, i.measureCt.M(i.value[category]))
}
// Inc increments the inner value by value `v`.
func (i *Int64WithCategory) Inc(ctx context.Context, category string, v int64) {
i.mux.Lock()
defer i.mux.Unlock()
ctx, _ = tag.New(ctx, tag.Insert(tagCategory, category))
if _, ok := i.value[category]; !ok {
i.value[category] = 0
}
i.value[category] += v
stats.Record(ctx, i.measureCt.M(i.value[category]))
}
func NewInt64WithCategory(name, desc string, unit string, keys ...tag.Key) *Int64WithCategory {
keys = append(keys, tagCategory)
if unit == "" {
unit = stats.UnitDimensionless
}
iMeasure := stats.Int64(name, desc, unit)
iView := &view.View{
Name: name,
Measure: iMeasure,
Description: desc,
Aggregation: view.LastValue(),
TagKeys: keys,
}
if err := view.Register(iView); err != nil {
// a panic here indicates a developer error when creating a view.
// Since this method is called in init() methods, this panic when hit
// will cause running the program to fail immediately.
panic(err)
}
value := make(map[string]int64)
return &Int64WithCategory{
measureCt: iMeasure,
view: iView,
value: value,
}
}
type CounterWithCategory Int64WithCategory
// Tick triggers a record with value 1
func (c *CounterWithCategory) Tick(ctx context.Context, category string) {
(*Int64WithCategory)(c).Set(ctx, category, 1)
}
func NewCounterWithCategory(name, desc string, keys ...tag.Key) *CounterWithCategory {
return (*CounterWithCategory)(NewInt64WithCategory(name, desc, "", keys...))
}