Class: Fluent::ElasticsearchOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::ElasticsearchOutput
show all
- Includes:
- SetTagKeyMixin
- Defined in:
- lib/fluent/plugin/out_elasticsearch.rb
Defined Under Namespace
Classes: ConnectionFailure, TimeParser
Instance Method Summary
collapse
Constructor Details
48
49
50
51
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 48
def initialize
super
@time_parser = TimeParser.new(@time_key_format, @router)
end
|
Instance Method Details
#append_record_to_messages(op, meta, record, msgs) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 171
def append_record_to_messages(op, meta, record, msgs)
case op
when "update", "upsert"
if meta.has_key?("_id")
msgs << { "update" => meta }
msgs << { "doc" => record, "doc_as_upsert" => op == "upsert" }
end
when "create"
if meta.has_key?("_id")
msgs << { "create" => meta }
msgs << record
end
when "index"
msgs << { "index" => meta }
msgs << record
end
end
|
#client ⇒ Object
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
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 94
def client
@_es ||= begin
excon_options = { client_key: @client_key, client_cert: @client_cert, client_key_pass: @client_key_pass }
adapter_conf = lambda {|f| f.adapter :excon, excon_options }
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(get_connection_options.merge(
options: {
reload_connections: @reload_connections,
reload_on_failure: @reload_on_failure,
resurrect_after: @resurrect_after,
retry_on_failure: 5,
transport_options: {
request: { timeout: @request_timeout },
ssl: { verify: @ssl_verify, ca_file: @ca_file }
}
}), &adapter_conf)
es = Elasticsearch::Client.new transport: transport
begin
raise ConnectionFailure, "Can not reach Elasticsearch cluster (#{connection_options_description})!" unless es.ping
rescue *es.transport.host_unreachable_exceptions => e
raise ConnectionFailure, "Can not reach Elasticsearch cluster (#{connection_options_description})! #{e.message}"
end
log.info "Connection opened to Elasticsearch cluster => #{connection_options_description}"
es
end
end
|
53
54
55
56
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 53
def configure(conf)
super
@time_parser = TimeParser.new(@time_key_format, @router)
end
|
#connection_options_description ⇒ Object
155
156
157
158
159
160
161
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 155
def connection_options_description
get_connection_options[:hosts].map do |host_info|
attributes = host_info.dup
attributes[:password] = 'obfuscated' if attributes.has_key?(:password)
attributes.inspect
end.join(', ')
end
|
163
164
165
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 163
def format(tag, time, record)
[tag, time, record].to_msgpack
end
|
#get_connection_options ⇒ Object
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
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 122
def get_connection_options
raise "`password` must be present if `user` is present" if @user && !@password
hosts = if @hosts
@hosts.split(',').map do |host_str|
if host_str.match(%r{^[^:]+(\:\d+)?$})
{
host: host_str.split(':')[0],
port: (host_str.split(':')[1] || @port).to_i,
scheme: @scheme
}
else
uri = URI(host_str)
%w(user password path).inject(host: uri.host, port: uri.port, scheme: uri.scheme) do |hash, key|
hash[key.to_sym] = uri.public_send(key) unless uri.public_send(key).nil? || uri.public_send(key) == ''
hash
end
end
end.compact
else
[{host: @host, port: @port, scheme: @scheme}]
end.each do |host|
host.merge!(user: @user, password: @password) if !host[:user] && @user
host.merge!(path: @path) if !host[:path] && @path
end
{
hosts: hosts
}
end
|
#send(data) ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 233
def send(data)
retries = 0
begin
client.bulk body: data
rescue *client.transport.host_unreachable_exceptions => e
if retries < 2
retries += 1
@_es = nil
log.warn "Could not push logs to Elasticsearch, resetting connection and trying again. #{e.message}"
sleep 2**retries
retry
end
raise ConnectionFailure, "Could not push logs to Elasticsearch after #{retries} retries. #{e.message}"
end
end
|
#shutdown ⇒ Object
167
168
169
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 167
def shutdown
super
end
|
#start ⇒ Object
58
59
60
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 58
def start
super
end
|
#write(chunk) ⇒ Object
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
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 189
def write(chunk)
bulk_message = []
chunk.msgpack_each do |tag, time, record|
next unless record.is_a? Hash
if @target_index_key && record[@target_index_key]
target_index = record.delete @target_index_key
elsif @logstash_format
if record.has_key?("@timestamp")
dt = record["@timestamp"]
dt = @time_parser.parse(record["@timestamp"], time)
elsif record.has_key?(@time_key)
dt = @time_parser.parse(record[@time_key], time)
record['@timestamp'] = record[@time_key]
else
dt = Time.at(time).to_datetime
record.merge!({"@timestamp" => dt.to_s})
end
dt = dt.new_offset(0) if @utc_index
target_index = "#{@logstash_prefix}-#{dt.strftime(@logstash_dateformat)}"
else
target_index = @index_name
end
if @include_tag_key
record.merge!(@tag_key => tag)
end
meta = {"_index" => target_index, "_type" => type_name}
if @id_key && record[@id_key]
meta['_id'] = record[@id_key]
end
if @parent_key && record[@parent_key]
meta['_parent'] = record[@parent_key]
end
append_record_to_messages(@write_operation, meta, record, bulk_message)
end
send(bulk_message) unless bulk_message.empty?
bulk_message.clear
end
|