-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathext.go
106 lines (99 loc) · 2.41 KB
/
ext.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
// Copyright 2020-2021 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package write
import (
"fmt"
"strconv"
"strings"
"time"
)
// Point extension methods for test
// PointToLineProtocolBuffer creates InfluxDB line protocol string from the Point, converting associated timestamp according to precision
// and write result to the string builder
func PointToLineProtocolBuffer(p *Point, sb *strings.Builder, precision time.Duration) {
escapeKey(sb, p.Name(), false)
sb.WriteRune(',')
for i, t := range p.TagList() {
if i > 0 {
sb.WriteString(",")
}
escapeKey(sb, t.Key, true)
sb.WriteString("=")
escapeKey(sb, t.Value, true)
}
sb.WriteString(" ")
for i, f := range p.FieldList() {
if i > 0 {
sb.WriteString(",")
}
escapeKey(sb, f.Key, true)
sb.WriteString("=")
switch f.Value.(type) {
case string:
sb.WriteString(`"`)
escapeValue(sb, f.Value.(string))
sb.WriteString(`"`)
default:
sb.WriteString(fmt.Sprintf("%v", f.Value))
}
switch f.Value.(type) {
case int64:
sb.WriteString("i")
case uint64:
sb.WriteString("u")
}
}
if !p.Time().IsZero() {
sb.WriteString(" ")
switch precision {
case time.Microsecond:
sb.WriteString(strconv.FormatInt(p.Time().UnixNano()/1000, 10))
case time.Millisecond:
sb.WriteString(strconv.FormatInt(p.Time().UnixNano()/1000000, 10))
case time.Second:
sb.WriteString(strconv.FormatInt(p.Time().Unix(), 10))
default:
sb.WriteString(strconv.FormatInt(p.Time().UnixNano(), 10))
}
}
sb.WriteString("\n")
}
// PointToLineProtocol creates InfluxDB line protocol string from the Point, converting associated timestamp according to precision
func PointToLineProtocol(p *Point, precision time.Duration) string {
var sb strings.Builder
sb.Grow(1024)
PointToLineProtocolBuffer(p, &sb, precision)
return sb.String()
}
func escapeKey(sb *strings.Builder, key string, escapeEqual bool) {
for _, r := range key {
switch r {
case '\n':
sb.WriteString(`\\n`)
continue
case '\r':
sb.WriteString(`\\r`)
continue
case '\t':
sb.WriteString(`\\t`)
continue
case ' ', ',':
sb.WriteString(`\`)
case '=':
if escapeEqual {
sb.WriteString(`\`)
}
}
sb.WriteRune(r)
}
}
func escapeValue(sb *strings.Builder, value string) {
for _, r := range value {
switch r {
case '\\', '"':
sb.WriteString(`\`)
}
sb.WriteRune(r)
}
}