This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
forked from abramovic/logrus_influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogrus_influxdb.go
213 lines (179 loc) · 5.13 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
package logrus_influxdb
import (
"context"
"fmt"
"github.com/influxdata/influxdb-client-go/v2/api"
"os"
"time"
influxdb "github.com/influxdata/influxdb-client-go/v2"
"github.com/sirupsen/logrus"
)
var (
defaultHost = "localhost"
defaultPort = 8086
defaultBucket = "logrus"
defaultBatchIntervalMs = uint(5000)
defaultMeasurement = "logrus"
defaultBatchCount = uint(200)
defaultPrecision = time.Nanosecond
defaultSyslog = false
)
// InfluxDBHook delivers logs to an InfluxDB cluster.
type InfluxDBHook struct {
client influxdb.Client
writeAPI api.WriteAPI
precision time.Duration
org, bucket, measurement string
tagList []string
lastBatchUpdate time.Time
batchIntervalMs uint
batchCount uint
syslog bool
facility string
facilityCode int
appName string
version string
minLevel string
ErrorChannel <-chan error
}
// 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 = newInfluxDBClient(config)
} 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))
}
ready, err := client.Ready(context.Background())
if !ready || err != nil {
return nil, fmt.Errorf("client is not available. ready=%v,err=%v", ready, err)
}
writeAPI := client.WriteAPI(config.Organization, config.Bucket)
hook = &InfluxDBHook{
client: client,
bucket: config.Bucket,
org: config.Organization,
measurement: config.Measurement,
tagList: config.Tags,
batchIntervalMs: config.BatchIntervalMs,
batchCount: config.BatchCount,
precision: config.Precision,
syslog: config.Syslog,
facility: config.Facility,
facilityCode: config.FacilityCode,
appName: config.AppName,
version: config.Version,
minLevel: config.MinLevel,
writeAPI: writeAPI,
ErrorChannel: writeAPI.Errors(),
}
return hook, nil
}
func (hook *InfluxDBHook) Close() {
hook.writeAPI.Flush()
hook.client.Close()
}
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)
}
}
}
hook.writeAPI.WritePoint(influxdb.NewPoint(measurement, tags, data, entry.Time))
}
return nil
}