-
Notifications
You must be signed in to change notification settings - Fork 0
/
es.go
94 lines (75 loc) · 1.95 KB
/
es.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
// Package es implements an Elasticsearch batch handler. Currently this implementation
// assumes the index format of "logs-YY-MM-DD".
package es
import (
"io"
stdlog "log"
"sync"
"time"
"github.com/tj/go-elastic/batch"
"github.com/apex/log"
)
// TODO(tj): allow dumping logs to stderr on timeout
// TODO(tj): allow custom format that does not include .fields etc
// TODO(tj): allow interval flushes
// TODO(tj): allow explicit Flush() (for Lambda where you have to flush at the end of function)
// Elasticsearch interface.
type Elasticsearch interface {
Bulk(io.Reader) error
}
// Config for handler.
type Config struct {
BufferSize int // BufferSize is the number of logs to buffer before flush (default: 100)
Format string // Format for index
Client Elasticsearch // Client for ES
}
// defaults applies defaults to the config.
func (c *Config) defaults() {
if c.BufferSize == 0 {
c.BufferSize = 100
}
if c.Format == "" {
c.Format = "logs-06-01-02"
}
}
// Handler implementation.
type Handler struct {
*Config
mu sync.Mutex
batch *batch.Batch
}
// New handler with BufferSize
func New(config *Config) *Handler {
config.defaults()
return &Handler{
Config: config,
}
}
// HandleLog implements log.Handler.
func (h *Handler) HandleLog(e *log.Entry) error {
h.mu.Lock()
defer h.mu.Unlock()
if h.batch == nil {
h.batch = &batch.Batch{
Index: time.Now().Format(h.Config.Format),
Elastic: h.Client,
Type: "log",
}
}
h.batch.Add(e)
if h.batch.Size() >= h.BufferSize {
go h.flush(h.batch)
h.batch = nil
}
return nil
}
// flush the given `batch` asynchronously.
func (h *Handler) flush(batch *batch.Batch) {
size := batch.Size()
start := time.Now()
stdlog.Printf("log/elastic: flushing %d logs", size)
if err := batch.Flush(); err != nil {
stdlog.Printf("log/elastic: failed to flush %d logs: %s", size, err)
}
stdlog.Printf("log/elastic: flushed %d logs in %s", size, time.Since(start))
}