-
Notifications
You must be signed in to change notification settings - Fork 1
/
logdna.go
336 lines (311 loc) · 7.95 KB
/
logdna.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package logdna
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/gogap/config"
"github.com/gogap/logrus_mate"
"github.com/sirupsen/logrus"
)
// DefaultIngestURL is the URL of the LogDNA ingestion API endpoint
const DefaultIngestURL = "https://logs.logdna.com/logs/ingest"
// Config is the configuration struct for LogDNAHook
type Config struct {
IngestURL string
APIKey string
Hostname string
MAC string
IP string
App string // NOTE: App and Env are global at the moment
Env string
BufferSize int
QueueSize int
FlushEvery time.Duration
MayDrop bool
LineJSON bool
MessageFormatter logrus.Formatter
// Instrumentation functions, called in a separate goroutine
OnDrop func(entries int)
OnFlush func(entries int, err error)
}
// Hook is a Logrus hook that sends entries to LogDNA
type Hook struct {
Config *Config
c chan *logEntry
wg *sync.WaitGroup
}
type logEntry struct {
Timestamp int64 `json:"timestamp"`
Line string `json:"line"`
Level string `json:"level,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty"`
config *Config
}
// MarshalJSON marshals logEntry to JSON
func (entry *logEntry) MarshalJSON() ([]byte, error) {
type LogEntry logEntry
return json.Marshal(&struct {
App string `json:"app,omitempty"`
Env string `json:"env,omitempty"`
*LogEntry
}{
App: entry.config.App,
Env: entry.config.Env,
LogEntry: (*LogEntry)(entry),
})
}
// Levels returns a list of all supported log levels
func (hook *Hook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
}
}
// Fire queues the message for submission to LogDNA
func (hook *Hook) Fire(entry *logrus.Entry) error {
e := &logEntry{
Timestamp: entry.Time.UnixNano() / 1000000,
Level: strings.ToUpper(entry.Level.String()),
config: hook.Config,
}
message := entry.Message
if hook.Config.MessageFormatter != nil {
formatted, err := hook.Config.MessageFormatter.Format(entry)
if err != nil {
return err
}
message = string(formatted)
}
if hook.Config.LineJSON {
meta := make(map[string]interface{})
for k, v := range entry.Data {
if k == "message" {
// Don't use both "message" and "_message" in your log metadata
k = "_message"
}
meta[k] = v
}
meta["message"] = message
line, err := json.Marshal(meta)
if err != nil {
return err
}
e.Line = string(line)
e.Meta = nil
} else {
meta := entry.Data
if len(meta) == 0 {
// Don't bother sending empty maps
meta = nil
}
e.Line = message
e.Meta = meta
}
if hook.Config.MayDrop {
// May discard the entry if channel's full, but won't block.
select {
case hook.c <- e:
default:
if hook.Config.OnDrop != nil {
go hook.Config.OnDrop(1)
}
}
} else {
// Won't lose data but may block if channel's full.
hook.c <- e
}
return nil
}
// Flush sends all the pending entries to the LogDNA ingestion API
func (hook *Hook) Flush() error {
hook.c <- nil
return nil
}
// Close stops the processing
func (hook *Hook) Close() error {
close(hook.c)
hook.wg.Wait()
return nil
}
func (config *Config) flush(buffer *[]*logEntry) error {
if buffer == nil || len(*buffer) == 0 {
return nil
}
err := config._flush(buffer)
if config.OnFlush != nil {
go config.OnFlush(len(*buffer), err)
}
return err
}
func (config *Config) _flush(buffer *[]*logEntry) error {
body, err := json.Marshal(&struct {
Lines []*logEntry `json:"lines"`
}{
Lines: *buffer,
})
if err != nil {
return err
}
client := &http.Client{} // TODO: Make this user-configurable?
bodyReader := bytes.NewReader(body)
req, err := http.NewRequest("POST", config.IngestURL, bodyReader)
if err != nil {
return err
}
req.SetBasicAuth("", config.APIKey)
req.Header.Set("Content-Type", "application/json")
q := req.URL.Query()
q.Add("hostname", config.Hostname)
q.Add("now", fmt.Sprintf("%d", time.Now().UnixNano()/1000000))
if config.MAC != "" {
q.Add("mac", config.MAC)
}
if config.IP != "" {
q.Add("ip", config.IP)
}
req.URL.RawQuery = q.Encode()
response, err := client.Do(req)
if err != nil {
return err
}
defer func() {
_ = response.Body.Close()
}()
if response.StatusCode > 299 || response.StatusCode < 200 {
return fmt.Errorf("HTTP error %d", response.StatusCode)
}
if response.StatusCode == 204 {
// Just in case LogDNA will decide to respond with HTTP 204
return nil
}
var res map[string]interface{}
b, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
err = json.Unmarshal(b, &res)
if err != nil {
return err
}
status, ok := res["status"]
if !ok {
return errors.New("LogDNA response has no 'status' key")
}
if !ok || status != "ok" {
return fmt.Errorf("LogDNA responded with non-OK status: %v", status)
}
return nil
}
func (hook *Hook) run() chan *logEntry {
hook.wg.Add(1) // TODO: Consider only locking when buffer has something?
defer hook.wg.Done()
buffer := make([]*logEntry, 0)
timeout := time.After(hook.Config.FlushEvery)
for {
select {
case entry, ok := <-hook.c:
if !ok || entry == nil {
err := hook.Config.flush(&buffer)
if err == nil {
buffer = make([]*logEntry, 0)
if !ok {
hook.c = nil
return nil
}
} else if !ok {
// We were requested to terminate but can't
panic(fmt.Sprintf("Failed to flush to LogDNA: %v", err))
}
}
buffer = append(buffer, entry)
if len(buffer) >= hook.Config.BufferSize {
err := hook.Config.flush(&buffer)
if err == nil || hook.Config.MayDrop {
// TODO: Consider retrying a few times?
if err != nil && hook.Config.OnDrop != nil {
go hook.Config.OnDrop(len(buffer))
}
buffer = make([]*logEntry, 0)
timeout = time.After(hook.Config.FlushEvery)
}
}
case <-timeout:
if len(buffer) > 0 {
err := hook.Config.flush(&buffer)
if err == nil {
buffer = make([]*logEntry, 0)
timeout = time.After(hook.Config.FlushEvery)
}
}
}
}
}
func init() {
logrus_mate.RegisterHook("logdna", NewFromConfig)
}
// New creates a new LogDNA hook from a given Config struct
func New(config Config) (logrus.Hook, error) {
if config.APIKey == "" {
return nil, errors.New("LogDNA API key is required")
}
if config.Hostname == "" {
var err error
config.Hostname, err = os.Hostname()
if err != nil {
return nil, err
}
}
if config.QueueSize == 0 {
config.QueueSize = 128
}
hook := &Hook{
Config: &config,
c: make(chan *logEntry, config.QueueSize),
wg: &sync.WaitGroup{},
}
go hook.run()
logrus.RegisterExitHandler(func() {
_ = hook.Close()
})
return hook, nil
}
// NewConfig creates a new LogDNA Config from the Logrus Mate configuration
func NewConfig(cfg config.Configuration) Config {
var formatter logrus.Formatter
if cfg.GetBoolean("text-format", false) {
// TODO: Think of a way to pass arbitrary formatter via config
formatter = &SimpleTextFormatter{
QuoteEmptyFields: true,
}
}
return Config{
IngestURL: cfg.GetString("url", DefaultIngestURL),
APIKey: cfg.GetString("api-key"),
Hostname: cfg.GetString("hostname"),
MAC: cfg.GetString("mac"),
IP: cfg.GetString("ip"),
App: cfg.GetString("app"),
Env: cfg.GetString("env"),
BufferSize: int(cfg.GetInt32("size", 4096)),
QueueSize: int(cfg.GetInt32("qsize", 128)),
FlushEvery: cfg.GetTimeDuration("flush", 10*time.Second),
MayDrop: cfg.GetBoolean("drop", false),
LineJSON: cfg.GetBoolean("json", false),
MessageFormatter: formatter,
}
}
// NewFromConfig creates a new LogDNA hook from the Logrus Mate configuration
func NewFromConfig(cfg config.Configuration) (logrus.Hook, error) {
return New(NewConfig(cfg))
}