forked from ruflin/filebeat
/
logstash.go
197 lines (173 loc) · 4.63 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"
"errors"
"fmt"
"time"
"github.com/elastic/libbeat/common"
"github.com/elastic/libbeat/logp"
"github.com/elastic/libbeat/outputs"
"github.com/elastic/libbeat/outputs/mode"
)
var debug = logp.MakeDebug("logstash")
func init() {
outputs.RegisterOutputPlugin("logstash", logstashOutputPlugin{})
}
type logstashOutputPlugin struct{}
func (p logstashOutputPlugin) NewOutput(
beat string,
config *outputs.MothershipConfig,
topologyExpire int,
) (outputs.Outputer, error) {
output := &logstash{}
err := output.init(beat, *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
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 *logstash) init(
beat string,
config outputs.MothershipConfig,
topologyExpire int,
) error {
useTLS := false
if config.TLS != nil {
useTLS = !config.TLS.Disabled
}
timeout := logstashDefaultTimeout
if config.Timeout != 0 {
timeout = time.Duration(config.Timeout) * time.Second
}
defaultPort := logstashDefaultPort
if config.Port != 0 {
defaultPort = config.Port
}
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 = makeClients(config, timeout,
func(host string) (TransportClient, error) {
return newTLSClient(host, defaultPort, tlsConfig)
})
} else {
clients, err = makeClients(config, timeout,
func(host string) (TransportClient, error) {
return newTCPClient(host, defaultPort)
})
}
if err != nil {
return err
}
sendRetries := defaultSendRetries
if config.Max_retries != nil {
sendRetries = *config.Max_retries
}
var m mode.ConnectionMode
if len(clients) == 1 {
m, err = mode.NewSingleConnectionMode(clients[0],
sendRetries, waitRetry, timeout)
} else {
loadBalance := config.LoadBalance != nil && *config.LoadBalance
if loadBalance {
m, err = mode.NewLoadBalancerMode(clients, sendRetries, waitRetry, timeout)
} else {
m, err = mode.NewFailOverConnectionMode(clients, sendRetries, waitRetry, timeout)
}
}
if err != nil {
return err
}
lj.mode = m
if config.Index != "" {
lj.index = config.Index
} else {
lj.index = beat
}
return nil
}
func makeClients(
config outputs.MothershipConfig,
timeout time.Duration,
newTransp func(string) (TransportClient, error),
) ([]mode.ProtocolClient, error) {
switch {
case len(config.Hosts) > 0:
var clients []mode.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 []mode.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 *logstash) PublishEvent(
signaler outputs.Signaler,
ts time.Time,
event common.MapStr,
) error {
lj.addMeta(event)
return lj.mode.PublishEvent(signaler, event)
}
// BulkPublish implements the BulkOutputer interface pushing a bulk of events
// via lumberjack.
func (lj *logstash) BulkPublish(
trans outputs.Signaler,
ts time.Time,
events []common.MapStr,
) error {
for _, event := range events {
lj.addMeta(event)
}
return lj.mode.PublishEvents(trans, 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
ts := time.Time(event["timestamp"].(common.Time)).UTC()
index := fmt.Sprintf("%s-%02d.%02d.%02d", lj.index,
ts.Year(), ts.Month(), ts.Day())
event["@metadata"] = common.MapStr{
"index": index,
"type": event["type"].(string),
}
}