forked from grafana/carbon-relay-ng
/
pickle.go
174 lines (153 loc) · 4.12 KB
/
pickle.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
package input
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"math/big"
"net"
"github.com/graphite-ng/carbon-relay-ng/badmetrics"
"github.com/graphite-ng/carbon-relay-ng/cfg"
"github.com/graphite-ng/carbon-relay-ng/table"
"github.com/graphite-ng/carbon-relay-ng/validate"
ogorek "github.com/kisielk/og-rek"
m20 "github.com/metrics20/go-metrics20/carbon20"
)
type Pickle struct {
config cfg.Config
bad *badmetrics.BadMetrics
table *table.Table
}
func NewPickle(config cfg.Config, addr string, tbl *table.Table, bad *badmetrics.BadMetrics) (net.Listener, error) {
l, err := listen(addr, &Pickle{config, bad, tbl})
if err != nil {
return nil, err
}
return l, nil
}
func (p *Pickle) Handle(c net.Conn) {
defer c.Close()
// TODO c.SetTimeout(60e9)
r := bufio.NewReaderSize(c, 4096)
ReadLoop:
for {
// Note that everything in this loop should proceed as fast as it can
// so we're not blocked and can keep processing
// so the validation, the pipeline initiated via table.Dispatch(), etc
// must never block.
var length uint32
err := binary.Read(r, binary.BigEndian, &length)
if err != nil {
if io.EOF != err {
log.Error("couldn't read payload length: " + err.Error())
}
break
}
lengthTotal := int(length)
lengthRead := 0
payload := make([]byte, lengthTotal, lengthTotal)
for {
tmpLengthRead, err := r.Read(payload[lengthRead:])
if err != nil {
log.Error("couldn't read payload: " + err.Error())
break ReadLoop
}
lengthRead += tmpLengthRead
if lengthRead == lengthTotal {
break
}
if lengthRead > lengthTotal {
log.Error(fmt.Sprintf("expected to read %d bytes, but read %d", length, lengthRead))
break ReadLoop
}
}
decoder := ogorek.NewDecoder(bytes.NewBuffer(payload))
rawDecoded, err := decoder.Decode()
if err != nil {
if io.EOF != err {
log.Error("error reading pickled data " + err.Error())
}
break
}
decoded, ok := rawDecoded.([]interface{})
if !ok {
log.Error(fmt.Sprintf("Unrecognized type %T for pickled data", rawDecoded))
break
}
ItemLoop:
for _, rawItem := range decoded {
numIn.Inc(1)
item, ok := rawItem.([]interface{})
if !ok {
log.Error(fmt.Sprintf("Unrecognized type %T for item", rawItem))
numInvalid.Inc(1)
continue
}
if len(item) != 2 {
log.Error(fmt.Sprintf("item length must be 2, got %d", len(item)))
numInvalid.Inc(1)
continue
}
metric, ok := item[0].(string)
if !ok {
log.Error(fmt.Sprintf("item metric must be a string, got %T", item[0]))
numInvalid.Inc(1)
continue
}
data, ok := item[1].([]interface{})
if !ok {
log.Error(fmt.Sprintf("item data must be an array, got %T", item[1]))
numInvalid.Inc(1)
continue
}
if len(data) != 2 {
log.Error(fmt.Sprintf("item data length must be 2, got %d", len(data)))
numInvalid.Inc(1)
continue
}
var value string
switch data[1].(type) {
case string:
value = data[1].(string)
case uint8, uint16, uint32, uint64, int8, int16, int32, int64:
value = fmt.Sprintf("%d", data[1])
case float32, float64:
value = fmt.Sprintf("%f", data[1])
default:
log.Error(fmt.Sprintf("Unrecognized type %T for value", data[1]))
numInvalid.Inc(1)
continue ItemLoop
}
var timestamp string
switch data[0].(type) {
case string:
timestamp = data[0].(string)
case uint8, uint16, uint32, uint64, int8, int16, int32, int64, (*big.Int):
timestamp = fmt.Sprintf("%d", data[0])
case float32, float64:
timestamp = fmt.Sprintf("%.0f", data[0])
default:
log.Error(fmt.Sprintf("Unrecognized type %T for timestamp", data[0]))
numInvalid.Inc(1)
continue ItemLoop
}
buf := []byte(metric + " " + value + " " + timestamp)
key, _, ts, err := m20.ValidatePacket(buf, p.config.Validation_level_legacy.Level, p.config.Validation_level_m20.Level)
if err != nil {
p.bad.Add(key, buf, err)
numInvalid.Inc(1)
continue
}
if p.config.Validate_order {
err = validate.Ordered(key, ts)
if err != nil {
p.bad.Add(key, buf, err)
numOutOfOrder.Inc(1)
continue
}
}
p.table.Dispatch(buf)
}
}
}