-
Notifications
You must be signed in to change notification settings - Fork 444
/
statsutils.go
64 lines (51 loc) · 1.7 KB
/
statsutils.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
package utils
import (
"context"
"github.com/solo-io/go-utils/contextutils"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
func MakeGauge(name, description string, tagKeys ...tag.Key) *stats.Int64Measure {
gauge := stats.Int64(name, description, stats.UnitDimensionless)
_ = view.Register(&view.View{
Name: gauge.Name(),
Measure: gauge,
Description: gauge.Description(),
Aggregation: view.LastValue(),
TagKeys: tagKeys,
})
return gauge
}
func MakeSumCounter(name, description string, tagKeys ...tag.Key) *stats.Int64Measure {
return MakeCounter(name, description, view.Sum(), tagKeys...)
}
func MakeLastValueCounter(name, description string, tagKeys ...tag.Key) *stats.Int64Measure {
return MakeCounter(name, description, view.LastValue(), tagKeys...)
}
func MakeCounter(name, description string, aggregation *view.Aggregation, tagKeys ...tag.Key) *stats.Int64Measure {
counter := stats.Int64(name, description, stats.UnitDimensionless)
_ = view.Register(&view.View{
Name: counter.Name(),
Measure: counter,
Description: counter.Description(),
Aggregation: aggregation,
TagKeys: tagKeys,
})
return counter
}
func MeasureZero(ctx context.Context, counter *stats.Int64Measure, tags ...tag.Mutator) {
Measure(ctx, counter, 0, tags...)
}
func MeasureOne(ctx context.Context, counter *stats.Int64Measure, tags ...tag.Mutator) {
Measure(ctx, counter, 1, tags...)
}
func Measure(ctx context.Context, counter *stats.Int64Measure, val int64, tags ...tag.Mutator) {
if err := stats.RecordWithTags(
ctx,
tags,
counter.M(val),
); err != nil {
contextutils.LoggerFrom(ctx).Errorf("setting counter %v: %v", counter.Name(), err)
}
}