-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinfluxdb_output.go
316 lines (274 loc) · 6.6 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package influxdb
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/mozilla-services/heka/message"
. "github.com/mozilla-services/heka/pipeline"
hekaTcp "github.com/mozilla-services/heka/plugins/tcp"
)
type InfluxDBOutputConfig struct {
Server string
Database string
Username string
Password string
ResponseHeaderTimeout uint `toml:"response_header_timeout"`
Tls hekaTcp.TlsConfig
Series string
UseHekaTimestamp bool `toml:"use_heka_timestamp"`
FieldMap map[string]string `toml:"field_map"`
FlushInterval uint `toml:"flush_interval"`
FlushCount uint `toml:"flush_count"`
}
type InfluxDBOutput struct {
seriesUrl string
httpClient *http.Client
flushInterval time.Duration
flushCount uint
series string
seriesField string
columns []string
columnFields []string
}
func (o *InfluxDBOutput) ConfigStruct() interface{} {
return &InfluxDBOutputConfig{
Server: "http://localhost:8086",
Username: "root",
Password: "root",
UseHekaTimestamp: true,
FlushInterval: 1000,
FlushCount: 10,
ResponseHeaderTimeout: 30,
}
}
func (o *InfluxDBOutput) Init(config interface{}) error {
conf := config.(*InfluxDBOutputConfig)
serverUrl, err := url.Parse(conf.Server)
if err != nil {
return fmt.Errorf("Unable to parse InfluxDB server URL (%s): %s", conf.Server, err)
}
switch strings.ToLower(serverUrl.Scheme) {
case "http", "https":
serverUrl.Path = path.Join(serverUrl.Path, "db", url.QueryEscape(conf.Database), "series")
vals := serverUrl.Query()
vals.Set("time_precision", "u") // microseconds
if conf.Username != "" {
vals.Set("u", conf.Username)
}
if conf.Password != "" {
vals.Set("p", conf.Password)
}
serverUrl.RawQuery = vals.Encode()
default:
return fmt.Errorf("Unable to handle server URL scheme %r", serverUrl.Scheme)
}
o.seriesUrl = serverUrl.String()
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
ResponseHeaderTimeout: time.Duration(conf.ResponseHeaderTimeout) * time.Second,
}
if serverUrl.Scheme == "https" {
tlsConf, err := hekaTcp.CreateGoTlsConfig(&conf.Tls)
if err != nil {
return fmt.Errorf("Error settings TLS configuration: %s", err)
}
transport.TLSClientConfig = tlsConf
}
o.httpClient = &http.Client{Transport: transport}
if conf.Series == "" && conf.FieldMap["series"] == "" {
return fmt.Errorf("Must set series or field_map.series")
}
o.series = conf.Series
if conf.UseHekaTimestamp {
_, hasTime := conf.FieldMap["time"]
if !hasTime {
o.columns = []string{"time"}
o.columnFields = []string{"_hekaTimestampMicro"}
}
}
for column, field := range conf.FieldMap {
if column == "series" {
o.seriesField = field
continue
}
o.columns = append(o.columns, column)
o.columnFields = append(o.columnFields, field)
}
o.flushInterval = time.Duration(conf.FlushInterval) * time.Millisecond
o.flushCount = conf.FlushCount
return nil
}
type point struct {
series string
values []interface{}
}
func (o *InfluxDBOutput) Run(or OutputRunner, h PluginHelper) error {
chanSize := 10
flushChan := make(chan []point, chanSize)
recycleChan := make(chan []point, chanSize)
go o.flusher(or, flushChan, recycleChan)
var pack *PipelinePack
inChan := or.InChan()
columnCount := len(o.columns)
msgMax := int(o.flushCount)
flushTimer := time.NewTimer(0)
running := true
for running {
msgCount := 0
// Use a recycled []point if available
var points []point
select {
case points = <-recycleChan:
points = points[:msgMax]
default:
points = make([]point, msgMax)
}
flushTimer.Reset(o.flushInterval)
BatchLoop:
for running {
select {
case pack, running = <-inChan:
if !running {
break BatchLoop
}
pt := &points[msgCount]
if pt.values == nil {
pt.values = make([]interface{}, columnCount)
}
err := o.process(pt, pack.Message)
pack.Recycle()
if err == nil {
msgCount += 1
} else {
or.LogError(err)
}
// flush_count
if msgCount == msgMax {
break BatchLoop
}
case <-flushTimer.C:
// flush_interval
if msgCount > 0 {
break BatchLoop
} else {
flushTimer.Reset(o.flushInterval)
}
}
}
flushChan <-points[:msgCount]
}
close(flushChan)
return nil
}
func (o *InfluxDBOutput) process(pt *point, msg *message.Message) error {
var series *string
if o.seriesField != "" {
series, _ = getField(msg, o.seriesField).(*string)
}
if series == nil {
pt.series = o.series
} else {
pt.series = *series
}
if pt.series == "" {
return fmt.Errorf("series_field %s not found", o.seriesField)
}
columnCount := len(o.columns)
for i := 0; i < columnCount; i++ {
pt.values[i] = getField(msg, o.columnFields[i])
}
return nil
}
func (o *InfluxDBOutput) flusher(or OutputRunner, flushChan, recycleChan chan []point) {
buf := &bytes.Buffer{}
for pts := range flushChan {
r, w := io.Pipe()
go func() {
buf.Reset()
jenc := json.NewEncoder(buf)
buf.Write([]byte{'['})
var firstPoint bool
var lastSeries string
for i, pt := range pts {
if pt.series != lastSeries {
if i != 0 {
buf.Write([]byte(`]},`))
buf.WriteTo(w)
}
buf.Write([]byte(`{"name":`))
jenc.Encode(pt.series)
buf.Write([]byte(`,"columns":`))
jenc.Encode(o.columns)
buf.Write([]byte(`,"points":[`))
lastSeries = pt.series
firstPoint = true
}
if firstPoint {
firstPoint = false
} else {
buf.Write([]byte{','})
}
jenc.Encode(pt.values)
}
buf.Write([]byte(`]}]`))
buf.WriteTo(w)
err := w.Close()
if err != nil {
or.LogError(err)
}
// We're done with pts; release it back to the main loop
select {
case recycleChan <- pts:
default: // No room in the channel; let pts get GCed
}
}()
resp, err := o.httpClient.Post(o.seriesUrl, "application/json", r)
if err == nil {
resp.Body.Close()
} else {
or.LogError(err)
}
}
}
func getField(msg *message.Message, name string) interface{} {
switch name {
case "Uuid":
if msg.Uuid == nil {
return nil
}
return msg.GetUuidString()
case "Timestamp":
return msg.Timestamp
case "Type":
return msg.Type
case "Logger":
return msg.Logger
case "Severity":
return msg.Severity
case "Payload":
return msg.Payload
case "EnvVersion":
return msg.EnvVersion
case "Pid":
return msg.Pid
case "Hostname":
return msg.Hostname
case "_hekaTimestampMicro":
if msg.Timestamp != nil {
return *msg.Timestamp / 1000 // nano -> micro
}
return nil
default:
val, _ := msg.GetFieldValue(name)
return val
}
}
func init() {
RegisterPlugin("InfluxDBOutput", func() interface{} {return new(InfluxDBOutput)})
}