-
Notifications
You must be signed in to change notification settings - Fork 444
/
statsutils.go
72 lines (59 loc) · 2.52 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
65
66
67
68
69
70
71
72
package utils
import (
"context"
"github.com/solo-io/go-utils/contextutils"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
// MakeGauge returns a new gauge with the given name and description
func MakeGauge(name, description string, tagKeys ...tag.Key) *stats.Int64Measure {
return MakeLastValueCounter(name, description, tagKeys...)
}
// MakeSumCounter returns a new counter with a Sum aggregation for the given name, description, and tag keys
func MakeSumCounter(name, description string, tagKeys ...tag.Key) *stats.Int64Measure {
return MakeCounter(name, description, view.Sum(), tagKeys...)
}
// MakeLastValueCounter returns a counter with a LastValue aggregation for the given name, description, and tag keys
func MakeLastValueCounter(name, description string, tagKeys ...tag.Key) *stats.Int64Measure {
return MakeCounter(name, description, view.LastValue(), tagKeys...)
}
// MakeCounter returns a new counter with the given name, description, aggregation, and tag keys
func MakeCounter(name, description string, aggregation *view.Aggregation, tagKeys ...tag.Key) *stats.Int64Measure {
counter := Int64Measure(name, description)
counterView := ViewForCounter(counter, aggregation, tagKeys...)
_ = view.Register(counterView)
return counter
}
// MeasureZero records a zero value to the given counter
func MeasureZero(ctx context.Context, counter *stats.Int64Measure, tags ...tag.Mutator) {
Measure(ctx, counter, 0, tags...)
}
// MeasureOne records a one value to the given counter
func MeasureOne(ctx context.Context, counter *stats.Int64Measure, tags ...tag.Mutator) {
Measure(ctx, counter, 1, tags...)
}
// Measure records the given value to the given counter
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)
}
}
// Int64Measure returns a new Int64Measure with the given name and description
func Int64Measure(name, description string) *stats.Int64Measure {
return stats.Int64(name, description, stats.UnitDimensionless)
}
// ViewForCounter returns a view for the given measure with the given aggregation and tag keys
func ViewForCounter(counter *stats.Int64Measure, aggregation *view.Aggregation, tagKeys ...tag.Key) *view.View {
return &view.View{
Name: counter.Name(),
Measure: counter,
Description: counter.Description(),
Aggregation: aggregation,
TagKeys: tagKeys,
}
}