forked from Super-long/influxdb-comparisons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
293 lines (254 loc) · 6.47 KB
/
core.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
package report
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/url"
"sync"
"time"
"crypto/tls"
"github.com/valyala/fasthttp"
)
type valueKind byte
const (
invalidValueKind valueKind = iota
float64ValueKind
int64ValueKind
boolValueKind
)
// Tag represents an InfluxDB tag.
// Users should prefer to keep the strings as long-lived items.
type Tag struct {
Key, Value string
}
func (t *Tag) Serialize(w io.Writer) {
fmt.Fprintf(w, "%s=%s", t.Key, t.Value)
}
// Field represents an InfluxDB field.
// Users should prefer to keep the strings as long-lived items.
//
// Implementor's note: this type uses more memory than is ideal, but it avoids
// unsafe and it avoids reflection.
type Field struct {
key string
int64Value int64
float64Value float64
boolValue bool
mode valueKind
}
func (f *Field) SetFloat64(key string, x float64) {
if f.mode != invalidValueKind {
panic("logic error: Field already has a value")
}
f.key = key
f.float64Value = x
f.mode = float64ValueKind
}
func (f *Field) SetInt64(key string, x int64) {
if f.mode != invalidValueKind {
panic("logic error: Field already has a value")
}
f.key = key
f.int64Value = x
f.mode = int64ValueKind
}
func (f *Field) SetBool(key string, x bool) {
if f.mode != invalidValueKind {
panic("logic error: Field already has a value")
}
f.key = key
f.boolValue = x
f.mode = boolValueKind
}
func (f *Field) Serialize(w io.Writer) {
if f.mode == int64ValueKind {
fmt.Fprintf(w, "%s=%di", f.key, f.int64Value)
} else if f.mode == float64ValueKind {
fmt.Fprintf(w, "%s=%f", f.key, f.float64Value)
} else {
fmt.Fprintf(w, "%s=%v", f.key, f.boolValue)
}
}
// Point wraps an InfluxDB point data.
// Its primary purpose is to be serialized out to a []byte.
// Users should prefer to keep the strings as long-lived items.
type Point struct {
Measurement string
Tags []Tag
Fields []Field
TimestampNano int64
}
func (p *Point) Init(m string, ts int64) {
p.Measurement = m
p.TimestampNano = ts
}
func (p *Point) AddTag(k, v string) {
p.Tags = append(p.Tags, Tag{Key: k, Value: v})
}
func (p *Point) AddInt64Field(k string, x int64) {
f := Field{}
f.SetInt64(k, x)
p.Fields = append(p.Fields, f)
}
func (p *Point) AddIntField(k string, x int) {
f := Field{}
f.SetInt64(k, int64(x))
p.Fields = append(p.Fields, f)
}
func (p *Point) AddBoolField(k string, x bool) {
f := Field{}
f.SetBool(k, x)
p.Fields = append(p.Fields, f)
}
func (p *Point) AddFloat64Field(k string, x float64) {
f := Field{}
f.SetFloat64(k, x)
p.Fields = append(p.Fields, f)
}
func (p *Point) Serialize(w io.Writer) {
fmt.Fprintf(w, "%s", p.Measurement)
for i, tag := range p.Tags {
if i == 0 {
fmt.Fprint(w, ",")
}
tag.Serialize(w)
if i < len(p.Tags)-1 {
fmt.Fprint(w, ",")
}
}
for i, field := range p.Fields {
if i == 0 {
fmt.Fprint(w, " ")
}
field.Serialize(w)
if i < len(p.Fields)-1 {
fmt.Fprint(w, ",")
}
}
if p.TimestampNano > 0 {
fmt.Fprintf(w, " %d", p.TimestampNano)
}
}
func (p *Point) Reset() {
p.Measurement = ""
p.Tags = p.Tags[:0]
p.Fields = p.Fields[:0]
p.TimestampNano = 0
}
var GlobalPointPool *sync.Pool = &sync.Pool{New: func() interface{} { return &Point{} }}
func GetPointFromGlobalPool() *Point {
return GlobalPointPool.Get().(*Point)
}
func PutPointIntoGlobalPool(p *Point) {
p.Reset()
GlobalPointPool.Put(p)
}
type Collector struct {
Points []*Point
client *fasthttp.Client
writeUri string
baseUri string
encodedBasicAuth string
dbName string
//1,2
influxDBVersion int
buf *bytes.Buffer
}
func NewCollector(influxhost, dbname, userName, password string) *Collector {
encodedBasicAuth := ""
if userName != "" {
encodedBasicAuth = "Basic " + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", userName, password)))
}
return &Collector{
buf: new(bytes.Buffer),
Points: make([]*Point, 0, 0),
client: &fasthttp.Client{
Name: "collector",
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
MaxIdleConnDuration: 90 * time.Second,
},
baseUri: influxhost,
writeUri: influxhost + "/write?db=" + url.QueryEscape(dbname),
encodedBasicAuth: encodedBasicAuth,
dbName: dbname,
influxDBVersion: 1,
}
}
func NewCollectorV2(influxhost, orgId, bucketId, authToken string) *Collector {
return &Collector{
buf: new(bytes.Buffer),
Points: make([]*Point, 0, 0),
client: &fasthttp.Client{
Name: "collector",
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
MaxIdleConnDuration: 90 * time.Second,
},
baseUri: influxhost,
writeUri: influxhost + "/api/v2/write?org=" + orgId + "&bucket=" + bucketId,
encodedBasicAuth: authToken,
dbName: bucketId,
influxDBVersion: 2,
}
}
func (c *Collector) Put(p *Point) {
c.Points = append(c.Points, p)
}
func (c *Collector) Reset() {
c.Points = c.Points[:0]
c.buf.Reset()
}
func (c *Collector) PrepBatch() {
for _, p := range c.Points {
p.Serialize(c.buf)
fmt.Fprint(c.buf, "\n")
}
}
func (c *Collector) CreateDatabase() error {
var err error
err = nil
if c.influxDBVersion == 1 {
req := fasthttp.AcquireRequest()
req.Header.SetMethod("POST")
req.Header.SetRequestURI(c.baseUri + "/query?q=create%20database%20" + url.QueryEscape(c.dbName))
if c.encodedBasicAuth != "" {
req.Header.Set("Authorization", c.encodedBasicAuth)
}
req.SetBody(c.buf.Bytes())
// Perform the request while tracking latency:
resp := fasthttp.AcquireResponse()
err = c.client.Do(req, resp)
if resp.StatusCode() != fasthttp.StatusOK {
return fmt.Errorf("collector error: unexpected status code %d", resp.StatusCode())
}
fasthttp.ReleaseResponse(resp)
fasthttp.ReleaseRequest(req)
}
return err
}
func (c *Collector) SendBatch() error {
req := fasthttp.AcquireRequest()
req.Header.SetMethod("POST")
req.Header.SetRequestURI(c.writeUri)
if c.encodedBasicAuth != "" {
if c.influxDBVersion == 1 {
req.Header.Set("Authorization", c.encodedBasicAuth)
} else {
req.Header.Add("Authorization", fmt.Sprintf("Token %s", c.encodedBasicAuth))
}
}
req.SetBody(c.buf.Bytes())
// Perform the request while tracking latency:
resp := fasthttp.AcquireResponse()
err := c.client.Do(req, resp)
if resp.StatusCode() != fasthttp.StatusNoContent && resp.StatusCode() != fasthttp.StatusOK {
return fmt.Errorf("collector error: unexpected status code %d: %s", resp.StatusCode(), resp.Body())
}
fasthttp.ReleaseResponse(resp)
fasthttp.ReleaseRequest(req)
return err
}