-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.go
168 lines (140 loc) · 3.52 KB
/
client.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package stats
import (
"encoding/json"
"fmt"
"net"
"strconv"
"sync"
"time"
)
type httpStats struct {
lock sync.RWMutex
requests []*HTTPRequest
}
func (h *httpStats) add(r *HTTPRequest) {
h.lock.Lock()
defer h.lock.Unlock()
h.requests = append(h.requests, r)
}
func (h *httpStats) extract() []*HTTPRequest {
h.lock.Lock()
defer h.lock.Unlock()
old := h.requests
h.requests = []*HTTPRequest{}
return old
}
// ClientConfig is used to initialize a new ClientStats object
type ClientConfig struct {
Domain string
Port int
PollInterval int
Debug bool
LogHostInfo bool
LogCPUInfo bool
LogTotalCPUTimes bool
LogPerCPUTimes bool
LogMemory bool
LogGoMemory bool
CustomBufferSize int
}
// ClientStats is the object used to collect and send data to the server for processing
type ClientStats struct {
localAddr string
serverAddr string
stop chan struct{}
interval int
debug bool
httpStats *httpStats
logHostInfo bool
logCPUInfo bool
logTotalCPUTimes bool
logPerCPUTimes bool
logMemory bool
logGoMemory bool
bufferSize int
}
// NewClient create a new client object for use
func NewClient(clientConfig *ClientConfig, serverConfig *ServerConfig) (*ClientStats, error) {
bSize := clientConfig.CustomBufferSize
if bSize == 0 {
bSize = defaultBufferSize
}
return &ClientStats{
localAddr: clientConfig.Domain + ":" + strconv.Itoa(clientConfig.Port),
serverAddr: serverConfig.Domain + ":" + strconv.Itoa(serverConfig.Port),
interval: clientConfig.PollInterval,
stop: make(chan struct{}),
debug: clientConfig.Debug,
httpStats: new(httpStats),
logHostInfo: clientConfig.LogHostInfo,
logCPUInfo: clientConfig.LogCPUInfo,
logTotalCPUTimes: clientConfig.LogTotalCPUTimes,
logPerCPUTimes: clientConfig.LogPerCPUTimes,
logMemory: clientConfig.LogMemory,
logGoMemory: clientConfig.LogGoMemory,
bufferSize: bSize,
}, nil
}
// Run starts sending the profiling stats to the server
// NOTE: the server must be running prior to starting
func (c *ClientStats) Run() {
var localAddr *net.UDPAddr
var serverAddr *net.UDPAddr
var client *net.UDPConn
var err error
serverAddr, err = net.ResolveUDPAddr(udp, c.serverAddr)
if err != nil {
panic(err)
}
localAddr, err = net.ResolveUDPAddr(udp, c.localAddr)
if err != nil {
panic(err)
}
client, err = net.DialUDP(udp, localAddr, serverAddr)
if err != nil {
panic(err)
}
defer client.Close()
client.SetWriteBuffer(c.bufferSize)
stats := new(Stats)
if c.logHostInfo {
stats.GetHostInfo()
}
if c.logCPUInfo {
stats.GetCPUInfo()
}
var bytesWritten int
var bytes []byte
ticker := time.NewTicker(time.Millisecond * time.Duration(c.interval))
defer ticker.Stop()
for {
select {
case <-ticker.C:
if c.logTotalCPUTimes {
stats.GetTotalCPUTimes()
}
if c.logPerCPUTimes {
stats.GetCPUTimes()
}
stats.GetMemoryInfo(c.logMemory, c.logGoMemory)
stats.HTTPRequests = c.httpStats.extract()
bytes, err = json.Marshal(stats)
bytesWritten, err = client.Write(bytes)
if err != nil {
fmt.Println(err)
continue
}
if c.debug {
fmt.Println("Wrote:", bytesWritten, "bytes")
}
case <-c.stop:
fmt.Println("done")
return
}
}
}
// Stop halts the client from sending any more data to the server,
// but may be run again at any time.
func (c *ClientStats) Stop() {
c.stop <- struct{}{}
}