-
Notifications
You must be signed in to change notification settings - Fork 4
/
lp.go
385 lines (320 loc) · 7.92 KB
/
lp.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
package lineproto
import (
"bytes"
"fmt"
"reflect"
"sort"
"time"
"unsafe"
"github.com/influxdata/influxdb1-client/models"
lp "github.com/influxdata/line-protocol/v2/lineprotocol"
)
func unsafeBytesToString(runes []byte) string {
return *(*string)(unsafe.Pointer(&runes)) //nolint:gosec
}
// GetSafeString get a copy of string s.
func GetSafeString(s string) string {
if s == "" {
return ""
}
return string(*(*[]byte)(unsafe.Pointer(&s))) //nolint:gosec
}
type Precision = lp.Precision
const (
Nanosecond = lp.Nanosecond
Microsecond = lp.Microsecond
Millisecond = lp.Millisecond
Second = lp.Second
)
// ConvertPrecisionToV2 map version 1 precision to version 2.
func ConvertPrecisionToV2(precision string) (Precision, error) {
switch precision {
case "u":
return Microsecond, nil
case "ms":
return Millisecond, nil
case "s":
return Second, nil
case "m":
return 0, fmt.Errorf("line protocol v2 precision do not support minute")
case "h":
return 0, fmt.Errorf("line protocol v2 precision do not support hour")
default:
return Nanosecond, nil
}
}
type Point struct {
Name string
Tags map[string]string
Fields map[string]interface{}
Time time.Time
}
func NewPoint(name string,
tags map[string]string,
fields map[string]interface{},
t ...time.Time,
) (*Point, error) {
var tm time.Time
if len(t) > 0 {
tm = t[0]
}
return &Point{
Name: name,
Tags: tags,
Fields: fields,
Time: tm,
}, nil
}
func (p *Point) ToInfluxdbPoint(defaultTime time.Time) (models.Point, error) {
tm := p.Time
if tm.IsZero() {
tm = defaultTime
}
return models.NewPoint(p.Name, models.NewTags(p.Tags), p.Fields, tm)
}
func (p *Point) AddTag(key, val string) {
p.Tags[key] = val
}
func (p *Point) AddField(key string, val interface{}) error {
// check the val value
if _, ok := InterfaceToValue(val); !ok {
return fmt.Errorf("unsupported line protocol field interface{}: %T, [%v]", val, val)
}
p.Fields[key] = val
return nil
}
func (p *Point) String() (string, error) {
encoder := NewLineEncoder()
if err := encoder.AppendPoint(p); err != nil {
return "", fmt.Errorf("encoder append point err: %w", err)
}
line, err := encoder.UnsafeStringWithoutLn()
if err != nil {
return "", fmt.Errorf("line protocol encoding err: %w", err)
}
return line, nil
}
func ParseWithOptionSetter(data []byte, optionSetters ...OptionSetter) ([]*Point, error) {
option := DefaultOption
if len(optionSetters) > 0 {
for _, setter := range optionSetters {
setter(option)
}
}
return Parse(data, option)
}
func Parse(data []byte, opt *Option) ([]*Point, error) {
if len(data) == 0 {
return nil, fmt.Errorf("empty data")
}
if opt == nil {
opt = DefaultOption
}
if opt.MaxFields <= 0 {
opt.MaxFields = 1024
}
if opt.MaxTags <= 0 {
opt.MaxTags = 256
}
ptTime := opt.Time
if opt.Time.IsZero() {
ptTime = time.Now()
}
dec := lp.NewDecoderWithBytes(data)
var pts []*Point
for dec.Next() {
pt := &Point{
Tags: make(map[string]string),
Fields: make(map[string]interface{}),
}
m, err := dec.Measurement()
if err != nil {
return pts, fmt.Errorf("parse measurement err: %w", err)
}
pt.Name = unsafeBytesToString(m)
for {
k, v, err := dec.NextTag()
if err != nil {
return pts, fmt.Errorf("read next tag err: %w", err)
}
if k == nil {
break
}
pt.Tags[unsafeBytesToString(k)] = unsafeBytesToString(v)
}
for {
k, v, err := dec.NextField()
if err != nil {
return pts, fmt.Errorf("get next field err: %w", err)
}
if k == nil {
break
}
pt.Fields[unsafeBytesToString(k)] = v.Interface()
}
t, err := dec.Time(opt.PrecisionV2, ptTime)
if err != nil {
return pts, err
}
pt.Time = t
if opt.ExtraTags != nil {
for k, v := range opt.ExtraTags {
if _, ok := pt.Tags[k]; !ok {
pt.AddTag(k, v)
}
}
}
if opt.CallbackV2 != nil {
newPoint, err := opt.CallbackV2(pt)
if err != nil {
return nil, fmt.Errorf("call callbackv2 err: %w", err)
}
pt = newPoint
}
if pt == nil {
return nil, fmt.Errorf("line point is empty")
}
if err := checkPointV2(pt, opt); err != nil {
return nil, err
}
pts = append(pts, pt)
}
return pts, nil
}
func InterfaceToValue(x interface{}) (lp.Value, bool) {
switch y := x.(type) {
case int64, uint64, float64, bool, string, []byte:
// do nothing
case int:
x = int64(y)
case rune:
x = int64(y)
case int16:
x = int64(y)
case int8:
x = int64(y)
case uint:
x = uint64(y)
case uint32:
x = int64(y)
case uint16:
x = int64(y)
case byte:
x = int64(y)
case float32:
x = float64(y)
default:
k := reflect.ValueOf(y).Kind()
if k == reflect.Slice || k == reflect.Map || k == reflect.Array ||
k == reflect.Struct || k == reflect.Ptr {
x = fmt.Sprintf("%v", y)
}
}
return lp.NewValue(x)
}
type LineEncoder struct {
Encoder *lp.Encoder
Opt *Option
}
func NewLineEncoder(optionSetters ...OptionSetter) *LineEncoder {
opt := DefaultOption
if len(optionSetters) > 0 {
for _, setter := range optionSetters {
setter(opt)
}
}
encoder := &lp.Encoder{}
encoder.SetPrecision(opt.PrecisionV2)
return &LineEncoder{
Encoder: encoder,
Opt: opt,
}
}
func (le *LineEncoder) EnableLax() {
le.Encoder.SetLax(true)
}
func (le *LineEncoder) AppendPoint(pt *Point) error {
le.Encoder.StartLine(pt.Name)
maxLen := len(pt.Tags)
if len(pt.Fields) > maxLen {
maxLen = len(pt.Fields)
}
keys := make([]string, 0, maxLen)
for key, val := range pt.Tags {
if val == "" {
continue
}
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
le.Encoder.AddTag(key, pt.Tags[key])
}
if len(pt.Fields) == 0 {
return models.ErrPointMustHaveAField
}
keys = keys[:0]
for key := range pt.Fields {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
value, ok := InterfaceToValue(pt.Fields[key])
if !ok {
return fmt.Errorf("unable parse value from interface{}: %T, [%v]", pt.Fields[key], pt.Fields[key])
}
le.Encoder.AddField(key, value)
}
le.Encoder.EndLine(pt.Time)
return le.Encoder.Err()
}
// Bytes return the line protocol bytes
// You should be **VERY CAREFUL** when using this function together with the Reset.
func (le *LineEncoder) Bytes() ([]byte, error) {
return le.Encoder.Bytes(), le.Encoder.Err()
}
// BytesWithoutLn return the line protocol bytes without the trailing new line
// You should be **VERY CAREFUL** when using this function together with the Reset.
func (le *LineEncoder) BytesWithoutLn() ([]byte, error) {
return bytes.TrimRightFunc(le.Encoder.Bytes(), func(r rune) bool {
return r == '\r' || r == '\n'
}), le.Encoder.Err()
}
// UnsafeString return string with no extra allocation
// You should be **VERY CAREFUL** when using this function together with the Reset.
func (le *LineEncoder) UnsafeString() (string, error) {
lineBytes, err := le.Bytes()
if len(lineBytes) > 0 {
return *(*string)(unsafe.Pointer(&lineBytes)), err //nolint:gosec
}
return "", err
}
// UnsafeStringWithoutLn return the line protocol **UNSAFE** string without the trailing new line
// You should be **VERY CAREFUL** when using this function together with the Reset.
func (le *LineEncoder) UnsafeStringWithoutLn() (string, error) {
lineBytes, err := le.BytesWithoutLn()
if len(lineBytes) > 0 {
return *(*string)(unsafe.Pointer(&lineBytes)), err //nolint:gosec
}
return "", err
}
func (le *LineEncoder) Reset() {
le.Encoder.Reset()
}
func (le *LineEncoder) SetBuffer(buf []byte) {
le.Encoder.SetBuffer(buf)
}
func Encode(pts []*Point, opt ...OptionSetter) ([]byte, error) {
enc := NewLineEncoder(opt...)
for _, pt := range pts {
err := enc.AppendPoint(pt)
if err != nil {
return nil, fmt.Errorf("encoder append point err: %w", err)
}
}
return enc.Bytes()
}