forked from Ingensi/dockbeat
/
lumberjack.go
164 lines (143 loc) · 3.74 KB
/
lumberjack.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
package lumberjack
// lumberjack.go defines the lumberjack plugin as being registered with all
// output plugins
import (
"crypto/tls"
"errors"
"time"
"github.com/elastic/libbeat/common"
"github.com/elastic/libbeat/logp"
"github.com/elastic/libbeat/outputs"
)
var debug = logp.MakeDebug("lumberjack")
func init() {
outputs.RegisterOutputPlugin("lumberjack", lumberjackOutputPlugin{})
}
type lumberjackOutputPlugin struct{}
func (p lumberjackOutputPlugin) NewOutput(
beat string,
config *outputs.MothershipConfig,
topologyExpire int,
) (outputs.Outputer, error) {
output := &lumberjack{}
err := output.init(beat, *config, topologyExpire)
if err != nil {
return nil, err
}
return output, nil
}
type lumberjack struct {
mode ConnectionMode
}
const (
lumberjackDefaultTimeout = 5 * time.Second
defaultSendRetries = 3
)
// ErrNoHostsConfigured indicates missing host or hosts configuration
var ErrNoHostsConfigured = errors.New("no host configuration found")
var waitRetry = time.Duration(1) * time.Second
func (lj *lumberjack) init(
beat string,
config outputs.MothershipConfig,
topologyExpire int,
) error {
useTLS := false
if config.TLS != nil {
useTLS = *config.TLS
}
timeout := lumberjackDefaultTimeout
if config.Timeout != 0 {
timeout = time.Duration(config.Timeout) * time.Second
}
var clients []ProtocolClient
var err error
if useTLS {
var tlsConfig *tls.Config
tlsConfig, err = outputs.LoadTLSConfig(config)
if err != nil {
return err
}
clients, err = makeClients(config, timeout,
func(host string) (TransportClient, error) {
return newTLSClient(host, tlsConfig)
})
} else {
clients, err = makeClients(config, timeout,
func(host string) (TransportClient, error) {
return newTCPClient(host)
})
}
if err != nil {
return err
}
sendRetries := defaultSendRetries
if config.Max_retries != nil {
sendRetries = *config.Max_retries
}
var mode ConnectionMode
if len(clients) == 1 {
mode, err = newSingleConnectionMode(clients[0], sendRetries, waitRetry, timeout)
} else {
loadBalance := config.LoadBalance == nil || *config.LoadBalance
if loadBalance {
mode, err = newLoadBalancerMode(clients, sendRetries, waitRetry, timeout)
} else {
mode, err = newFailOverConnectionMode(clients, sendRetries, waitRetry, timeout)
}
}
if err != nil {
return err
}
lj.mode = mode
return nil
}
func makeClients(
config outputs.MothershipConfig,
timeout time.Duration,
newTransp func(string) (TransportClient, error),
) ([]ProtocolClient, error) {
switch {
case len(config.Hosts) > 0:
var clients []ProtocolClient
for _, host := range config.Hosts {
transp, err := newTransp(host)
if err != nil {
for _, client := range clients {
_ = client.Close() // ignore error
}
return nil, err
}
client := newLumberjackClient(transp, timeout)
clients = append(clients, client)
}
return clients, nil
case config.Host != "":
transp, err := newTransp(config.Host)
if err != nil {
return nil, err
}
return []ProtocolClient{newLumberjackClient(transp, timeout)}, nil
default:
return nil, ErrNoHostsConfigured
}
}
// TODO: update Outputer interface to support multiple events for batch-like
// processing (e.g. for filebeat). Batch like processing might reduce
// send/receive overhead per event for other implementors too.
func (lj *lumberjack) PublishEvent(
trans outputs.Signaler,
ts time.Time,
event common.MapStr,
) error {
events := []common.MapStr{event}
return lj.mode.PublishEvents(trans, events)
}
// BulkPublish implements the BulkOutputer interface pushing a bulk of events
// via lumberjack.
func (lj *lumberjack) BulkPublish(
trans outputs.Signaler,
ts time.Time,
events []common.MapStr,
) error {
return lj.mode.PublishEvents(trans, events)
}