/
metric_type.go
123 lines (109 loc) · 2.72 KB
/
metric_type.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
package metrics
import "errors"
// A MetricType specifies the type of a metric.
type MetricType int
// Possible values for MetricType.
const (
Counter = MetricType(iota) // A counter that sums its data points
Gauge // A gauge that displays the latest value
Trend // A trend, min/max/avg/med are interesting
Rate // A rate, displays % of values that aren't 0
)
// ErrInvalidMetricType indicates the serialized metric type is invalid.
var ErrInvalidMetricType = errors.New("invalid metric type")
const (
counterString = "counter"
gaugeString = "gauge"
trendString = "trend"
rateString = "rate"
defaultString = "default"
timeString = "time"
dataString = "data"
negativeString = "negative"
)
// MarshalJSON serializes a MetricType as a human readable string.
func (t MetricType) MarshalJSON() ([]byte, error) {
txt, err := t.MarshalText()
if err != nil {
return nil, err
}
return []byte(`"` + string(txt) + `"`), nil
}
// MarshalText serializes a MetricType as a human readable string.
func (t MetricType) MarshalText() ([]byte, error) {
switch t {
case Counter:
return []byte(counterString), nil
case Gauge:
return []byte(gaugeString), nil
case Trend:
return []byte(trendString), nil
case Rate:
return []byte(rateString), nil
default:
return nil, ErrInvalidMetricType
}
}
// UnmarshalText deserializes a MetricType from a string representation.
func (t *MetricType) UnmarshalText(data []byte) error {
switch string(data) {
case counterString:
*t = Counter
case gaugeString:
*t = Gauge
case trendString:
*t = Trend
case rateString:
*t = Rate
default:
return ErrInvalidMetricType
}
return nil
}
func (t MetricType) String() string {
switch t {
case Counter:
return counterString
case Gauge:
return gaugeString
case Trend:
return trendString
case Rate:
return rateString
default:
return "[INVALID]"
}
}
// supportedAggregationMethods returns the list of threshold aggregation methods
// that can be used against this MetricType.
func (t MetricType) supportedAggregationMethods() []string {
switch t {
case Counter:
return []string{tokenCount, tokenRate}
case Gauge:
return []string{tokenValue}
case Rate:
return []string{tokenRate}
case Trend:
return []string{
tokenAvg,
tokenMin,
tokenMax,
tokenMed,
tokenPercentile,
}
default:
// unreachable!
panic("unreachable")
}
}
// supportsAggregationMethod returns whether the MetricType supports a
// given threshold aggregation method or not.
func (t MetricType) supportsAggregationMethod(aggregationMethod string) bool {
for _, m := range t.supportedAggregationMethods() {
if aggregationMethod == m {
return true
}
}
return false
}