forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
influx.go
341 lines (284 loc) · 7.47 KB
/
influx.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package influx
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"math"
"sort"
"strconv"
"strings"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/serializers"
)
const (
MaxInt64 = int64(^uint64(0) >> 1)
NeedMoreSpace = "need more space"
InvalidName = "invalid name"
NoFields = "no serializable fields"
)
// MetricError is an error causing an entire metric to be unserializable.
type MetricError struct {
series string
reason string
}
func (e MetricError) Error() string {
if e.series != "" {
return fmt.Sprintf("%q: %s", e.series, e.reason)
}
return e.reason
}
// FieldError is an error causing a field to be unserializable.
type FieldError struct {
reason string
}
func (e FieldError) Error() string {
return e.reason
}
// Serializer is a serializer for line protocol.
type Serializer struct {
MaxLineBytes int `toml:"influx_max_line_bytes"`
SortFields bool `toml:"influx_sort_fields"`
UintSupport bool `toml:"influx_uint_support"`
bytesWritten int
buf bytes.Buffer
header []byte
footer []byte
pair []byte
}
func (s *Serializer) Init() error {
s.header = make([]byte, 0, 50)
s.footer = make([]byte, 0, 21)
s.pair = make([]byte, 0, 50)
return nil
}
// Serialize writes the telegraf.Metric to a byte slice. May produce multiple
// lines of output if longer than maximum line length. Lines are terminated
// with a newline (LF) char.
func (s *Serializer) Serialize(m telegraf.Metric) ([]byte, error) {
s.buf.Reset()
err := s.writeMetric(&s.buf, m)
if err != nil {
return nil, err
}
out := make([]byte, 0, s.buf.Len())
return append(out, s.buf.Bytes()...), nil
}
// SerializeBatch writes the slice of metrics and returns a byte slice of the
// results. The returned byte slice may contain multiple lines of data.
func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
s.buf.Reset()
for _, m := range metrics {
err := s.Write(&s.buf, m)
if err != nil {
var mErr *MetricError
if errors.As(err, &mErr) {
continue
}
return nil, err
}
}
out := make([]byte, 0, s.buf.Len())
return append(out, s.buf.Bytes()...), nil
}
func (s *Serializer) Write(w io.Writer, m telegraf.Metric) error {
return s.writeMetric(w, m)
}
func (s *Serializer) writeString(w io.Writer, str string) error {
n, err := io.WriteString(w, str)
s.bytesWritten += n
return err
}
func (s *Serializer) writeBytes(w io.Writer, b []byte) error {
n, err := w.Write(b)
s.bytesWritten += n
return err
}
func (s *Serializer) buildHeader(m telegraf.Metric) error {
s.header = s.header[:0]
name := nameEscape(m.Name())
if name == "" {
return s.newMetricError(InvalidName)
}
s.header = append(s.header, name...)
for _, tag := range m.TagList() {
key := escape(tag.Key)
value := escape(tag.Value)
// Tag keys and values that end with a backslash cannot be encoded by
// line protocol.
if strings.HasSuffix(key, `\`) {
key = strings.TrimRight(key, `\`)
}
if strings.HasSuffix(value, `\`) {
value = strings.TrimRight(value, `\`)
}
// Tag keys and values must not be the empty string.
if key == "" || value == "" {
continue
}
s.header = append(s.header, ',')
s.header = append(s.header, key...)
s.header = append(s.header, '=')
s.header = append(s.header, value...)
}
s.header = append(s.header, ' ')
return nil
}
func (s *Serializer) buildFooter(m telegraf.Metric) {
s.footer = s.footer[:0]
s.footer = append(s.footer, ' ')
s.footer = strconv.AppendInt(s.footer, m.Time().UnixNano(), 10)
s.footer = append(s.footer, '\n')
}
func (s *Serializer) buildFieldPair(key string, value interface{}) error {
s.pair = s.pair[:0]
key = escape(key)
// Some keys are not encodeable as line protocol, such as those with a
// trailing '\' or empty strings.
if key == "" {
return &FieldError{"invalid field key"}
}
s.pair = append(s.pair, key...)
s.pair = append(s.pair, '=')
pair, err := s.appendFieldValue(s.pair, value)
if err != nil {
return err
}
s.pair = pair
return nil
}
func (s *Serializer) writeMetric(w io.Writer, m telegraf.Metric) error {
var err error
err = s.buildHeader(m)
if err != nil {
return err
}
s.buildFooter(m)
if s.SortFields {
sort.Slice(m.FieldList(), func(i, j int) bool {
return m.FieldList()[i].Key < m.FieldList()[j].Key
})
}
pairsLen := 0
firstField := true
for _, field := range m.FieldList() {
err = s.buildFieldPair(field.Key, field.Value)
if err != nil {
log.Printf(
"D! [serializers.influx] could not serialize field %q: %v; discarding field",
field.Key, err)
continue
}
bytesNeeded := len(s.header) + pairsLen + len(s.pair) + len(s.footer)
// Additional length needed for field separator `,`
if !firstField {
bytesNeeded++
}
if s.MaxLineBytes > 0 && bytesNeeded > s.MaxLineBytes {
// Need at least one field per line, this metric cannot be fit
// into the max line bytes.
if firstField {
return s.newMetricError(NeedMoreSpace)
}
err = s.writeBytes(w, s.footer)
if err != nil {
return err
}
pairsLen = 0
firstField = true
bytesNeeded = len(s.header) + len(s.pair) + len(s.footer)
if bytesNeeded > s.MaxLineBytes {
return s.newMetricError(NeedMoreSpace)
}
}
if firstField {
err = s.writeBytes(w, s.header)
if err != nil {
return err
}
} else {
err = s.writeString(w, ",")
if err != nil {
return err
}
}
err = s.writeBytes(w, s.pair)
if err != nil {
return err
}
pairsLen += len(s.pair)
firstField = false
}
if firstField {
return s.newMetricError(NoFields)
}
return s.writeBytes(w, s.footer)
}
func (s *Serializer) newMetricError(reason string) *MetricError {
if len(s.header) != 0 {
series := bytes.TrimRight(s.header, " ")
return &MetricError{series: string(series), reason: reason}
}
return &MetricError{reason: reason}
}
func (s *Serializer) appendFieldValue(buf []byte, value interface{}) ([]byte, error) {
switch v := value.(type) {
case uint64:
if s.UintSupport {
return appendUintField(buf, v), nil
}
if v <= uint64(MaxInt64) {
return appendIntField(buf, int64(v)), nil
}
return appendIntField(buf, MaxInt64), nil
case int64:
return appendIntField(buf, v), nil
case float64:
if math.IsNaN(v) {
return nil, &FieldError{"is NaN"}
}
if math.IsInf(v, 0) {
return nil, &FieldError{"is Inf"}
}
return appendFloatField(buf, v), nil
case string:
return appendStringField(buf, v), nil
case bool:
return appendBoolField(buf, v), nil
default:
return buf, &FieldError{fmt.Sprintf("invalid value type: %T", v)}
}
}
func appendUintField(buf []byte, value uint64) []byte {
return append(strconv.AppendUint(buf, value, 10), 'u')
}
func appendIntField(buf []byte, value int64) []byte {
return append(strconv.AppendInt(buf, value, 10), 'i')
}
func appendFloatField(buf []byte, value float64) []byte {
return strconv.AppendFloat(buf, value, 'f', -1, 64)
}
func appendBoolField(buf []byte, value bool) []byte {
return strconv.AppendBool(buf, value)
}
func appendStringField(buf []byte, value string) []byte {
buf = append(buf, '"')
buf = append(buf, stringFieldEscape(value)...)
buf = append(buf, '"')
return buf
}
func init() {
serializers.Add("influx",
func() serializers.Serializer {
return &Serializer{}
},
)
}
// InitFromConfig is a compatibility function to construct the parser the old way
func (s *Serializer) InitFromConfig(cfg *serializers.Config) error {
s.MaxLineBytes = cfg.InfluxMaxLineBytes
s.SortFields = cfg.InfluxSortFields
s.UintSupport = cfg.InfluxUintSupport
return nil
}