-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
101 lines (91 loc) · 2.58 KB
/
stats.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
package stats
import (
"bytes"
"expvar"
"fmt"
"time"
)
var (
TotalConnections *expvar.Int
ActiveConnections *expvar.Int
BytesIn *expvar.Int
BytesOut *expvar.Int
TotalRequests *expvar.Int
BatchRequests *expvar.Int
ReadRequests *expvar.Int
TailRequests *expvar.Int
StatsRequests *expvar.Int
CloseRequests *expvar.Int
ConfigRequests *expvar.Int
TotalErrors *expvar.Int
BatchErrors *expvar.Int
ReadErrors *expvar.Int
TailErrors *expvar.Int
StatsErrors *expvar.Int
CloseErrors *expvar.Int
ConfigErrors *expvar.Int
TotalTopics *expvar.Int
DisallowedTopics *expvar.Int
)
func init() {
TotalConnections = expvar.NewInt("conns.total")
ActiveConnections = expvar.NewInt("conns.active")
BytesIn = expvar.NewInt("bytes.in")
BytesOut = expvar.NewInt("bytes.out")
TotalRequests = expvar.NewInt("requests.total")
BatchRequests = expvar.NewInt("requests.batch")
ReadRequests = expvar.NewInt("requests.read")
TailRequests = expvar.NewInt("requests.tail")
StatsRequests = expvar.NewInt("requests.stats")
CloseRequests = expvar.NewInt("requests.close")
ConfigRequests = expvar.NewInt("requests.config")
TotalErrors = expvar.NewInt("errors.total")
BatchErrors = expvar.NewInt("errors.batch")
ReadErrors = expvar.NewInt("errors.read")
TailErrors = expvar.NewInt("errors.tail")
StatsErrors = expvar.NewInt("errors.stats")
CloseErrors = expvar.NewInt("errors.close")
ConfigErrors = expvar.NewInt("errors.config")
TotalTopics = expvar.NewInt("topics.total")
DisallowedTopics = expvar.NewInt("topics.disallowed")
}
// MultiOK returns an MOK response body
func MultiOK() []byte {
b := &bytes.Buffer{}
expvar.Do(func(kv expvar.KeyValue) {
if kv.Key == "memstats" || kv.Key == "cmdline" {
return
}
b.WriteString(kv.Key)
b.WriteString(": ")
b.WriteString(kv.Value.String())
b.WriteString("\r\n")
})
return b.Bytes()
}
func periodicFlush() {
for {
time.Sleep(5 * time.Second)
expvar.Do(func(kv expvar.KeyValue) {
if kv.Key == "memstats" || kv.Key == "cmdline" {
return
}
fmt.Println(kv.Key, kv.Value.String())
})
}
}
func PrettyTime(ns float64) string {
if ns > float64(time.Second*60) {
return fmt.Sprintf("%.2fm", ns/float64(time.Second*60))
}
if ns > float64(time.Second) {
return fmt.Sprintf("%.2fs", ns/float64(time.Second))
}
if ns > float64(time.Millisecond) {
return fmt.Sprintf("%.2fms", ns/float64(time.Millisecond))
}
if ns > float64(time.Microsecond) {
return fmt.Sprintf("%.2fμ", ns/float64(time.Microsecond))
}
return fmt.Sprintf("%.2fns", ns)
}