/
summations.go
108 lines (86 loc) · 2.98 KB
/
summations.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
package statz
import (
"context"
"github.com/edaniels/golog"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
)
// Summation0 is a incremental int64 summation type with no metric labels.
type Summation0 struct {
wrapper *ocSummationWrapper
}
// Inc increments summation by 1.
func (c *Summation0) Inc() {
c.IncBy(1)
}
// IncBy increments summation by X.
func (c *Summation0) IncBy(by int64) {
c.wrapper.incBy(context.Background(), labelsToStringSlice(), by)
}
// Summation1 is a incremental int64 summation type with 1 metric label.
type Summation1[T1 labelContraint] struct {
wrapper *ocSummationWrapper
}
// Inc increments summation by 1.
func (c *Summation1[T1]) Inc(v1 T1) {
c.IncBy(v1, 1)
}
// IncBy increments summation by X.
func (c *Summation1[T1]) IncBy(v1 T1, by int64) {
c.wrapper.incBy(context.Background(), labelsToStringSlice(v1), by)
}
// Summation2 is a incremental int64 summation type with 2 metric label.
type Summation2[T1 labelContraint, T2 labelContraint] struct {
wrapper *ocSummationWrapper
}
// Inc increments summation by 1.
func (c *Summation2[T1, T2]) Inc(v1 T1, v2 T2) {
c.IncBy(v1, v2, 1)
}
// IncBy increments summation by X.
func (c *Summation2[T1, T2]) IncBy(v1 T1, v2 T2, by int64) {
c.wrapper.incBy(context.Background(), labelsToStringSlice(v1, v2), by)
}
// Summation3 is a incremental int64 summation type with 3 metric label.
type Summation3[T1 labelContraint, T2 labelContraint, T3 labelContraint] struct {
wrapper *ocSummationWrapper
}
// Inc increments summation by 1.
func (c *Summation3[T1, T2, T3]) Inc(v1 T1, v2 T2, v3 T3) {
c.IncBy(v1, v2, v3, 1)
}
// IncBy increments summation by X.
func (c *Summation3[T1, T2, T3]) IncBy(v1 T1, v2 T2, v3 T3, by int64) {
c.wrapper.incBy(context.Background(), labelsToStringSlice(v1, v2, v3), by)
}
// Summation4 is a incremental int64 summation type with 4 metric label.
type Summation4[T1 labelContraint, T2 labelContraint, T3 labelContraint, T4 labelContraint] struct {
wrapper *ocSummationWrapper
}
// Inc increments summation by 1.
func (c *Summation4[T1, T2, T3, T4]) Inc(v1 T1, v2 T2, v3 T3, v4 T4) {
c.IncBy(v1, v2, v3, v4, 1)
}
// IncBy increments summation by X.
func (c *Summation4[T1, T2, T3, T4]) IncBy(v1 T1, v2 T2, v3 T3, v4 T4, by int64) {
c.wrapper.incBy(context.Background(), labelsToStringSlice(v1, v2, v3, v4), by)
}
///// internal
type ocSummationWrapper struct {
data *opencensusStatsData
measure *stats.Int64Measure
}
func (w *ocSummationWrapper) incBy(ctx context.Context, labels []string, incBy int64) {
mutations := w.data.labelsToMutations(labels)
if err := stats.RecordWithTags(ctx, mutations, w.measure.M(incBy)); err != nil {
golog.Global().Errorf("faild to write metric %s", err)
}
}
func createSummationWrapper(name string, cfg MetricConfig) *ocSummationWrapper {
measure := stats.Int64(name, cfg.Description, string(cfg.Unit))
ocData := createAndRegisterOpenCensusMetric(name, measure, view.Sum(), cfg)
return &ocSummationWrapper{
data: ocData,
measure: measure,
}
}