forked from leifwalsh/olbermann
-
Notifications
You must be signed in to change notification settings - Fork 1
/
latency_report_type.go
96 lines (80 loc) · 2.38 KB
/
latency_report_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
package olbermann
import (
"errors"
"fmt"
"github.com/bmizerany/perks/quantile"
"reflect"
"strconv"
"strings"
"time"
)
type windowLatencyReportType struct {
nameString string
strm *quantile.Stream
quant float64
}
func newWindowLatencyReportType(name string, quant float64) (res *windowLatencyReportType) {
res = &windowLatencyReportType{nameString: name, strm: quantile.NewTargeted(quant), quant: quant}
return
}
func (t *windowLatencyReportType) name() string {
return t.nameString
}
func (t *windowLatencyReportType) add(fval reflect.Value) {
t.strm.Insert(toFloat(fval))
}
func (t *windowLatencyReportType) get(iterDuration time.Duration, cumDuration time.Duration) (res float64) {
res = t.strm.Query(t.quant)
t.strm.Reset()
return
}
func (t *windowLatencyReportType) string(val float64) string {
return fmt.Sprintf("%.2f", val)
}
func (t *windowLatencyReportType) close() {}
type cumulativeLatencyReportType struct {
nameString string
strm *quantile.Stream
quant float64
}
func newCumulativeLatencyReportType(name string, quant float64) (res *cumulativeLatencyReportType) {
res = &cumulativeLatencyReportType{nameString: name, strm: quantile.NewTargeted(quant), quant: quant}
return
}
func (t *cumulativeLatencyReportType) name() string {
return t.nameString
}
func (t *cumulativeLatencyReportType) add(fval reflect.Value) {
t.strm.Insert(toFloat(fval))
}
func (t *cumulativeLatencyReportType) get(iterDuration time.Duration, cumDuration time.Duration) (res float64) {
res = t.strm.Query(t.quant)
return
}
func (t *cumulativeLatencyReportType) string(val float64) string {
return fmt.Sprintf("%.2f", val)
}
func (t *cumulativeLatencyReportType) close() {}
func newLatencyMetric(field reflect.StructField) (metric metricType, err error) {
reportNames := strings.Split(field.Tag.Get("report"), ",")
if len(reportNames) < 1 {
err = errors.New("latency metric " + field.Name + " must define reports")
return
}
reports := make([]reportType, len(reportNames))
for i := range reportNames {
var percentile float64
if percentile, err = strconv.ParseFloat(reportNames[i][1:], 64); err != nil {
return
}
switch reportNames[i][:1] {
case "w":
reports[i] = newWindowLatencyReportType(reportNames[i], percentile*0.01)
case "c":
reports[i] = newCumulativeLatencyReportType(reportNames[i], percentile*0.01)
}
}
metric.name = field.Name
metric.reports = reports
return
}