-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinfluxdb.go
More file actions
116 lines (96 loc) · 2.2 KB
/
influxdb.go
File metadata and controls
116 lines (96 loc) · 2.2 KB
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
package main
import (
"log"
"time"
influx "github.com/influxdata/influxdb/client/v2"
)
type InfluxConfig struct {
// influx.UDPConfig
Addr string
PayloadSize int
// influx.BatchPointsConfig
Precision string
Database string
RetentionPolicy string
WriteConsistency string
}
type InfluxDataClient struct {
influx.Client
conf InfluxConfig
udpConf influx.UDPConfig
bpConf influx.BatchPointsConfig
err error
}
func (c *InfluxDataClient) Errored() bool {
return c.err != nil
}
func NewInfluxDataClient(conf InfluxConfig) (*InfluxDataClient, error) {
udpConfig := influx.UDPConfig{Addr: conf.Addr, PayloadSize: conf.PayloadSize}
udpClient, err := influx.NewUDPClient(udpConfig)
bpConfig := influx.BatchPointsConfig{
Database: conf.Database,
RetentionPolicy: conf.RetentionPolicy,
WriteConsistency: conf.WriteConsistency,
}
return &InfluxDataClient{
Client: udpClient,
conf: conf,
udpConf: udpConfig,
bpConf: bpConfig,
err: err,
}, err
}
type InfluxDataTracker struct {
*InfluxDataClient
}
func NewInfluxDataTracker(client *InfluxDataClient) *InfluxDataTracker {
return &InfluxDataTracker{client}
}
func (st *InfluxDataTracker) Track(event *Event) error {
if st.Errored() {
st.InfluxDataClient, err = NewInfluxDataClient(st.InfluxDataClient.conf)
if nil != err {
log.Println(err)
}
}
tags := make(map[string]string)
if event.Properties["Tags"] == nil {
event.Properties["Tags"] = make(map[string]string)
}
for k, v := range event.Properties["Tags"].(map[string]string) {
tags[k] = v
}
fields := make(map[string]interface{})
for k, v := range event.Properties {
if k != "Tags" {
switch v := v.(type) {
case time.Duration:
fields[k] = int64(v / time.Millisecond)
default:
fields[k] = v
}
}
}
for k, v := range tags {
if _, present := fields[k]; present {
fields[k] = v
}
}
point, err := influx.NewPoint(event.Event,
tags,
fields,
time.Now(),
)
if err != nil {
log.Println(err)
return err
}
batchPoints, err := influx.NewBatchPoints(st.InfluxDataClient.bpConf)
if err != nil {
log.Println(err)
return err
}
batchPoints.AddPoint(point)
err = st.Write(batchPoints)
return err
}