-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
280 lines (243 loc) · 6.3 KB
/
sender.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"bytes"
"compress/zlib"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"sync/atomic"
"time"
log "github.com/sirupsen/logrus"
// "github.com/pkg/profile"
)
func limitRefresher(limitPerSec *int, slog *log.Entry) {
savedLimit := *limitPerSec
slog.WithFields(log.Fields{"limitPerSec": savedLimit}).Info("Starting Packs Limit Refresher.")
for {
time.Sleep(1 * time.Second)
*limitPerSec = savedLimit
}
}
func runSender(
id int,
host string,
port int,
outputChan chan *Metric,
TLS bool,
mutualTLS bool,
tlsCaCert string,
tlsCert string,
tlsKey string,
ignoreCert bool,
limitPerSec int,
compress bool) {
var slog *log.Entry
slog = clog.WithFields(log.Fields{
"host": host,
"port": port,
"tls": TLS,
"mutualTLS": mutualTLS,
"ignoreCert": ignoreCert,
"compress": compress,
"thread": "sender",
"id": id,
})
slog.Info("Starting Sender.")
// Start limit refresher thread
go limitRefresher(&limitPerSec, slog)
for {
curLimit := limitPerSec
slog.WithFields(log.Fields{"limit": curLimit}).Debug("Current limit.")
if curLimit > 0 {
// Create output connection
connection, err := createConnection(
host,
port,
TLS,
mutualTLS,
tlsCaCert,
tlsCert,
tlsKey,
ignoreCert,
slog)
if err != nil {
slog.WithFields(log.Fields{"error": err}).Error("Can't create connection.")
atomic.AddInt64(&state.ConnectionError[id], 1)
time.Sleep(5 * time.Second)
continue
} else {
atomic.AddInt64(&state.Connection[id], 1)
atomic.AddInt64(&state.ConnectionAlive[id], 1)
}
// collect a pack of metrics
var metrics [10000]*Metric
for i := 0; i < len(metrics); i++ {
select {
case metric := <-outputChan:
metrics[i] = metric
default:
metrics[i] = emptyMetric
time.Sleep(5 * time.Millisecond)
}
}
// send the pack
if len(metrics) > 0 {
go sendMetric(&metrics, connection, outputChan, compress, slog, id)
limitPerSec--
}
} else {
slog.Warning("Limit of metric packs per second overflew.")
atomic.AddInt64(&state.PacksOverflewError[id], 1)
time.Sleep(1 * time.Second)
}
}
}
func createConnection(
host string,
port int,
TLS bool,
mutualTLS bool,
tlsCaCert string,
tlsCert string,
tlsKey string,
ignoreCert bool,
slog *log.Entry) (net.Conn, error) {
netAddress := fmt.Sprintf("%s:%d", host, port)
slog.Debug("Connecting to Graphite.")
timeout, _ := time.ParseDuration("10s")
dialer := &net.Dialer{Timeout: timeout}
var connection net.Conn
var err error
if TLS || mutualTLS {
var config *tls.Config
if mutualTLS {
caCert, err := ioutil.ReadFile(tlsCaCert)
if err != nil {
rlog.WithFields(log.Fields{"error": err}).Fatal("TLS Sender CA certificate error.")
}
certs, err := tls.LoadX509KeyPair(tlsCert, tlsKey)
if err != nil {
rlog.WithFields(log.Fields{"error": err}).Fatal("TLS Sender client certificates error.")
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
config = &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{certs},
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS12,
DynamicRecordSizingDisabled: false,
}
} else {
config = &tls.Config{
InsecureSkipVerify: ignoreCert,
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS12,
DynamicRecordSizingDisabled: false,
}
}
connection, err = tls.DialWithDialer(dialer, "tcp4", netAddress, config)
} else {
connection, err = dialer.Dial("tcp4", netAddress)
}
if err != nil {
slog.WithFields(log.Fields{"error": err}).Error("Dialer error.")
} else {
connection.SetDeadline(time.Now().Add(600 * time.Second))
slog.WithFields(log.Fields{
"remoteAddress": connection.RemoteAddr(),
}).Info("Connection to Graphite established.")
}
return connection, err
}
func sendMetric(metrics *[10000]*Metric, connection net.Conn, outputChan chan *Metric, compress bool, slog *log.Entry, id int) {
buffered := 0
sent := 0
returned := 0
connectionAlive := true
var buf bytes.Buffer
compressedBuf := zlib.NewWriter(&buf)
var dataLength int
var err error
for i := 0; i < len(metrics); i++ {
if metrics[i] == emptyMetric {
continue
}
// If connection is dead - just return metrics to outputChan
if connectionAlive == false {
outputChan <- metrics[i]
returned++
continue
}
var metricString string
if metrics[i].Tenant == "" {
metricString = fmt.Sprintf(
"%s%s %s %d\n",
metrics[i].Prefix,
metrics[i].Path,
metrics[i].Value,
metrics[i].Timestamp,
)
} else {
metricString = fmt.Sprintf(
"%s%s %s %d %s\n",
metrics[i].Prefix,
metrics[i].Path,
metrics[i].Value,
metrics[i].Timestamp,
metrics[i].Tenant,
)
}
if compress == true {
dataLength, err = compressedBuf.Write([]byte(metricString))
if err != nil {
slog.WithFields(log.Fields{"error": err}).Error("Compression buffer write error.")
} else {
buffered++
}
} else {
dataLength, err = buf.Write([]byte(metricString))
slog.WithFields(log.Fields{
"bytes": dataLength,
"metric": metricString,
"number": i,
}).Debug("Metric buffered.")
buffered++
}
}
compressedBuf.Close()
// Now the time to dump all buffer into connection
packDataLength, err := buf.WriteTo(connection)
if err != nil {
slog.WithFields(log.Fields{"error": err}).Error("Connection write error.")
atomic.AddInt64(&state.SendError[id], 1)
connectionAlive = false
// Here we must return metric to out outputChan
for i := 0; i < len(metrics); i++ {
if metrics[i] == emptyMetric {
continue
}
outputChan <- metrics[i]
returned += 1
}
} else {
slog.WithFields(log.Fields{
"bytes": packDataLength,
"number": buffered,
}).Debug("Metrics pack sent.")
sent += buffered
}
connection.Close()
atomic.AddInt64(&state.ConnectionAlive[id], -1)
slog.WithFields(log.Fields{
"sent_bytes": packDataLength,
"sent_num": sent,
"returned_num": returned,
}).Info("Pack is finished.")
// Increment per sender counters
atomic.AddInt64(&state.OutBytes[id], int64(packDataLength))
atomic.AddInt64(&state.Out[id], int64(sent))
atomic.AddInt64(&state.Returned[id], int64(returned))
}