/
InfluxDBWriter08.go
159 lines (135 loc) · 4.69 KB
/
InfluxDBWriter08.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
// Copyright 2015-2018 trivago N.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package producer
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/trivago/gollum/core"
"github.com/trivago/tgo/tio"
)
// influxDBWriter08 implements the io.Writer interface for InfluxDB 0.9 connections
type influxDBWriter08 struct {
client http.Client
Control func() chan<- core.PluginControl
buffer tio.ByteStream
host string
username string
password string
writeURL string
testURL string
databaseTemplate string
logger logrus.FieldLogger
connectionUp bool
timeBasedDBName bool
}
// Configure sets the database connection values
func (writer *influxDBWriter08) configure(conf core.PluginConfigReader, prod *InfluxDB) error {
writer.host = conf.GetString("Host", "localhost:8086")
writer.username = conf.GetString("User", "")
writer.password = conf.GetString("Password", "")
writer.databaseTemplate = conf.GetString("Database", "default")
writer.buffer = tio.NewByteStream(4096)
writer.connectionUp = false
writer.timeBasedDBName = conf.GetBool("TimeBasedName", true)
writer.Control = prod.Control
writer.logger = prod.Logger
writer.writeURL = fmt.Sprintf("http://%s/db/%%s/series?time_precision=ms", writer.host)
writer.testURL = fmt.Sprintf("http://%s/db", writer.host)
if writer.username != "" {
credentials := fmt.Sprintf("u=%s&p=%s", url.QueryEscape(writer.username), url.QueryEscape(writer.password))
writer.writeURL += "&" + credentials
writer.testURL += "?" + credentials
}
return conf.Errors.OrNil()
}
func (writer *influxDBWriter08) isConnectionUp() bool {
if writer.connectionUp {
return true // ### return, connection not reported to be down ###
}
if response, err := http.Get(writer.testURL); err == nil && response != nil {
defer response.Body.Close()
switch response.Status[:3] {
case "200":
writer.connectionUp = true
writer.logger.Info("Connected to " + writer.host)
}
}
return writer.connectionUp
}
func (writer *influxDBWriter08) createDatabase(database string) error {
url := fmt.Sprintf("http://%s/db?u=%s&p=%s", writer.host, url.QueryEscape(writer.username), url.QueryEscape(writer.password))
body := fmt.Sprintf(`{"name": "%s"}`, database)
response, err := writer.client.Post(url, "application/json", strings.NewReader(body))
if err != nil {
return err
}
defer response.Body.Close()
switch response.Status[:3] {
case "201":
writer.logger.Infof("Created database %s", database)
return nil
default:
body, _ := ioutil.ReadAll(response.Body)
return fmt.Errorf("Could not create database %s with status code \"%s\" and error \"%s\"", database, response.Status[:3], body)
}
}
func (writer *influxDBWriter08) post() (int, error) {
databaseName := writer.databaseTemplate
if writer.timeBasedDBName {
databaseName = time.Now().Format(databaseName)
}
writeURL := fmt.Sprintf(writer.writeURL, url.QueryEscape(databaseName))
response, err := writer.client.Post(writeURL, "application/json", &writer.buffer)
if err != nil {
writer.connectionUp = false
// TBD: health check? (ex-fuse breaker)
return 0, err // ### return, failed to connect ###
}
defer response.Body.Close()
// Check status codes
switch response.Status[:3] {
case "200":
return writer.buffer.Len(), nil // ### return, OK ###
case "400":
body, _ := ioutil.ReadAll(response.Body)
// 400 Bad Request: Database foobar doesn't exist
if strings.Contains(string(body), databaseName) {
err := writer.createDatabase(databaseName)
if err != nil {
return 0, err // ### return, failed to create database ###
}
writer.buffer.ResetRead()
return writer.post() // ### return, retry ###
}
fallthrough
default:
body, _ := ioutil.ReadAll(response.Body)
writer.connectionUp = false
return 0, fmt.Errorf("%s returned %s: %s", writeURL, response.Status, string(body))
}
}
func (writer *influxDBWriter08) Write(data []byte) (int, error) {
data = bytes.TrimRight(data, " \t,") // remove the last comma
writer.buffer.Reset()
writer.buffer.WriteByte('[')
writer.buffer.Write(data)
writer.buffer.WriteByte(']')
return writer.post()
}