Class: Fluent::ElasticsearchOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::ElasticsearchOutput
- Includes:
- ElasticsearchIndexTemplate, SetTagKeyMixin
- Defined in:
- lib/fluent/plugin/out_elasticsearch.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ConnectionFailure, TimeParser
Instance Method Summary collapse
- #append_record_to_messages(op, meta, record, msgs) ⇒ Object
- #client ⇒ Object
- #configure(conf) ⇒ Object
- #connection_options_description ⇒ Object
- #flatten_record(record, prefix = []) ⇒ Object
- #format(tag, time, record) ⇒ Object
- #get_connection_options ⇒ Object
-
#get_parent_of(record, path) ⇒ Object
returns [parent, child_key] of child described by path array in record’s tree returns [nil, child_key] if path doesnt exist in record.
-
#initialize ⇒ ElasticsearchOutput
constructor
A new instance of ElasticsearchOutput.
- #remove_keys(record) ⇒ Object
- #send(data) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #update_body(record, op) ⇒ Object
- #write(chunk) ⇒ Object
Constructor Details
#initialize ⇒ ElasticsearchOutput
Returns a new instance of ElasticsearchOutput.
62 63 64 65 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 62 def initialize super @time_parser = TimeParser.new(@time_key_format, @router) end |
Instance Method Details
#append_record_to_messages(op, meta, record, msgs) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 205 def (op, , record, msgs) case op when "update", "upsert" if .has_key?("_id") msgs << { "update" => } msgs << update_body(record, op) end when "create" if .has_key?("_id") msgs << { "create" => } msgs << record end when "index" msgs << { "index" => } msgs << record end end |
#client ⇒ Object
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 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 128 def client @_es ||= begin = { client_key: @client_key, client_cert: @client_cert, client_key_pass: @client_key_pass } adapter_conf = lambda {|f| f.adapter :excon, } transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(.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 |
#configure(conf) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 67 def configure(conf) super @time_parser = TimeParser.new(@time_key_format, @router) if @remove_keys @remove_keys = @remove_keys.split(/\s*,\s*/) end if @target_index_key && @target_index_key.is_a?(String) @target_index_key = @target_index_key.split '.' end if @target_type_key && @target_type_key.is_a?(String) @target_type_key = @target_type_key.split '.' end if @remove_keys_on_update && @remove_keys_on_update.is_a?(String) @remove_keys_on_update = @remove_keys_on_update.split ',' end if @template_name && @template_file template_install(@template_name, @template_file) end end |
#connection_options_description ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 189 def [:hosts].map do |host_info| attributes = host_info.dup attributes[:password] = 'obfuscated' if attributes.has_key?(:password) attributes.inspect end.join(', ') end |
#flatten_record(record, prefix = []) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 245 def flatten_record(record, prefix=[]) ret = {} if record.is_a? Hash record.each { |key, value| ret.merge! flatten_record(value, prefix + [key.to_s]) } elsif record.is_a? Array # Don't mess with arrays, leave them unprocessed ret.merge!({prefix.join(@flatten_hashes_separator) => record}) else return {prefix.join(@flatten_hashes_separator) => record} end ret end |
#format(tag, time, record) ⇒ Object
197 198 199 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 197 def format(tag, time, record) [tag, time, record].to_msgpack end |
#get_connection_options ⇒ Object
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 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 156 def raise "`password` must be present if `user` is present" if @user && !@password hosts = if @hosts @hosts.split(',').map do |host_str| # Support legacy hosts format host:port,host:port,host:port... if host_str.match(%r{^[^:]+(\:\d+)?$}) { host: host_str.split(':')[0], port: (host_str.split(':')[1] || @port).to_i, scheme: @scheme } else # New hosts format expects URLs such as http://logs.foo.com,https://john:[email protected]/elastic 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 |
#get_parent_of(record, path) ⇒ Object
returns [parent, child_key] of child described by path array in record’s tree returns [nil, child_key] if path doesnt exist in record
324 325 326 327 328 329 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 324 def get_parent_of(record, path) return [nil, nil] unless path parent_object = path[0..-2].reduce(record) { |a, e| a.is_a?(Hash) ? a[e] : nil } [parent_object, path[-1]] end |
#remove_keys(record) ⇒ Object
236 237 238 239 240 241 242 243 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 236 def remove_keys(record) keys = record[@remove_keys_on_update_key] || @remove_keys_on_update || [] record.delete(@remove_keys_on_update_key) return record unless keys.any? record = record.dup keys.each { |key| record.delete(key) } record end |
#send(data) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 331 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
201 202 203 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 201 def shutdown super end |
#start ⇒ Object
92 93 94 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 92 def start super end |
#update_body(record, op) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 223 def update_body(record, op) update = remove_keys(record) body = { "doc" => update } if op == "upsert" if update == record body["doc_as_upsert"] = true else body["upsert"] = record end end body end |
#write(chunk) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 260 def write(chunk) = [] chunk.msgpack_each do |tag, time, record| if @flatten_hashes record = flatten_record(record) end next unless record.is_a? Hash target_index_parent, target_index_child_key = get_parent_of(record, @target_index_key) if target_index_parent && target_index_parent[target_index_child_key] target_index = target_index_parent.delete(target_index_child_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] unless 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 # Change target_index to lower-case since Elasticsearch doesn't # allow upper-case characters in index names. target_index = target_index.downcase if @include_tag_key record.merge!(@tag_key => tag) end target_type_parent, target_type_child_key = get_parent_of(record, @target_type_key) if target_type_parent && target_type_parent[target_type_child_key] target_type = target_type_parent.delete(target_type_child_key) else target_type = @type_name end = {"_index" => target_index, "_type" => target_type} ||= { 'id_key' => '_id', 'parent_key' => '_parent', 'routing_key' => '_routing' } .each_pair do |config_name, | record_key = self.instance_variable_get("@#{config_name}") [] = record[record_key] if record_key && record[record_key] end if @remove_keys @remove_keys.each { |key| record.delete(key) } end (@write_operation, , record, ) end send() unless .empty? .clear end |