forked from bosun-monitor/bosun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
158 lines (150 loc) · 3.11 KB
/
queue.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
package collect
import (
"bytes"
"compress/gzip"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"bosun.org/metadata"
"bosun.org/opentsdb"
"bosun.org/slog"
)
func queuer() {
for dp := range tchan {
qlock.Lock()
for {
if len(queue) > MaxQueueLen {
slock.Lock()
dropped++
slock.Unlock()
break
}
queue = append(queue, dp)
select {
case dp = <-tchan:
continue
default:
}
break
}
qlock.Unlock()
}
}
// Locks the queue and sends all datapoints. Intended to be used as scollector exits.
func Flush() {
flushData()
metadata.FlushMetadata()
qlock.Lock()
for len(queue) > 0 {
i := len(queue)
if i > BatchSize {
i = BatchSize
}
sending := queue[:i]
queue = queue[i:]
if Debug {
slog.Infof("sending: %d, remaining: %d", i, len(queue))
}
sendBatch(sending)
}
qlock.Unlock()
}
func send() {
for {
qlock.Lock()
if i := len(queue); i > 0 {
if i > BatchSize {
i = BatchSize
}
sending := queue[:i]
queue = queue[i:]
if Debug {
slog.Infof("sending: %d, remaining: %d", i, len(queue))
}
qlock.Unlock()
Sample("collect.post.batchsize", Tags, float64(len(sending)))
sendBatch(sending)
} else {
qlock.Unlock()
time.Sleep(time.Second)
}
}
}
func sendBatch(batch []*opentsdb.DataPoint) {
if Print {
for _, d := range batch {
j, err := d.MarshalJSON()
if err != nil {
slog.Error(err)
}
slog.Info(string(j))
}
recordSent(len(batch))
return
}
now := time.Now()
resp, err := SendDataPoints(batch, tsdbURL)
if err == nil {
defer resp.Body.Close()
}
d := time.Since(now).Nanoseconds() / 1e6
Sample("collect.post.duration", Tags, float64(d))
Add("collect.post.total_duration", Tags, d)
Add("collect.post.count", Tags, 1)
// Some problem with connecting to the server; retry later.
if err != nil || resp.StatusCode != http.StatusNoContent {
if err != nil {
Add("collect.post.error", Tags, 1)
slog.Error(err)
} else if resp.StatusCode != http.StatusNoContent {
Add("collect.post.bad_status", Tags, 1)
slog.Errorln(resp.Status)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
slog.Error(err)
}
if len(body) > 0 {
slog.Error(string(body))
}
}
restored := 0
for _, msg := range batch {
restored++
tchan <- msg
}
d := time.Second * 5
Add("collect.post.restore", Tags, int64(restored))
slog.Infof("restored %d, sleeping %s", restored, d)
time.Sleep(d)
return
}
recordSent(len(batch))
}
func recordSent(num int) {
if Debug {
slog.Infoln("sent", num)
}
slock.Lock()
sent += int64(num)
slock.Unlock()
}
func SendDataPoints(dps []*opentsdb.DataPoint, tsdb string) (*http.Response, error) {
var buf bytes.Buffer
g := gzip.NewWriter(&buf)
if err := json.NewEncoder(g).Encode(dps); err != nil {
return nil, err
}
if err := g.Close(); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", tsdb, &buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Encoding", "gzip")
Add("collect.post.total_bytes", Tags, int64(buf.Len()))
resp, err := client.Do(req)
return resp, err
}