-
Notifications
You must be signed in to change notification settings - Fork 13
/
influxdb_output.go
60 lines (52 loc) · 1.3 KB
/
influxdb_output.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
package main
import (
"fmt"
"time"
"net/url"
"syscall"
influxClient "github.com/influxdb/influxdb/client"
)
func PublishMetricsWithInfluxDB(address string, metrics []*Metric, hostname string) (e error) {
started := time.Now()
var tv syscall.Timeval
syscall.Gettimeofday(&tv)
taken := (int64(tv.Sec)*1e3 + int64(tv.Usec)/1e3)
series := []*influxClient.Series{}
//TODO, test address componets ...
u, err := url.Parse("addmissing://"+address)
if err != nil {
return err
}
p, _ := u.User.Password()
c, err := influxClient.NewClient(&influxClient.ClientConfig{
Username: u.User.Username(),
Password: p,
Host: u.Host,
Database: u.Path[1:],
})
if err != nil {
return err
}
for _, m := range metrics {
columns := []string{"time", "value", "host"}
_points := []interface{}{taken, m.Value, hostname}
for k,v := range m.Tags{
columns = append(columns,k)
_points = append(_points,v)
}
points := [][]interface{}{_points}
series = append(series, &influxClient.Series{
Name: m.Key,
Columns: columns,
Points: points,
})
}
if len(series) > 0 {
if err := c.WriteSeries(series); err != nil {
return err
} else {
fmt.Printf("sent %d metrics in %.06f to influxdb://%s/%s \n", len(series), time.Now().Sub(started).Seconds(),u.Host,u.Path[1:])
}
}
return nil
}