-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstorage_influxdb.go
164 lines (131 loc) · 4.29 KB
/
storage_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
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
package main
import (
"context"
"fmt"
"log"
"sync"
client "github.com/influxdata/influxdb1-client/v2"
)
// InfluxDBConfig describes the YAML-provided configuration for a InfluxDB
// storage backend
type InfluxDBConfig struct {
Scheme string `yaml:"scheme"`
Host string `yaml:"host"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
Database string `yaml:"database"`
Port int `yaml:"port,omitempty"`
Protocol string `yaml:"protocol,omitempty"`
Namespace string `yaml:"metric-namespace,omitempty"`
}
// InfluxDBStorage holds the configuration for a InfluxDB storage backend
type InfluxDBStorage struct {
Namespace string
InfluxDBConn client.Client
DBName string
}
// StartStorageEngine creates a goroutine loop to receive metrics and send
// them off to Graphite
func (i InfluxDBStorage) StartStorageEngine(ctx context.Context, wg *sync.WaitGroup) (chan<- Metric, chan<- Event) {
// We're going to declare eventChan here but not initialize the channel because Graphite
// storage doesn't support Events, only Metrics. We should never receive an Event on this
// channel and if something mistakenly sends one, the program will panic.
var eventChan chan<- Event
// We *do* support Metrics, so we'll initialize this as a buffered channel
metricChan := make(chan Metric, 10)
// Start processing the metrics we receive
go i.processMetrics(ctx, wg, metricChan)
return metricChan, eventChan
}
func (i InfluxDBStorage) processMetrics(ctx context.Context, wg *sync.WaitGroup, mchan <-chan Metric) {
wg.Add(1)
defer wg.Done()
for {
select {
case m := <-mchan:
err := i.sendMetric(m)
if err != nil {
log.Println(err)
}
case <-ctx.Done():
log.Println("Cancellation request recieved. Cancelling metrics processor.")
return
}
}
}
// sendMetric sends a metric value to InfluxDB
func (i InfluxDBStorage) sendMetric(m Metric) error {
var metricName string
if i.Namespace == "" {
metricName = fmt.Sprintf("crabby.%v", m.Job)
} else {
metricName = fmt.Sprintf("%v.%v", i.Namespace, m.Job)
}
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: i.DBName,
Precision: "s",
})
// Make a map to store the metric name/value pair
values := map[string]interface{}{m.Timing: m.Value}
pt, err := client.NewPoint(metricName, m.Tags, values, m.Timestamp)
if err != nil {
return fmt.Errorf("Could not create data point for InfluxDB: %v", err)
}
bp.AddPoint(pt)
// Write the batch
err = i.InfluxDBConn.Write(bp)
if err != nil {
return fmt.Errorf("Could not write data point to InfluxDB: %v", err)
}
return nil
}
// sendEvent is necessary to implement the StorageEngine interface.
func (i InfluxDBStorage) sendEvent(e Event) error {
var err error
return err
}
// NewInfluxDBStorage sets up a new InfluxDB storage backend
func NewInfluxDBStorage(c ServiceConfig) InfluxDBStorage {
var err error
i := InfluxDBStorage{}
i.DBName = c.Storage.InfluxDB.Database
i.Namespace = c.Storage.InfluxDB.Namespace
if c.Storage.InfluxDB.Protocol != "udp" && c.Storage.InfluxDB.Scheme == "" {
c.Storage.InfluxDB.Scheme = "http"
}
switch c.Storage.InfluxDB.Protocol {
case "http":
url := fmt.Sprintf("%v://%v:%v", c.Storage.InfluxDB.Scheme, c.Storage.InfluxDB.Host, c.Storage.InfluxDB.Port)
i.InfluxDBConn, err = client.NewHTTPClient(client.HTTPConfig{
Addr: url,
Username: c.Storage.InfluxDB.Username,
Password: c.Storage.InfluxDB.Password,
})
if err != nil {
log.Println("Warning: could not create InfluxDB connection!", err)
return InfluxDBStorage{}
}
case "udp":
u := client.UDPConfig{
Addr: fmt.Sprintf("%v:%v", c.Storage.InfluxDB.Host, c.Storage.InfluxDB.Port),
}
i.InfluxDBConn, err = client.NewUDPClient(u)
if err != nil {
log.Println("Warning: could not create InfluxDB connection.", err)
return InfluxDBStorage{}
}
default:
url := fmt.Sprintf("%v://%v:%v", c.Storage.InfluxDB.Scheme, c.Storage.InfluxDB.Host, c.Storage.InfluxDB.Port)
i.InfluxDBConn, err = client.NewHTTPClient(client.HTTPConfig{
Addr: url,
Username: c.Storage.InfluxDB.Username,
Password: c.Storage.InfluxDB.Password,
})
if err != nil {
log.Println("Warning: could not create InfluxDB connection!", err)
return InfluxDBStorage{}
}
}
return i
}