-
Notifications
You must be signed in to change notification settings - Fork 432
/
metrics.go
41 lines (34 loc) · 1.3 KB
/
metrics.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
package transforms
import (
errors "github.com/rotisserie/eris"
"go.opencensus.io/stats/view"
)
// WithLastValueTransform returns a function that takes a slice of view.Rows and returns the last value value or an errorß
func WithLastValueTransform() func(rows []*view.Row) (int, error) {
return func(rows []*view.Row) (int, error) {
if len(rows) == 0 {
// In the future, it would be ideal to distinguish between "no rows" and "a row with a value of 0"
// For now, we return 0 to represent both cases
return 0, nil
}
if len(rows) > 1 {
return 0, errors.Errorf("expected 1 row, found %d", len(rows))
}
return int(rows[0].Data.(*view.LastValueData).Value), nil
}
}
// WithSumValueTransform returns a function that takes a slice of view.Rows and returns the sum value or an error.
// Implemented to be used with counters to get the current total
func WithSumValueTransform() func(rows []*view.Row) (int, error) {
return func(rows []*view.Row) (int, error) {
if len(rows) == 0 {
// In the future, it would be ideal to distinguish between "no rows" and "a row with a value of 0"
// For now, we return 0 to represent both cases
return 0, nil
}
if len(rows) > 1 {
return 0, errors.Errorf("expected 1 row, found %d", len(rows))
}
return int(rows[0].Data.(*view.SumData).Value), nil
}
}