forked from HeavyHorst/fluxrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluxrus.go
184 lines (160 loc) · 3.66 KB
/
fluxrus.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
package fluxrus
import (
"fmt"
"sync"
"time"
influx "github.com/influxdata/influxdb/client/v2"
"github.com/sirupsen/logrus"
)
type InfluxHook struct {
client influx.Client
database string
measurement string
tags []string
precision string
batchSize int
batchInterval time.Duration
batchChan chan *influx.Point
flushChan chan struct{}
flushed chan struct{}
err error
errLock sync.RWMutex
}
func (h *InfluxHook) setError(e error) {
h.errLock.Lock()
h.err = e
h.errLock.Unlock()
}
func ensureDBExists(client influx.Client, db string) error {
response, err := client.Query(influx.Query{
Command: fmt.Sprintf("CREATE DATABASE %s", db),
Database: db,
})
if err != nil {
return err
}
return response.Error()
}
func New(url, db, measurement string, opts ...Option) (*InfluxHook, error) {
hook := &InfluxHook{
database: db,
measurement: measurement,
precision: "ns",
batchSize: 200,
batchInterval: 5 * time.Second,
flushChan: make(chan struct{}),
flushed: make(chan struct{}),
err: nil,
errLock: sync.RWMutex{},
}
for _, o := range opts {
o(hook)
}
// we have no client - create one
if hook.client == nil {
influxClient, err := influx.NewHTTPClient(influx.HTTPConfig{
Addr: url,
})
if err != nil {
return nil, err
}
hook.client = influxClient
}
if err := ensureDBExists(hook.client, db); err != nil {
return nil, err
}
hook.batchChan = make(chan *influx.Point, hook.batchSize)
go func() {
var err error
var batch influx.BatchPoints
ticker := time.NewTicker(hook.batchInterval)
batch, err = influx.NewBatchPoints(influx.BatchPointsConfig{
Database: hook.database,
Precision: hook.precision,
})
if err != nil {
logrus.Errorf("Could not create the InfluxDB batch of points: %v", err)
}
flushAndClear := func() {
err := hook.client.Write(batch)
hook.setError(err)
// only clear the buffer if all data is written to the server
if err == nil {
batch, err = influx.NewBatchPoints(influx.BatchPointsConfig{
Database: hook.database,
Precision: hook.precision,
})
}
}
for {
select {
case <-ticker.C:
flushAndClear()
case p := <-hook.batchChan:
batch.AddPoint(p)
if len(batch.Points()) >= hook.batchSize {
flushAndClear()
}
case <-hook.flushChan:
for p := range hook.batchChan {
batch.AddPoint(p)
}
flushAndClear()
hook.flushed <- struct{}{}
}
}
}()
return hook, nil
}
func (h *InfluxHook) Close() {
h.flushChan <- struct{}{}
close(h.batchChan)
<-h.flushed
}
func (h *InfluxHook) Fire(entry *logrus.Entry) error {
tags := map[string]string{
"level": entry.Level.String(),
}
for _, tag := range h.tags {
if tagValue, ok := getTag(entry.Data, tag); ok {
tags[tag] = tagValue
}
}
fields := map[string]interface{}{
"message": entry.Message,
}
for k, v := range entry.Data {
fields[k] = v
}
for _, tag := range h.tags {
delete(fields, tag)
}
pt, err := influx.NewPoint(h.measurement, tags, fields, entry.Time)
if err != nil {
return fmt.Errorf("Could not create new InfluxDB point: %v", err)
}
h.batchChan <- pt
h.errLock.RLock()
err = h.err
h.errLock.RUnlock()
return err
}
// Levels implementation allows for level logging.
func (h *InfluxHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
}
}
// Helper function.
func getTag(fields logrus.Fields, tag string) (string, bool) {
value, ok := fields[tag]
if ok {
return fmt.Sprintf("%v", value), ok
}
return "", ok
}