Class: Fluent::Plugin::ElasticsearchOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::Plugin::ElasticsearchOutput
- Includes:
- ElasticsearchIndexTemplate, ElasticsearchConstants
- Defined in:
- lib/fluent/plugin/out_elasticsearch.rb
Direct Known Subclasses
Defined Under Namespace
Classes: MissingIdFieldError, RecoverableRequestFailure, RequestInfo, RetryStreamError, UnrecoverableRequestFailure
Constant Summary collapse
- DEFAULT_BUFFER_TYPE =
"memory"- DEFAULT_ELASTICSEARCH_VERSION =
For compatibility.
5- DEFAULT_TYPE_NAME_ES_7x =
"_doc".freeze
- DEFAULT_TYPE_NAME =
"fluentd".freeze
- DEFAULT_RELOAD_AFTER =
-1- TARGET_BULK_BYTES =
20 * 1024 * 1024
Constants included from ElasticsearchConstants
Fluent::Plugin::ElasticsearchConstants::BODY_DELIMITER, Fluent::Plugin::ElasticsearchConstants::CREATE_OP, Fluent::Plugin::ElasticsearchConstants::ID_FIELD, Fluent::Plugin::ElasticsearchConstants::INDEX_OP, Fluent::Plugin::ElasticsearchConstants::TIMESTAMP_FIELD, Fluent::Plugin::ElasticsearchConstants::UPDATE_OP, Fluent::Plugin::ElasticsearchConstants::UPSERT_OP
Instance Method Summary collapse
-
#append_record_to_messages(op, meta, header, record, msgs) ⇒ Object
append_record_to_messages adds a record to the bulk message payload to be submitted to Elasticsearch.
- #backend_options ⇒ Object
- #client(host = nil) ⇒ Object
- #client_library_version ⇒ Object
- #configure(conf) ⇒ Object
- #configure_routing_key_name ⇒ Object
- #connection_options_description(con_host = nil) ⇒ Object
- #convert_compat_id_key(key) ⇒ Object
- #convert_numeric_time_into_string(numeric_time, time_key_format = "%Y-%m-%d %H:%M:%S.%N%z") ⇒ Object
- #create_meta_config_map ⇒ Object
-
#create_time_parser ⇒ Object
once fluent v0.14 is released we might be able to use Fluent::Parser::TimeParser, but it doesn’t quite do what we want - if gives [sec,nsec] where as we want something we can call ‘strftime` on…
- #detect_es_major_version ⇒ Object
- #expand_placeholders(chunk) ⇒ Object
- #flatten_record(record, prefix = []) ⇒ Object
- #get_connection_options(con_host = nil) ⇒ Object
- #get_escaped_userinfo(host_str) ⇒ 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.
- #is_existing_connection(host) ⇒ Object
- #multi_workers_ready? ⇒ Boolean
- #parse_time(value, event_time, tag) ⇒ Object
- #placeholder?(name, param) ⇒ Boolean
- #process_message(tag, meta, header, time, record, extracted_values) ⇒ Object
- #remove_keys(record) ⇒ Object
-
#send_bulk(data, tag, chunk, bulk_message_count, extracted_values, info) ⇒ Object
send_bulk given a specific bulk request, the original tag, chunk, and bulk_message_count.
- #update_body(record, op) ⇒ Object
- #write(chunk) ⇒ Object
Methods included from ElasticsearchIndexTemplate
#get_custom_template, #get_template, #indexcreation, #retry_operate, #template_custom_install, #template_exists?, #template_install, #template_put, #templates_hash_install
Constructor Details
#initialize ⇒ ElasticsearchOutput
149 150 151 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 149 def initialize super end |
Instance Method Details
#append_record_to_messages(op, meta, header, record, msgs) ⇒ Object
append_record_to_messages adds a record to the bulk message payload to be submitted to Elasticsearch. Records that do not include ‘_id’ field are skipped when ‘write_operation’ is configured for ‘create’ or ‘update’
returns ‘true’ if record was appended to the bulk message
and 'false' otherwise
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 491 def (op, , header, record, msgs) case op when UPDATE_OP, UPSERT_OP if .has_key?(ID_FIELD) header[UPDATE_OP] = msgs << @dump_proc.call(header) << BODY_DELIMITER msgs << @dump_proc.call(update_body(record, op)) << BODY_DELIMITER return true end when CREATE_OP if .has_key?(ID_FIELD) header[CREATE_OP] = msgs << @dump_proc.call(header) << BODY_DELIMITER msgs << @dump_proc.call(record) << BODY_DELIMITER return true end when INDEX_OP header[INDEX_OP] = msgs << @dump_proc.call(header) << BODY_DELIMITER msgs << @dump_proc.call(record) << BODY_DELIMITER return true end return false end |
#backend_options ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 308 def case @http_backend when :excon { client_key: @client_key, client_cert: @client_cert, client_key_pass: @client_key_pass } when :typhoeus require 'typhoeus' { sslkey: @client_key, sslcert: @client_cert, keypasswd: @client_key_pass } end rescue LoadError => ex log.error_backtrace(ex.backtrace) raise Fluent::ConfigError, "You must install #{@http_backend} gem. Exception: #{ex}" end |
#client(host = nil) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 395 def client(host = nil) # check here to see if we already have a client connection for the given host = (host) @_es = nil unless is_existing_connection([:hosts]) @_es ||= begin @current_config = [:hosts].clone adapter_conf = lambda {|f| f.adapter @http_backend, } local_reload_connections = @reload_connections if local_reload_connections && @reload_after > DEFAULT_RELOAD_AFTER local_reload_connections = @reload_after end headers = { 'Content-Type' => @content_type.to_s }.merge(@custom_headers) transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(.merge( options: { reload_connections: local_reload_connections, reload_on_failure: @reload_on_failure, resurrect_after: @resurrect_after, logger: @transport_logger, transport_options: { headers: headers, request: { timeout: @request_timeout }, ssl: { verify: @ssl_verify, ca_file: @ca_file, version: @ssl_version } }, http: { user: @user, password: @password }, sniffer_class: @sniffer_class, serializer_class: @serializer_class, }), &adapter_conf) Elasticsearch::Client.new transport: transport end end |
#client_library_version ⇒ Object
326 327 328 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 326 def client_library_version Elasticsearch::VERSION end |
#configure(conf) ⇒ Object
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 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 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 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 153 def configure(conf) compat_parameters_convert(conf, :buffer) super raise Fluent::ConfigError, "'tag' in chunk_keys is required." if not @chunk_key_tag @time_parser = create_time_parser = 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 raise Fluent::ConfigError, "'max_retry_putting_template' must be positive number." if @max_retry_putting_template < 0 # Raise error when using host placeholders and template features at same time. valid_host_placeholder = placeholder?(:host_placeholder, @host) if valid_host_placeholder && (@template_name && @template_file || @templates) raise Fluent::ConfigError, "host placeholder and template installation are exclusive features." end if !Fluent::Engine.dry_run_mode if @template_name && @template_file retry_operate(@max_retry_putting_template) do if @customize_template if @rollover_index raise Fluent::ConfigError, "'deflector_alias' must be provided if 'rollover_index' is set true ." if not @deflector_alias end template_custom_install(@template_name, @template_file, @template_overwrite, @customize_template, @index_prefix, @rollover_index, @deflector_alias, @application_name, @index_date_pattern) else template_install(@template_name, @template_file, @template_overwrite) end end elsif @templates retry_operate(@max_retry_putting_template) do templates_hash_install(@templates, @template_overwrite) end end end # Consider missing the prefix of "$." in nested key specifiers. @id_key = convert_compat_id_key(@id_key) if @id_key @parent_key = convert_compat_id_key(@parent_key) if @parent_key @routing_key = convert_compat_id_key(@routing_key) if @routing_key = @serializer_class = nil begin require 'oj' @dump_proc = Oj.method(:dump) if @prefer_oj_serializer @serializer_class = Fluent::Plugin::Serializer::Oj Elasticsearch::API.settings[:serializer] = Fluent::Plugin::Serializer::Oj end rescue LoadError @dump_proc = Yajl.method(:dump) end if @user && m = @user.match(/%{(?<user>.*)}/) @user = URI.encode_www_form_component(m["user"]) end if @password && m = @password.match(/%{(?<password>.*)}/) @password = URI.encode_www_form_component(m["password"]) end @transport_logger = nil if @with_transporter_log @transport_logger = log log_level = conf['@log_level'] || conf['log_level'] log.warn "Consider to specify log_level with @log_level." unless log_level end # Specify @sniffer_class before calling #client. # #detect_es_major_version uses #client. @sniffer_class = nil begin @sniffer_class = Object.const_get(@sniffer_class_name) if @sniffer_class_name rescue Exception => ex raise Fluent::ConfigError, "Could not load sniffer class #{@sniffer_class_name}: #{ex}" end @last_seen_major_version = if @verify_es_version_at_startup && !Fluent::Engine.dry_run_mode retry_operate(@max_retry_get_es_version) do detect_es_major_version end else @default_elasticsearch_version end if @last_seen_major_version == 6 && @type_name != DEFAULT_TYPE_NAME_ES_7x log.info "Detected ES 6.x: ES 7.x will only accept `_doc` in type_name." end if @last_seen_major_version >= 7 && @type_name != DEFAULT_TYPE_NAME_ES_7x log.warn "Detected ES 7.x or above: `_doc` will be used as the document `_type`." @type_name = '_doc'.freeze end if @validate_client_version && !Fluent::Engine.dry_run_mode if @last_seen_major_version != client_library_version.to_i raise Fluent::ConfigError, " Detected ES \#{@last_seen_major_version} but you use ES client \#{client_library_version}.\n Please consider to use \#{@last_seen_major_version}.x series ES client.\n EOC\n end\n end\n\n if @last_seen_major_version >= 6\n case @ssl_version\n when :SSLv23, :TLSv1, :TLSv1_1\n if @scheme == :https\n log.warn \"Detected ES 6.x or above and enabled insecure security:\n You might have to specify `ssl_version TLSv1_2` in configuration.\"\n end\n end\n end\n\n if @buffer_config.flush_thread_count < 2\n log.warn \"To prevent events traffic jam, you should specify 2 or more 'flush_thread_count'.\"\n end\n\n @routing_key_name = configure_routing_key_name\n @current_config = nil\n\n @ignore_exception_classes = @ignore_exceptions.map do |exception|\n unless Object.const_defined?(exception)\n log.warn \"Cannot find class \#{exception}. Will ignore it.\"\n\n nil\n else\n Object.const_get(exception)\n end\n end.compact\nend\n" |
#configure_routing_key_name ⇒ Object
330 331 332 333 334 335 336 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 330 def configure_routing_key_name if @last_seen_major_version >= 7 'routing' else '_routing' end end |
#connection_options_description(con_host = nil) ⇒ Object
476 477 478 479 480 481 482 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 476 def (con_host=nil) (con_host)[:hosts].map do |host_info| attributes = host_info.dup attributes[:password] = 'obfuscated' if attributes.has_key?(:password) attributes.inspect end.join(', ') end |
#convert_compat_id_key(key) ⇒ Object
338 339 340 341 342 343 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 338 def convert_compat_id_key(key) if key.include?('.') && !key.start_with?('$[') key = "$.#{key}" unless key.start_with?('$.') end key end |
#convert_numeric_time_into_string(numeric_time, time_key_format = "%Y-%m-%d %H:%M:%S.%N%z") ⇒ Object
383 384 385 386 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 383 def convert_numeric_time_into_string(numeric_time, time_key_format = "%Y-%m-%d %H:%M:%S.%N%z") numeric_time_parser = Fluent::NumericTimeParser.new(:float) Time.at(numeric_time_parser.parse(numeric_time).to_r).strftime(time_key_format) end |
#create_meta_config_map ⇒ Object
345 346 347 348 349 350 351 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 345 def result = [] result << [record_accessor_create(@id_key), '_id'] if @id_key result << [record_accessor_create(@parent_key), '_parent'] if @parent_key result << [record_accessor_create(@routing_key), @routing_key_name] if @routing_key result end |
#create_time_parser ⇒ Object
once fluent v0.14 is released we might be able to use Fluent::Parser::TimeParser, but it doesn’t quite do what we want - if gives
- sec,nsec
-
where as we want something we can call ‘strftime` on…
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 356 def create_time_parser if @time_key_format begin # Strptime doesn't support all formats, but for those it does it's # blazingly fast. strptime = Strptime.new(@time_key_format) Proc.new { |value| value = convert_numeric_time_into_string(value, @time_key_format) if value.is_a?(Numeric) strptime.exec(value).to_datetime } rescue # Can happen if Strptime doesn't recognize the format; or # if strptime couldn't be required (because it's not installed -- it's # ruby 2 only) Proc.new { |value| value = convert_numeric_time_into_string(value, @time_key_format) if value.is_a?(Numeric) DateTime.strptime(value, @time_key_format) } end else Proc.new { |value| value = convert_numeric_time_into_string(value) if value.is_a?(Numeric) DateTime.parse(value) } end end |
#detect_es_major_version ⇒ Object
321 322 323 324 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 321 def detect_es_major_version @_es_info ||= client.info @_es_info["version"]["number"].to_i end |
#expand_placeholders(chunk) ⇒ Object
556 557 558 559 560 561 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 556 def (chunk) logstash_prefix = extract_placeholders(@logstash_prefix, chunk) index_name = extract_placeholders(@index_name, chunk) type_name = extract_placeholders(@type_name, chunk) return logstash_prefix, index_name, type_name end |
#flatten_record(record, prefix = []) ⇒ Object
541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 541 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 |
#get_connection_options(con_host = nil) ⇒ Object
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 443 def (con_host=nil) raise "`password` must be present if `user` is present" if @user && !@password hosts = if con_host || @hosts (con_host || @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.to_s } else # New hosts format expects URLs such as http://logs.foo.com,https://john:[email protected]/elastic uri = URI(get_escaped_userinfo(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.to_s}] 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_escaped_userinfo(host_str) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 431 def get_escaped_userinfo(host_str) if m = host_str.match(/(?<scheme>.*)%{(?<user>.*)}:%{(?<password>.*)}(?<path>@.*)/) m["scheme"] + URI.encode_www_form_component(m["user"]) + ':' + URI.encode_www_form_component(m["password"]) + m["path"] else host_str end 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
701 702 703 704 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 701 def get_parent_of(record, path) parent_object = path[0..-2].reduce(record) { |a, e| a.is_a?(Hash) ? a[e] : nil } [parent_object, path[-1]] end |
#is_existing_connection(host) ⇒ Object
737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 737 def is_existing_connection(host) # check if the host provided match the current connection return false if @_es.nil? return false if @current_config.nil? return false if host.length != @current_config.length for i in 0...host.length if !host[i][:host].eql? @current_config[i][:host] || host[i][:port] != @current_config[i][:port] return false end end return true end |
#multi_workers_ready? ⇒ Boolean
563 564 565 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 563 def multi_workers_ready? true end |
#parse_time(value, event_time, tag) ⇒ Object
388 389 390 391 392 393 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 388 def parse_time(value, event_time, tag) @time_parser.call(value) rescue => e router.emit_error_event(@time_parse_error_tag, Fluent::Engine.now, {'tag' => tag, 'time' => event_time, 'format' => @time_key_format, 'value' => value}, e) return Time.at(event_time).to_datetime end |
#placeholder?(name, param) ⇒ Boolean
299 300 301 302 303 304 305 306 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 299 def placeholder?(name, param) begin placeholder_validate!(name, param) true rescue Fluent::ConfigError false end end |
#process_message(tag, meta, header, time, record, extracted_values) ⇒ Object
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 620 def (tag, , header, time, record, extracted_values) logstash_prefix, index_name, type_name = extracted_values if @flatten_hashes record = flatten_record(record) end dt = nil if @logstash_format || if record.has_key?(TIMESTAMP_FIELD) rts = record[TIMESTAMP_FIELD] dt = parse_time(rts, time, tag) elsif record.has_key?(@time_key) rts = record[@time_key] dt = parse_time(rts, time, tag) record[TIMESTAMP_FIELD] = dt.iso8601(@time_precision) unless else dt = Time.at(time).to_datetime record[TIMESTAMP_FIELD] = dt.iso8601(@time_precision) end end target_index_parent, target_index_child_key = @target_index_key ? get_parent_of(record, @target_index_key) : nil if target_index_parent && target_index_parent[target_index_child_key] target_index = target_index_parent.delete(target_index_child_key) elsif @logstash_format dt = dt.new_offset(0) if @utc_index target_index = "#{logstash_prefix}#{@logstash_prefix_separator}#{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[@tag_key] = tag end target_type_parent, target_type_child_key = @target_type_key ? get_parent_of(record, @target_type_key) : nil if target_type_parent && target_type_parent[target_type_child_key] target_type = target_type_parent.delete(target_type_child_key) if @last_seen_major_version == 6 log.warn "Detected ES 6.x: `@type_name` will be used as the document `_type`." target_type = type_name elsif @last_seen_major_version >= 7 log.warn "Detected ES 7.x or above: `_doc` will be used as the document `_type`." target_type = '_doc'.freeze end else if @last_seen_major_version >= 7 && @type_name != DEFAULT_TYPE_NAME_ES_7x log.warn "Detected ES 7.x or above: `_doc` will be used as the document `_type`." target_type = '_doc'.freeze else target_type = type_name end end .clear ["_index".freeze] = target_index ["_type".freeze] = target_type if @pipeline ["pipeline".freeze] = @pipeline end .each do |record_accessor, | if raw_value = record_accessor.call(record) [] = raw_value end end if @remove_keys @remove_keys.each { |key| record.delete(key) } end return [, header, record] end |
#remove_keys(record) ⇒ Object
532 533 534 535 536 537 538 539 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 532 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_bulk(data, tag, chunk, bulk_message_count, extracted_values, info) ⇒ Object
send_bulk given a specific bulk request, the original tag, chunk, and bulk_message_count
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 708 def send_bulk(data, tag, chunk, , extracted_values, info) begin log.on_trace { log.trace "bulk request: #{data}" } response = client(info.host).bulk body: data, index: info.index log.on_trace { log.trace "bulk response: #{response}" } if response['errors'] error = Fluent::Plugin::ElasticsearchErrorHandler.new(self) error.handle_error(response, tag, chunk, , extracted_values) end rescue RetryStreamError => e emit_tag = @retry_tag ? @retry_tag : tag router.emit_stream(emit_tag, e.retry_stream) rescue => e ignore = @ignore_exception_classes.any? { |clazz| e.class <= clazz } log.warn "Exception ignored in tag #{tag}: #{e.class.name} #{e.message}" if ignore @_es = nil if @reconnect_on_error @_es_info = nil if @reconnect_on_error raise UnrecoverableRequestFailure if ignore && @exception_backup # FIXME: identify unrecoverable errors and raise UnrecoverableRequestFailure instead raise RecoverableRequestFailure, "could not push logs to Elasticsearch cluster (#{connection_options_description(info.host)}): #{e.message}" unless ignore end end |
#update_body(record, op) ⇒ Object
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 516 def update_body(record, op) update = remove_keys(record) if @suppress_doc_wrap return update end body = {"doc".freeze => update} if op == UPSERT_OP if update == record body["doc_as_upsert".freeze] = true else body[UPSERT_OP] = record end end body end |
#write(chunk) ⇒ Object
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 567 def write(chunk) = Hash.new { |h,k| h[k] = 0 } = Hash.new { |h,k| h[k] = '' } header = {} = {} tag = chunk..tag extracted_values = (chunk) host = if @hosts extract_placeholders(@hosts, chunk) else extract_placeholders(@host, chunk) end chunk.msgpack_each do |time, record| next unless record.is_a? Hash begin , header, record = (tag, , header, time, record, extracted_values) info = if @include_index_in_url RequestInfo.new(host, .delete("_index".freeze)) else RequestInfo.new(host, nil) end if (@write_operation, , header, record, [info]) [info] += 1; if [info].size > TARGET_BULK_BYTES .each do |info, msgs| send_bulk(msgs, tag, chunk, [info], extracted_values, info) unless msgs.empty? msgs.clear # Clear bulk_message_count for this info. [info] = 0; next end end else if @emit_error_for_missing_id raise MissingIdFieldError, "Missing '_id' field. Write operation is #{@write_operation}" else log.on_debug { log.debug("Dropping record because its missing an '_id' field and write_operation is #{@write_operation}: #{record}") } end end rescue => e router.emit_error_event(tag, time, record, e) end end .each do |info, msgs| send_bulk(msgs, tag, chunk, [info], extracted_values, info) unless msgs.empty? msgs.clear end end |