forked from grafana/carbon-relay-ng
/
route_grafananet.go
229 lines (205 loc) · 6.64 KB
/
route_grafananet.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
package main
import (
"bytes"
"fmt"
"github.com/Dieterbe/go-metrics"
"github.com/jpillora/backoff"
"net/http"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/lomik/go-carbon/persister"
"github.com/raintank/raintank-metric/msg"
"github.com/raintank/raintank-metric/schema"
)
type RouteGrafanaNet struct {
baseRoute
addr string
apiKey string
buf chan []byte
schemas persister.WhisperSchemas
bufSize int // amount of messages we can buffer up before providing backpressure. each message is about 100B. so 1e7 is about 1GB.
flushMaxNum int
flushMaxWait time.Duration
numErrFlush metrics.Counter
numOut metrics.Counter // metrics successfully written to our buffered conn (no flushing yet)
durationTickFlush metrics.Timer // only updated after successfull flush
durationManuFlush metrics.Timer // only updated after successfull flush. not implemented yet
tickFlushSize metrics.Histogram // only updated after successfull flush
manuFlushSize metrics.Histogram // only updated after successfull flush. not implemented yet
numBuffered metrics.Gauge
}
// NewRouteGrafanaNet creates a special route that writes to a grafana.net datastore
// We will automatically run the route and the destination
// ignores spool for now
func NewRouteGrafanaNet(key, prefix, sub, regex, addr, apiKey, schemasFile string, spool bool, bufSize, flushMaxNum, flushMaxWait int) (Route, error) {
m, err := NewMatcher(prefix, sub, regex)
if err != nil {
return nil, err
}
schemas, err := persister.ReadWhisperSchemas(schemasFile)
if err != nil {
return nil, err
}
var defaultFound bool
for _, schema := range schemas {
if schema.Pattern.String() == ".*" {
defaultFound = true
}
if len(schema.Retentions) == 0 {
return nil, fmt.Errorf("retention setting cannot be empty")
}
}
if !defaultFound {
// good graphite health (not sure what graphite does if there's no .*
// but we definitely need to always be able to determine which interval to use
return nil, fmt.Errorf("storage-conf does not have a default '.*' pattern")
}
cleanAddr := addrToPath(addr)
r := &RouteGrafanaNet{
baseRoute: baseRoute{sync.Mutex{}, atomic.Value{}, key},
addr: addr,
apiKey: apiKey,
buf: make(chan []byte, bufSize), // takes about 228MB on 64bit
schemas: schemas,
bufSize: bufSize,
flushMaxNum: flushMaxNum,
flushMaxWait: time.Duration(flushMaxWait) * time.Millisecond,
numErrFlush: Counter("dest=" + cleanAddr + ".unit=Err.type=flush"),
numOut: Counter("dest=" + cleanAddr + ".unit=Metric.direction=out"),
durationTickFlush: Timer("dest=" + cleanAddr + ".what=durationFlush.type=ticker"),
durationManuFlush: Timer("dest=" + cleanAddr + ".what=durationFlush.type=manual"),
tickFlushSize: Histogram("dest=" + cleanAddr + ".unit=B.what=FlushSize.type=ticker"),
manuFlushSize: Histogram("dest=" + cleanAddr + ".unit=B.what=FlushSize.type=manual"),
numBuffered: Gauge("dest=" + cleanAddr + ".unit=Metric.what=numBuffered"),
}
r.config.Store(baseRouteConfig{*m, make([]*Destination, 0)})
go r.run()
return r, nil
}
func (route *RouteGrafanaNet) run() {
metrics := make([]*schema.MetricData, 0, route.flushMaxNum)
ticker := time.NewTicker(route.flushMaxWait)
client := &http.Client{
Timeout: time.Duration(2 * time.Second),
}
b := &backoff.Backoff{
Min: 100 * time.Millisecond,
Max: time.Minute,
Factor: 1.5,
Jitter: true,
}
flush := func() {
if len(metrics) == 0 {
return
}
for {
pre := time.Now()
mda := schema.MetricDataArray(metrics)
data, err := msg.CreateMsg(mda, 0, msg.FormatMetricDataArrayMsgp)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", route.addr, bytes.NewBuffer(data))
if err != nil {
panic(err)
}
req.Header.Add("Authorization", "Bearer "+route.apiKey)
req.Header.Add("Content-Type", "rt-metric-binary")
resp, err := client.Do(req)
diff := time.Since(pre)
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
b.Reset()
log.Info("GrafanaNet sent %d metrics in %s -msg size %d", len(metrics), diff, len(data))
route.numOut.Inc(int64(len(metrics)))
route.tickFlushSize.Update(int64(len(data)))
route.durationTickFlush.Update(diff)
metrics = metrics[:0]
resp.Body.Close()
break
}
route.numErrFlush.Inc(1)
dur := b.Duration()
if err != nil {
log.Warning("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.Warning("GrafanaNet failed to submit data: http %d - %s will try again in %s (this attempt took %s)", resp.StatusCode, buf[:n], dur, diff)
resp.Body.Close()
}
time.Sleep(dur)
}
}
for {
select {
case buf := <-route.buf:
route.numBuffered.Dec(1)
md := parseMetric(buf, route.schemas)
if md == nil {
continue
}
md.SetId()
metrics = append(metrics, md)
if len(metrics) == route.flushMaxNum {
flush()
}
case <-ticker.C:
flush()
}
}
}
func parseMetric(buf []byte, schemas persister.WhisperSchemas) *schema.MetricData {
msg := strings.TrimSpace(string(buf))
elements := strings.Fields(msg)
if len(elements) != 3 {
log.Error("RouteGrafanaNet: %q error: need 3 fields", str)
return nil
}
name := elements[0]
val, err := strconv.ParseFloat(elements[1], 64)
if err != nil {
log.Error("RouteGrafanaNet: %q error: %s", str, err.Error())
return nil
}
timestamp, err := strconv.ParseUint(elements[2], 10, 32)
if err != nil {
log.Error("RouteGrafanaNet: %q error: %s", str, err.Error())
return nil
}
s, ok := schemas.Match(name)
if !ok {
panic(fmt.Errorf("couldn't find a schema for %q - this is impossible since we asserted there was a default with patt .*", name))
}
md := schema.MetricData{
Name: name,
Interval: s.Retentions[0].SecondsPerPoint(),
Value: val,
Unit: "",
Time: int64(timestamp),
TargetType: "gauge",
Tags: []string{},
}
return &md
}
func (route *RouteGrafanaNet) Dispatch(buf []byte) {
//conf := route.config.Load().(RouteConfig)
// should return as quickly as possible
log.Info("route %s sending to dest %s: %s", route.key, route.addr, buf)
route.numBuffered.Inc(1)
route.buf <- buf
}
func (route *RouteGrafanaNet) Flush() error {
//conf := route.config.Load().(RouteConfig)
// no-op. Flush() is currently not called by anything.
return nil
}
func (route *RouteGrafanaNet) Shutdown() error {
//conf := route.config.Load().(RouteConfig)
return nil
}
func (route *RouteGrafanaNet) Snapshot() RouteSnapshot {
return makeSnapshot(&route.baseRoute, "GrafanaNet")
}