This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
gnet.go
157 lines (134 loc) · 3.47 KB
/
gnet.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
package gnet
import (
"bytes"
"crypto/tls"
"fmt"
"net"
"net/http"
"time"
"github.com/grafana/metrictank/cmd/mt-fakemetrics/out"
"github.com/grafana/metrictank/schema"
"github.com/grafana/metrictank/schema/msg"
"github.com/jpillora/backoff"
"github.com/raintank/met"
"github.com/raintank/worldping-api/pkg/log"
)
type Msg struct {
data []byte
num int // metrics contained within
}
type Gnet struct {
out.OutStats
url string
key string
bearer string
client *http.Client
bufSize int // amount of messages we can buffer up before providing backpressure.
timeout time.Duration
sslVerify bool
queue chan Msg
}
func New(url, key string, stats met.Backend) (*Gnet, error) {
bufSize := 100
timeout := 3 * time.Second
sslVerify := false
gnet := &Gnet{
OutStats: out.NewStats(stats, "gnet"),
url: url,
key: key,
bearer: fmt.Sprintf("Bearer " + key),
client: &http.Client{
Timeout: timeout,
},
bufSize: bufSize,
timeout: timeout,
sslVerify: sslVerify,
queue: make(chan Msg, bufSize),
}
if !sslVerify {
// this transport should be the equivalent of Go's DefaultTransport
gnet.client.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
// except for this
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
go gnet.run()
return gnet, nil
}
func (g *Gnet) Close() error {
return nil
}
func (g *Gnet) Flush(metrics []*schema.MetricData) error {
if len(metrics) == 0 {
g.FlushDuration.Value(0)
return nil
}
preFlush := time.Now()
log.Debug("gnet asked to publish %d metrics at ts %s", len(metrics), time.Unix(metrics[0].Time, 0))
mda := schema.MetricDataArray(metrics)
data, err := msg.CreateMsg(mda, 0, msg.FormatMetricDataArrayMsgp)
if err != nil {
panic(err)
}
g.PublishQueued.Inc(int64(len(metrics)))
g.queue <- Msg{data, len(metrics)}
g.FlushDuration.Value(time.Since(preFlush))
return nil
}
func (g *Gnet) run() {
for m := range g.queue {
g.PublishQueued.Dec(int64(m.num))
prePub := time.Now()
g.publish(m)
g.PublishDuration.Value(time.Since(prePub))
}
}
func (g *Gnet) publish(m Msg) {
b := &backoff.Backoff{
Min: 100 * time.Millisecond,
Max: time.Minute,
Factor: 1.5,
Jitter: true,
}
for {
g.MessageBytes.Value(int64(len(m.data)))
g.MessageMetrics.Value(int64(m.num))
pre := time.Now()
req, err := http.NewRequest("POST", g.url, bytes.NewBuffer(m.data))
if err != nil {
panic(err)
}
req.Header.Add("Authorization", g.bearer)
req.Header.Add("Content-Type", "rt-metric-binary")
resp, err := g.client.Do(req)
diff := time.Since(pre)
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
log.Info("GrafanaNet sent %d metrics in %s -msg size %d", m.num, diff, len(m.data))
b.Reset()
resp.Body.Close()
g.PublishedMetrics.Inc(int64(m.num))
g.PublishedMessages.Inc(1)
break
}
g.PublishErrors.Inc(1)
dur := b.Duration()
if err != nil {
log.Warn("GrafanaNet failed to submit data: %s will try again in %s (this attempt took %s)", err, dur, diff)
} else {
buf := make([]byte, 300)
n, _ := resp.Body.Read(buf)
log.Warn("GrafanaNet failed to submit data: http %d - %s: %s will try again in %s (this attempt took %s)", resp.StatusCode, resp.Status, buf[:n], dur, diff)
resp.Body.Close()
}
time.Sleep(dur)
}
}