-
Notifications
You must be signed in to change notification settings - Fork 3
/
influx_writer.go
76 lines (59 loc) · 1.23 KB
/
influx_writer.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
package mgobench
import (
"log"
"time"
"github.com/influxdata/influxdb/client/v2"
)
var (
database = "mongo"
username = ""
password = ""
)
type tags map[string]string
type fields map[string]interface{}
type Influxdb struct {
conn client.Client
}
func (client *Influxdb) InsertData(measurement string, tag string, field float64) {
tags := map[string]string{
"mongodb": tag,
}
fields := map[string]interface{}{
"count": field,
}
createMetrics(client.conn, measurement, tags, fields)
}
func NewInfluxClient(config *Config) *Influxdb {
database = config.Influxdb.Database
c, err := client.NewUDPClient(client.UDPConfig{Addr: config.Influxdb.ConnectionString})
if err != nil {
panic(err.Error())
}
if err != nil {
log.Fatalln("Error: ", err)
}
return &Influxdb{conn: c}
}
func createMetrics(c client.Client, measurement string, tags tags, fields fields) {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: database,
Precision: "s",
})
if err != nil {
log.Fatalln("Error: ", err)
}
point, err := client.NewPoint(
measurement,
tags,
fields,
time.Now(),
)
if err != nil {
log.Fatalln("Error: ", err)
}
bp.AddPoint(point)
err = c.Write(bp)
if err != nil {
log.Fatal(err)
}
}