forked from chrissnell/remoteweather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_influxdb.go
130 lines (109 loc) · 3.35 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
package main
import (
"context"
"fmt"
"log"
"sync"
"github.com/influxdata/influxdb/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"`
}
// InfluxDBStorage holds the configuration for a InfluxDB storage backend
type InfluxDBStorage struct {
InfluxDBConn client.Client
DBName string
}
// StartStorageEngine creates a goroutine loop to receive readings and send
// them off to InfluxDB
func (i InfluxDBStorage) StartStorageEngine(ctx context.Context, wg *sync.WaitGroup) chan<- Reading {
readingChan := make(chan Reading, 10)
go i.processMetrics(ctx, wg, readingChan)
return readingChan
}
func (i InfluxDBStorage) processMetrics(ctx context.Context, wg *sync.WaitGroup, rchan <-chan Reading) {
wg.Add(1)
defer wg.Done()
for {
select {
case r := <-rchan:
err := i.StoreReading(r)
if err != nil {
log.Println(err)
}
case <-ctx.Done():
log.Println("Cancellation request recieved. Cancelling readings processor.")
return
}
}
}
// StoreReading stores a reading value in InfluxDB
func (i InfluxDBStorage) StoreReading(r Reading) error {
fields := r.ToMap()
// Set the tags for this data point
tags := map[string]string{"station": r.StationName}
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: i.DBName,
Precision: "s",
})
pt, err := client.NewPoint("wx_reading", tags, fields, r.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
}
// NewInfluxDBStorage sets up a new Graphite storage backend
func NewInfluxDBStorage(c *Config) (InfluxDBStorage, error) {
var err error
i := InfluxDBStorage{}
i.DBName = c.Storage.InfluxDB.Database
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{}, err
}
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{}, err
}
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{}, err
}
}
return i, nil
}