-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlogrus_influxdb.go
286 lines (242 loc) · 7.59 KB
/
logrus_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
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
package logrus_influxdb
import (
"fmt"
"os"
"sync"
"time"
influxdb "github.com/influxdata/influxdb1-client/v2"
"github.com/sirupsen/logrus"
)
var (
defaultHost = "localhost"
defaultPort = 8086
defaultDatabase = "logrus"
defaultBatchInterval = 5 * time.Second
defaultMeasurement = "logrus"
defaultBatchCount = 200
defaultPrecision = "ns"
defaultSyslog = false
)
// InfluxDBHook delivers logs to an InfluxDB cluster.
type InfluxDBHook struct {
sync.Mutex // TODO: we should clean up all of these locks
client influxdb.Client
precision, database, measurement string
tagList []string
batchP influxdb.BatchPoints
lastBatchUpdate time.Time
batchInterval time.Duration
batchCount int
syslog bool
facility string
facilityCode int
appName string
version string
minLevel string
}
// NewInfluxDB returns a new InfluxDBHook.
func NewInfluxDB(config *Config, clients ...influxdb.Client) (hook *InfluxDBHook, err error) {
if config == nil {
config = &Config{}
}
config.defaults()
var client influxdb.Client
if len(clients) == 0 {
client, err = hook.newInfluxDBClient(config)
if err != nil {
return nil, fmt.Errorf("NewInfluxDB: Error creating InfluxDB Client, %v", err)
}
} else if len(clients) == 1 {
client = clients[0]
} else {
return nil, fmt.Errorf("NewInfluxDB: Error creating InfluxDB Client, %d is too many influxdb clients", len(clients))
}
// Make sure that we can connect to InfluxDB
_, _, err = client.Ping(5 * time.Second) // if this takes more than 5 seconds then influxdb is probably down
if err != nil {
return nil, fmt.Errorf("NewInfluxDB: Error connecting to InfluxDB, %v", err)
}
hook = &InfluxDBHook{
client: client,
database: config.Database,
measurement: config.Measurement,
tagList: config.Tags,
batchInterval: config.BatchInterval,
batchCount: config.BatchCount,
precision: config.Precision,
syslog: config.Syslog,
facility: config.Facility,
facilityCode: config.FacilityCode,
appName: config.AppName,
version: config.Version,
minLevel: config.MinLevel,
}
err = hook.autocreateDatabase()
if err != nil {
return nil, err
}
go hook.handleBatch()
return hook, nil
}
func parseSeverity(level string) (string, int) {
switch level {
case "info":
return "info", 6
case "error":
return "err", 3
case "debug":
return "debug", 7
case "panic":
return "panic", 0
case "fatal":
return "crit", 2
case "warning":
return "warning", 4
}
return "none", -1
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func (hook *InfluxDBHook) hasMinLevel(level string) bool {
if len(hook.minLevel) > 0 {
if hook.minLevel == "debug" {
return true
}
if hook.minLevel == "info" {
return stringInSlice(level, []string{"info", "warning", "error", "fatal", "panic"})
}
if hook.minLevel == "warning" {
return stringInSlice(level, []string{"warning", "error", "fatal", "panic"})
}
if hook.minLevel == "error" {
return stringInSlice(level, []string{"error", "fatal", "panic"})
}
if hook.minLevel == "fatal" {
return stringInSlice(level, []string{"fatal", "panic"})
}
if hook.minLevel == "panic" {
return level == "panic"
}
return false
}
return true
}
// Fire adds a new InfluxDB point based off of Logrus entry
func (hook *InfluxDBHook) Fire(entry *logrus.Entry) (err error) {
if hook.hasMinLevel(entry.Level.String()) {
measurement := hook.measurement
if result, ok := getTag(entry.Data, "measurement"); ok {
measurement = result
}
tags := make(map[string]string)
data := make(map[string]interface{})
if hook.syslog {
hostname, err := os.Hostname()
if err != nil {
return err
}
severity, severityCode := parseSeverity(entry.Level.String())
tags["appname"] = hook.appName
tags["facility"] = hook.facility
tags["host"] = hostname
tags["hostname"] = hostname
tags["severity"] = severity
data["facility_code"] = hook.facilityCode
data["message"] = entry.Message
data["procid"] = os.Getpid()
data["severity_code"] = severityCode
data["timestamp"] = entry.Time.UnixNano()
data["version"] = hook.version
} else {
// If passing a "message" field then it will be overridden by the entry Message
entry.Data["message"] = entry.Message
// Set the level of the entry
tags["level"] = entry.Level.String()
// getAndDel and getAndDelRequest are taken from https://github.com/evalphobia/logrus_sentry
if logger, ok := getTag(entry.Data, "logger"); ok {
tags["logger"] = logger
}
for k, v := range entry.Data {
data[k] = v
}
for _, tag := range hook.tagList {
if tagValue, ok := getTag(entry.Data, tag); ok {
tags[tag] = tagValue
delete(data, tag)
}
}
}
pt, err := influxdb.NewPoint(measurement, tags, data, entry.Time)
if err != nil {
return fmt.Errorf("Fire: %v", err)
}
return hook.addPoint(pt)
}
return nil
}
func (hook *InfluxDBHook) addPoint(pt *influxdb.Point) (err error) {
hook.Lock()
defer hook.Unlock()
if hook.batchP == nil {
err = hook.newBatchPoints()
if err != nil {
return fmt.Errorf("Error creating new batch: %v", err)
}
}
hook.batchP.AddPoint(pt)
// if the number of batch points are less than the batch size then we don't need to write them yet
if len(hook.batchP.Points()) < hook.batchCount {
return nil
}
return hook.writePoints()
}
// writePoints writes the batched log entries to InfluxDB.
func (hook *InfluxDBHook) writePoints() (err error) {
if hook.batchP == nil {
return nil
}
err = hook.client.Write(hook.batchP)
// Note: the InfluxDB client doesn't give us any good way to determine the reason for
// a failure (bad syntax, invalid type, failed connection, etc.), so there is no
// point in retrying a write. If the write fails, then we're going to clear out the
// batch, just as we would for a successful write.
hook.lastBatchUpdate = time.Now().UTC()
hook.batchP = nil
// Return the write error (if any).
return err
}
// we will periodically flush your points to influxdb.
func (hook *InfluxDBHook) handleBatch() {
if hook.batchInterval == 0 || hook.batchCount == 0 {
// we don't need to process this if the interval is 0
return
}
for {
time.Sleep(hook.batchInterval)
hook.Lock()
hook.writePoints()
hook.Unlock()
}
}
/* BEGIN BACKWARDS COMPATIBILITY */
// NewInfluxDBHook /* DO NOT USE */ creates a hook to be added to an instance of logger and initializes the InfluxDB client
func NewInfluxDBHook(host, database string, tags []string, batching ...bool) (hook *InfluxDBHook, err error) {
if len(batching) == 1 && batching[0] {
return NewInfluxDB(&Config{Host: host, Database: database, Tags: tags}, nil)
}
return NewInfluxDB(&Config{Host: host, Database: database, Tags: tags, BatchCount: 0}, nil)
}
// NewWithClientInfluxDBHook /* DO NOT USE */ creates a hook and uses the provided influxdb client
func NewWithClientInfluxDBHook(host, database string, tags []string, client influxdb.Client, batching ...bool) (hook *InfluxDBHook, err error) {
if len(batching) == 1 && batching[0] {
return NewInfluxDB(&Config{Host: host, Database: database, Tags: tags}, client)
}
return NewInfluxDB(&Config{Host: host, Database: database, Tags: tags, BatchCount: 0}, client)
}
/* END BACKWARDS COMPATIBILITY */