-
Notifications
You must be signed in to change notification settings - Fork 27
/
influxdb.go
75 lines (67 loc) · 2.15 KB
/
influxdb.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
package main
import (
"context"
"log"
"time"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
influxdb2write "github.com/influxdata/influxdb-client-go/v2/api/write"
)
// InfluxdbAsyncBatchSize influxdb asynchronize batch size
const InfluxdbAsyncBatchSize = 20
// InfluxdbClient is a client to connect to influxdb in influx cloud
type InfluxdbClient struct {
Bucket string
Organization string
AccessToken string
InfluxCloudURL string
Client influxdb2.Client
}
// NewInfluxdbClient Create a new InfluxClient
func NewInfluxdbClient(config InfluxdbConfig) *InfluxdbClient {
c := new(InfluxdbClient)
c.Bucket = config.Bucket
c.AccessToken = config.AccessToken
c.InfluxCloudURL = config.InfluxdbURL
c.Client = influxdb2.NewClientWithOptions(c.InfluxCloudURL, c.AccessToken,
influxdb2.DefaultOptions().SetBatchSize(InfluxdbAsyncBatchSize))
return c
}
// PrepareInfluxdbData prepare influxdb datapoint form PingResult
func (i *InfluxdbClient) PrepareInfluxdbData(r PingResult) *influxdb2write.Point {
return influxdb2.NewPoint(r.Cluster,
map[string]string{},
map[string]interface{}{
"hostname": r.Hostname,
"compute_unit_price": int64(r.ComputeUnitPrice),
"request_compute_unit": int64(r.RequestComputeUnits),
"submit": r.Submitted,
"confirmed": r.Confirmed,
"loss": r.Loss,
"max": r.Max,
"min": r.Min,
"mean": r.Mean,
"stddev": r.Stddev,
"take_time": r.TakeTime,
"error": r.Error,
},
time.Now())
}
// SendDatapointAsync send datapoint to influx cloud
func (i *InfluxdbClient) SendDatapointAsync(p *influxdb2write.Point) {
if i.Client == nil {
log.Println("ERROR! InfluxClient has not initiated!")
return
}
go func() {
writeAPI := i.Client.WriteAPIBlocking(i.Organization, i.Bucket)
err := writeAPI.WritePoint(context.Background(), p)
if err != nil {
log.Println("Influxdb write point ERROR! ", err)
}
writeAPI.Flush(context.Background())
}()
}
// ClientClose close Client connection
func (i *InfluxdbClient) ClientClose() {
i.Client.Close()
}