-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
82 lines (71 loc) · 1.45 KB
/
types.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
package main
type MetricType int64
const (
Counter = MetricType(iota)
Timer
Gauge
Averager
Accumulator
NMetricTypes = iota
)
var (
metricTypes [NMetricTypes]metricType
outputChannels map[string]MetricType = make(map[string]MetricType)
)
type metric interface {
init([]float64)
inject(*Metric)
tick() []float64
flush() []float64
}
type aggregator interface {
channels() []int
init([]float64)
put([]float64)
get() []float64
}
type metricType struct {
create func() metric
channels []string
defaults []float64
persist []bool
aggregator func([]string) aggregator
}
func registerMetricType(typ MetricType, mt metricType) {
metricTypes[typ] = mt
for _, ch := range mt.channels {
outputChannels[ch] = typ
}
}
func metricTypeByChannels(chs []string) (MetricType, error) {
if len(chs) == 0 {
return -1, Error("No channels specified")
}
typ, ok := outputChannels[chs[0]]
if !ok {
return -1, Error("No such channel: " + chs[0])
}
names := map[string]bool{chs[0]: true}
for _, ch := range chs[1:] {
t, ok := outputChannels[ch]
if !ok {
return -1, Error("No such channel: " + ch)
}
if t != typ {
return -1, Error("Cannot mix different metric types")
}
if names[ch] {
return -1, Error("Channel names must be unique: " + ch)
}
names[ch] = true
}
return typ, nil
}
func getChannelIndex(typ MetricType, ch string) int {
for i, n := range metricTypes[typ].channels {
if n == ch {
return i
}
}
return -1
}