Class: Fluent::Plugin::ElasticsearchOutputDynamic
- Inherits:
-
ElasticsearchOutput
- Object
- Output
- ElasticsearchOutput
- Fluent::Plugin::ElasticsearchOutputDynamic
- Defined in:
- lib/fluent/plugin/out_elasticsearch_dynamic.rb
Defined Under Namespace
Classes: RequestInfo
Constant Summary collapse
- DYNAMIC_PARAM_NAMES =
%W[hosts host port include_timestamp logstash_format logstash_prefix logstash_dateformat time_key utc_index index_name tag_key type_name id_key parent_key routing_key write_operation]
- DYNAMIC_PARAM_SYMBOLS =
DYNAMIC_PARAM_NAMES.map { |n| "@#{n}".to_sym }
Constants inherited from ElasticsearchOutput
Fluent::Plugin::ElasticsearchOutput::DEFAULT_BUFFER_TYPE, Fluent::Plugin::ElasticsearchOutput::DEFAULT_ELASTICSEARCH_VERSION, Fluent::Plugin::ElasticsearchOutput::DEFAULT_POLICY_ID, Fluent::Plugin::ElasticsearchOutput::DEFAULT_RELOAD_AFTER, Fluent::Plugin::ElasticsearchOutput::DEFAULT_TARGET_BULK_BYTES, Fluent::Plugin::ElasticsearchOutput::DEFAULT_TYPE_NAME, Fluent::Plugin::ElasticsearchOutput::DEFAULT_TYPE_NAME_ES_7x
Constants included from ElasticsearchTLS
Fluent::Plugin::ElasticsearchTLS::DEFAULT_VERSION, Fluent::Plugin::ElasticsearchTLS::SUPPORTED_TLS_VERSIONS
Constants included from ElasticsearchIndexLifecycleManagement
Fluent::Plugin::ElasticsearchIndexLifecycleManagement::ILM_DEFAULT_POLICY_PATH
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 Attribute Summary collapse
-
#dynamic_config ⇒ Object
readonly
Returns the value of attribute dynamic_config.
Attributes inherited from ElasticsearchOutput
#alias_indexes, #api_key_header, #compressable_connection, #ssl_version_options, #template_names
Instance Method Summary collapse
- #client(host = nil, compress_connection = false) ⇒ Object
- #configure(conf) ⇒ Object
- #connection_options_description(host) ⇒ Object
- #create_meta_config_map ⇒ Object
- #eval_or_val(var) ⇒ Object
- #expand_param(param, tag, time, record) ⇒ Object
- #get_connection_options(con_host) ⇒ Object
- #is_valid_expand_param_type(param) ⇒ Object
- #multi_workers_ready? ⇒ Boolean
- #send_bulk(data, host, index) ⇒ Object
- #write(chunk) ⇒ Object
Methods inherited from ElasticsearchOutput
#append_record_to_messages, #backend_options, #client_library_version, #cloud_client, #compression, #compression_strategy, #configure_routing_key_name, #convert_compat_id_key, #convert_numeric_time_into_string, #create_time_parser, #detect_es_major_version, #dry_run?, #expand_placeholders, #flatten_record, #get_affinity_target_indices, #get_escaped_userinfo, #get_parent_of, #gzip, #handle_last_seen_es_major_version, #initialize, #inject_chunk_id_to_record_if_needed, #is_existing_connection, #is_ipv6_host, #parse_time, #placeholder?, #placeholder_substitution_needed_for_template?, #process_message, #remove_keys, #retry_stream_retryable?, #setup_api_key, #split_request?, #split_request_size_check?, #split_request_size_uncheck?, #target_index_affinity_enabled?, #template_installation, #template_installation_actual, #update_body
Methods included from ElasticsearchTLS
included, #set_tls_minmax_version_config
Methods included from ElasticsearchIndexLifecycleManagement
#create_ilm_policy, #default_policy_payload, #get_ilm_policy, #ilm_policy_exists?, #ilm_policy_put, #setup_ilm, #verify_ilm_working, #xpack_info
Methods included from ElasticsearchIndexTemplate
#create_rollover_alias, #get_custom_template, #get_template, #get_template_name, #host_unreachable_exceptions, #indexcreation, #inject_ilm_settings_to_template, #retry_operate, #rollover_alias_payload, #template_custom_install, #template_exists?, #template_install, #template_put, #templates_hash_install
Constructor Details
This class inherits a constructor from Fluent::Plugin::ElasticsearchOutput
Instance Attribute Details
#dynamic_config ⇒ Object (readonly)
Returns the value of attribute dynamic_config.
18 19 20 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 18 def dynamic_config @dynamic_config end |
Instance Method Details
#client(host = nil, compress_connection = false) ⇒ Object
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 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 40 def client(host = nil, compress_connection = false) # check here to see if we already have a client connection for the given host = (host) @_es = nil unless is_existing_connection([:hosts]) @_es = nil unless @compressable_connection == compress_connection @_es ||= begin @compressable_connection = compress_connection @current_config = [:hosts].clone adapter_conf = lambda {|f| f.adapter @http_backend, @backend_options } gzip_headers = if compress_connection {'Content-Encoding' => 'gzip'} else {} end headers = { 'Content-Type' => @content_type.to_s, }.merge(gzip_headers) = { verify: @ssl_verify, ca_file: @ca_file}.merge(@ssl_version_options) transport = TRANSPORT_CLASS::Transport::HTTP::Faraday.new(.merge( options: { reload_connections: @reload_connections, reload_on_failure: @reload_on_failure, resurrect_after: @resurrect_after, logger: @transport_logger, transport_options: { headers: headers, request: { timeout: @request_timeout }, ssl: , }, http: { user: @user, password: @password, scheme: @scheme }, compression: compress_connection, }), &adapter_conf) Elasticsearch::Client.new transport: transport end end |
#configure(conf) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 20 def configure(conf) super # evaluate all configurations here @dynamic_config = {} DYNAMIC_PARAM_SYMBOLS.each_with_index { |var, i| value = (self.instance_variable_get(var), nil, nil, nil) key = DYNAMIC_PARAM_NAMES[i] @dynamic_config[key] = value.to_s } # end eval all configs log.warn "Elasticsearch dynamic plugin will be deprecated and removed in the future. Please consider to use normal Elasticsearch plugin" end |
#connection_options_description(host) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 113 def (host) (host)[:hosts].map do |host_info| attributes = host_info.dup attributes[:password] = 'obfuscated' if attributes.has_key?(:password) attributes.inspect end.join(', ') end |
#create_meta_config_map ⇒ Object
35 36 37 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 35 def {'id_key' => '_id', 'parent_key' => '_parent', 'routing_key' => @routing_key_name} end |
#eval_or_val(var) ⇒ Object
246 247 248 249 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 246 def eval_or_val(var) return var unless var.is_a?(String) eval(var) end |
#expand_param(param, tag, time, record) ⇒ Object
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 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 251 def (param, tag, time, record) # check for '${ ... }' # yes => `eval` # no => return param return param if (param.to_s =~ /\${.+}/).nil? # check for 'tag_parts[]' # separated by a delimiter (default '.') tag_parts = tag.split(@delimiter) unless (param =~ /tag_parts\[.+\]/).nil? || tag.nil? # pull out section between ${} then eval inner = param.clone while inner.match(/\${.+}/) to_eval = inner.match(/\${(.+?)}/){$1} if !(to_eval =~ /record\[.+\]/).nil? && record.nil? return to_eval elsif !(to_eval =~/tag_parts\[.+\]/).nil? && tag_parts.nil? return to_eval elsif !(to_eval =~/time/).nil? && time.nil? return to_eval else inner.sub!(/\${.+?}/, eval( to_eval )) end end inner end |
#get_connection_options(con_host) ⇒ Object
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 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 80 def (con_host) 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.to_i, 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 |
#is_valid_expand_param_type(param) ⇒ Object
279 280 281 282 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 279 def (param) return false if [:@buffer_type].include?(param) return self.instance_variable_get(param).is_a?(String) end |
#multi_workers_ready? ⇒ Boolean
121 122 123 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 121 def multi_workers_ready? true end |
#send_bulk(data, host, index) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 228 def send_bulk(data, host, index) begin prepared_data = if compression gzip(data) else data end response = client(host, compression).bulk body: prepared_data, index: index if response['errors'] log.error "Could not push log to Elasticsearch: #{response}" end rescue => e @_es = nil if @reconnect_on_error # FIXME: identify unrecoverable errors and raise UnrecoverableRequestFailure instead raise RecoverableRequestFailure, "could not push logs to Elasticsearch cluster (#{(host)}): #{e.}" end end |
#write(chunk) ⇒ Object
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 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 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 125 def write(chunk) = Hash.new { |h,k| h[k] = '' } dynamic_conf = @dynamic_config.clone headers = { UPDATE_OP => {}, UPSERT_OP => {}, CREATE_OP => {}, INDEX_OP => {} } tag = chunk..tag chunk.msgpack_each do |time, record| next unless record.is_a? Hash if @flatten_hashes record = flatten_record(record) end begin # evaluate all configurations here DYNAMIC_PARAM_SYMBOLS.each_with_index { |var, i| k = DYNAMIC_PARAM_NAMES[i] v = self.instance_variable_get(var) # check here to determine if we should evaluate if dynamic_conf[k] != v value = (v, tag, time, record) dynamic_conf[k] = value end } # end eval all configs rescue => e # handle dynamic parameters misconfigurations router.emit_error_event(tag, time, record, e) next end if eval_or_val(dynamic_conf['logstash_format']) || eval_or_val(dynamic_conf['include_timestamp']) if record.has_key?("@timestamp") time = Time.parse record["@timestamp"] elsif record.has_key?(dynamic_conf['time_key']) time = Time.parse record[dynamic_conf['time_key']] record['@timestamp'] = record[dynamic_conf['time_key']] unless else record.merge!({"@timestamp" => Time.at(time).iso8601(@time_precision)}) end end if eval_or_val(dynamic_conf['logstash_format']) if eval_or_val(dynamic_conf['utc_index']) target_index = "#{dynamic_conf['logstash_prefix']}#{@logstash_prefix_separator}#{Time.at(time).getutc.strftime("#{dynamic_conf['logstash_dateformat']}")}" else target_index = "#{dynamic_conf['logstash_prefix']}#{@logstash_prefix_separator}#{Time.at(time).strftime("#{dynamic_conf['logstash_dateformat']}")}" end else target_index = dynamic_conf['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!(dynamic_conf['tag_key'] => tag) end if dynamic_conf['hosts'] host = dynamic_conf['hosts'] else host = "#{dynamic_conf['host']}:#{dynamic_conf['port']}" end if @include_index_in_url key = RequestInfo.new(host, target_index) = {"_type" => dynamic_conf['type_name']} else key = RequestInfo.new(host, nil) = {"_index" => target_index, "_type" => dynamic_conf['type_name']} end @meta_config_map.each_pair do |config_name, | if dynamic_conf[config_name] && accessor = record_accessor_create(dynamic_conf[config_name]) if raw_value = accessor.call(record) [] = raw_value end end end if @remove_keys @remove_keys.each { |key| record.delete(key) } end write_op = dynamic_conf["write_operation"] (write_op, , headers[write_op], record, [key]) end .each do |info, msgs| send_bulk(msgs, info.host, info.index) unless msgs.empty? msgs.clear end end |