forked from leifwalsh/olbermann
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reflector.go
125 lines (112 loc) · 2.89 KB
/
reflector.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
124
125
package olbermann
import (
"errors"
"reflect"
"time"
)
func toFloat(fval reflect.Value) float64 {
switch fval.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(fval.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(fval.Uint())
case reflect.Float32, reflect.Float64:
return fval.Float()
default:
panic("wrong kind for float " + fval.Kind().String())
}
}
type reportType interface {
name() string
add(fval reflect.Value)
get(iterDuration time.Duration, cumDuration time.Duration) float64
string(val float64) string
close()
}
type metricType struct {
name string
reports []reportType
}
type metricSetType struct {
metrics []metricType
}
func newMetricSetTypeOf(val interface{}) (mst *metricSetType, err error) {
rtype := reflect.TypeOf(val)
if rtype.Kind() == reflect.Ptr {
rtype = rtype.Elem()
}
if rtype.Kind() != reflect.Struct {
err = errors.New("invalid kind of metric " + rtype.Kind().String())
return
}
return newMetricSetType(rtype)
}
func newMetricSetType(rtype reflect.Type) (mst *metricSetType, err error) {
newMst := new(metricSetType)
newMst.metrics = make([]metricType, rtype.NumField())
for i := 0; i < rtype.NumField(); i++ {
field := rtype.Field(i)
switch field.Tag.Get("type") {
case "counter":
if newMst.metrics[i], err = newCounterMetric(field); err != nil {
return
}
case "latency":
if newMst.metrics[i], err = newLatencyMetric(field); err != nil {
return
}
default:
err = errors.New("metric " + field.Name + " is of unknown type " + field.Tag.Get("type"))
return
}
}
mst = newMst
return
}
func (mst *metricSetType) update(val interface{}) (err error) {
rval := reflect.Indirect(reflect.ValueOf(val))
if rval.Kind() != reflect.Struct {
err = errors.New("invalid kind of metric " + rval.Kind().String())
return
}
for i := range mst.metrics {
rt := mst.metrics[i]
val := rval.Field(i)
for j := range rt.reports {
rt.reports[j].add(val)
}
}
return
}
func (mst *metricSetType) close() {
for i := range mst.metrics {
rt := mst.metrics[i]
for j := range rt.reports {
rt.reports[j].close()
}
}
}
type reportValue struct {
name string
value float64
}
type metricValue struct {
name string
reports []reportValue
}
type metricSetValue struct {
metrics []metricValue
}
func (mst *metricSetType) getValues(iterDuration time.Duration, cumDuration time.Duration) (msv *metricSetValue) {
msv = &metricSetValue{metrics: make([]metricValue, len(mst.metrics))}
for i := range mst.metrics {
metric := mst.metrics[i]
msv.metrics[i].name = metric.name
msv.metrics[i].reports = make([]reportValue, len(metric.reports))
for j := range metric.reports {
report := metric.reports[j]
msv.metrics[i].reports[j] = reportValue{name: report.name(), value: report.get(iterDuration, cumDuration)}
}
}
return
}