-
Notifications
You must be signed in to change notification settings - Fork 0
/
logstash.go
197 lines (168 loc) · 4.86 KB
/
logstash.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
package logstash
// logstash.go defines the logtash plugin (using lumberjack protocol) as being registered with all
// output plugins
import (
"crypto/tls"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/mode"
)
var debug = logp.MakeDebug("logstash")
func init() {
outputs.RegisterOutputPlugin("logstash", logstashOutputPlugin{})
}
type logstashOutputPlugin struct{}
func (p logstashOutputPlugin) NewOutput(
config *outputs.MothershipConfig,
topologyExpire int,
) (outputs.Outputer, error) {
output := &logstash{}
err := output.init(*config, topologyExpire)
if err != nil {
return nil, err
}
return output, nil
}
type logstash struct {
mode mode.ConnectionMode
index string
}
const (
logstashDefaultPort = 10200
logstashDefaultTimeout = 30 * time.Second
logstasDefaultMaxTimeout = 90 * time.Second
defaultSendRetries = 3
defaultMaxWindowSize = 1024
defaultCompressionLevel = 3
)
var waitRetry = time.Duration(1) * time.Second
// NOTE: maxWaitRetry has no effect on mode, as logstash client currently does not return ErrTempBulkFailure
var maxWaitRetry = time.Duration(60) * time.Second
func (lj *logstash) init(
config outputs.MothershipConfig,
topologyExpire int,
) error {
useTLS := (config.TLS != nil)
timeout := logstashDefaultTimeout
if config.Timeout != 0 {
timeout = time.Duration(config.Timeout) * time.Second
}
defaultPort := logstashDefaultPort
if config.Port != 0 {
defaultPort = config.Port
}
maxWindowSize := defaultMaxWindowSize
if config.BulkMaxSize != nil {
maxWindowSize = *config.BulkMaxSize
}
compressLevel := defaultCompressionLevel
if config.CompressionLevel != nil {
compressLevel = *config.CompressionLevel
}
var clients []mode.ProtocolClient
var err error
if useTLS {
var tlsConfig *tls.Config
tlsConfig, err = outputs.LoadTLSConfig(config.TLS)
if err != nil {
return err
}
clients, err = mode.MakeClients(config,
makeClientFactory(maxWindowSize, compressLevel, timeout,
makeTLSClient(defaultPort, tlsConfig)))
} else {
clients, err = mode.MakeClients(config,
makeClientFactory(maxWindowSize, compressLevel, timeout,
makeTCPClient(defaultPort)))
}
if err != nil {
return err
}
sendRetries := defaultSendRetries
if config.MaxRetries != nil {
sendRetries = *config.MaxRetries
}
logp.Info("Max Retries set to: %v", sendRetries)
maxAttempts := sendRetries + 1
if sendRetries < 0 {
maxAttempts = 0
}
var m mode.ConnectionMode
if len(clients) == 1 {
m, err = mode.NewSingleConnectionMode(clients[0],
maxAttempts, waitRetry, timeout, maxWaitRetry)
} else {
loadBalance := config.LoadBalance != nil && *config.LoadBalance
if loadBalance {
m, err = mode.NewLoadBalancerMode(clients, maxAttempts,
waitRetry, timeout, maxWaitRetry)
} else {
m, err = mode.NewFailOverConnectionMode(clients, maxAttempts, waitRetry, timeout)
}
}
if err != nil {
return err
}
lj.mode = m
lj.index = config.Index
return nil
}
func makeClientFactory(
maxWindowSize int,
compressLevel int,
timeout time.Duration,
makeTransp func(string) (TransportClient, error),
) func(string) (mode.ProtocolClient, error) {
return func(host string) (mode.ProtocolClient, error) {
transp, err := makeTransp(host)
if err != nil {
return nil, err
}
return newLumberjackClient(transp, compressLevel, maxWindowSize, timeout)
}
}
func makeTCPClient(port int) func(string) (TransportClient, error) {
return func(host string) (TransportClient, error) {
return newTCPClient(host, port)
}
}
func makeTLSClient(port int, tls *tls.Config) func(string) (TransportClient, error) {
return func(host string) (TransportClient, error) {
return newTLSClient(host, port, tls)
}
}
// 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 *logstash) PublishEvent(
signaler outputs.Signaler,
opts outputs.Options,
event common.MapStr,
) error {
lj.addMeta(event)
return lj.mode.PublishEvent(signaler, opts, event)
}
// BulkPublish implements the BulkOutputer interface pushing a bulk of events
// via lumberjack.
func (lj *logstash) BulkPublish(
trans outputs.Signaler,
opts outputs.Options,
events []common.MapStr,
) error {
for _, event := range events {
lj.addMeta(event)
}
return lj.mode.PublishEvents(trans, opts, events)
}
// addMeta adapts events to be compatible with logstash forwarer messages by renaming
// the "message" field to "line". The lumberjack server in logstash will
// decode/rename the "line" field into "message".
func (lj *logstash) addMeta(event common.MapStr) {
// add metadata for indexing
event["@metadata"] = common.MapStr{
"beat": lj.index,
"type": event["type"].(string),
}
}