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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
package clients
import (
"context"
"fmt"
"runtime"
"strings"
"sync"
"time"
"github.com/mimecast/dtail/internal/color"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/dlog"
"github.com/mimecast/dtail/internal/protocol"
)
// Used to collect and display various client stats.
type stats struct {
// Total amount servers to connect to.
servers int
// To keep track of what connected and disconnected
connectionsEstCh chan struct{}
// Amount of servers connections are established.
connected int
// To synchronize concurrent access.
mutex sync.Mutex
}
func newTailStats(servers int) *stats {
return &stats{
servers: servers,
connectionsEstCh: make(chan struct{}, servers),
connected: 0,
}
}
// Start starts printing client connection stats every time a signal is received or
// connection count has changed.
func (s *stats) Start(ctx context.Context, throttleCh <-chan struct{},
statsCh <-chan string, quiet bool) {
var connectedLast int
for {
var force bool
var messages []string
select {
case message := <-statsCh:
messages = append(messages, message)
force = true
case <-time.After(time.Second * 3):
case <-ctx.Done():
return
}
connected := len(s.connectionsEstCh)
throttle := len(throttleCh)
newConnections := connected - connectedLast
if (connected == connectedLast || quiet) && !force {
continue
}
switch force {
case true:
stats := s.statsLine(connected, newConnections, throttle)
messages = append(messages, fmt.Sprintf("Connection stats: %s", stats))
s.printStatsDueInterrupt(messages)
default:
data := s.statsData(connected, newConnections, throttle)
dlog.Client.Mapreduce("STATS", data)
}
connectedLast = connected
s.mutex.Lock()
s.connected = connected
s.mutex.Unlock()
}
}
func (s *stats) printStatsDueInterrupt(messages []string) {
dlog.Client.Pause()
for i, message := range messages {
if i > 0 && config.Client.TermColorsEnable {
fmt.Println(color.PaintStrWithAttr(message,
config.Client.TermColors.Client.ClientFg,
config.Client.TermColors.Client.ClientBg,
config.Client.TermColors.Client.ClientAttr,
))
continue
}
fmt.Println(fmt.Sprintf(" %s", message))
}
time.Sleep(time.Second * time.Duration(config.InterruptTimeoutS))
dlog.Client.Resume()
}
func (s *stats) statsData(connected, newConnections int,
throttle int) map[string]interface{} {
percConnected := percentOf(float64(s.servers), float64(connected))
data := make(map[string]interface{})
data["connected"] = connected
data["servers"] = s.servers
data["connected%"] = int(percConnected)
data["new"] = newConnections
data["throttle"] = throttle
data["goroutines"] = runtime.NumGoroutine()
data["cgocalls"] = runtime.NumCgoCall()
data["cpu"] = runtime.NumCPU()
return data
}
func (s *stats) statsLine(connected, newConnections int, throttle int) string {
sb := strings.Builder{}
i := 0
for k, v := range s.statsData(connected, newConnections, throttle) {
if i > 0 {
sb.WriteString(protocol.FieldDelimiter)
}
sb.WriteString(k)
sb.WriteByte('=')
sb.WriteString(fmt.Sprintf("%v", v))
i++
}
return sb.String()
}
func (s *stats) numConnected() int {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.connected
}
func percentOf(total float64, value float64) float64 {
if total == 0 || total == value {
return 100
}
return value / (total / 100.0)
}
|