Class: Fluent::ElasticsearchOutputDynamic
- Inherits:
-
ElasticsearchOutput
- Object
- ElasticsearchOutput
- Fluent::ElasticsearchOutputDynamic
- Defined in:
- lib/fluent/plugin/out_elasticsearch_dynamic.rb
Instance Method Summary collapse
- #client(host) ⇒ Object
- #configure(conf) ⇒ Object
- #connection_options_description(host) ⇒ Object
- #expand_param(param, tag, time, record) ⇒ Object
- #get_connection_options(con_host) ⇒ Object
- #is_existing_connection(host) ⇒ Object
- #is_valid_expand_param_type(param) ⇒ Object
- #send(data, host) ⇒ Object
- #write(chunk) ⇒ Object
Instance Method Details
#client(host) ⇒ Object
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 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 34 def client(host) # 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 = { client_key: @dynamic_config['client_key'], client_cert: @dynamic_config['client_cert'], client_key_pass: @dynamic_config['client_key_pass'] } adapter_conf = lambda {|f| f.adapter :excon, } transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(.merge( options: { reload_connections: @dynamic_config['reload_connections'], reload_on_failure: @dynamic_config['reload_on_failure'], resurrect_after: @dynamic_config['resurrect_after'].to_i, retry_on_failure: 5, transport_options: { request: { timeout: @dynamic_config['request_timeout'] }, ssl: { verify: @dynamic_config['ssl_verify'], ca_file: @dynamic_config['ca_file'] } } }), &adapter_conf) es = Elasticsearch::Client.new transport: transport begin raise ConnectionFailure, "Can not reach Elasticsearch cluster (#{(host)})!" unless es.ping rescue *es.transport.host_unreachable_exceptions => e raise ConnectionFailure, "Can not reach Elasticsearch cluster (#{(host)})! #{e.}" end log.info "Connection opened to Elasticsearch cluster => #{(host)}" es end end |
#configure(conf) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 19 def configure(conf) super # evaluate all configurations here @dynamic_params = self.instance_variables.select { |var| (var) } @dynamic_config = Hash.new @dynamic_params.each { |var| value = (self.instance_variable_get(var), nil, nil, nil) var = var[1..-1] @dynamic_config[var] = value } # end eval all configs @current_config = nil end |
#connection_options_description(host) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 102 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 |
#expand_param(param, tag, time, record) ⇒ Object
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 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 194 def (param, tag, time, record) # check for '${ ... }' # yes => `eval` # no => return param return param if (param =~ /\${.+}/).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
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 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 69 def (con_host) raise "`password` must be present if `user` is present" if @dynamic_config['user'] && !@dynamic_config['password'] hosts = if con_host || @dynamic_config['hosts'] (con_host || @dynamic_config['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] || @dynamic_config['port']).to_i, scheme: @dynamic_config['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: @dynamic_config['host'], port: @dynamic_config['port'].to_i, scheme: @dynamic_config['scheme']}] end.each do |host| host.merge!(user: @dynamic_config['user'], password: @dynamic_config['password']) if !host[:user] && @dynamic_config['user'] host.merge!(path: @dynamic_config['path']) if !host[:path] && @dynamic_config['path'] end { hosts: hosts } end |
#is_existing_connection(host) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 227 def is_existing_connection(host) # check if the host provided match the current connection return false if @_es.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 |
#is_valid_expand_param_type(param) ⇒ Object
222 223 224 225 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 222 def (param) return false if [:@buffer_type].include?(param) return self.instance_variable_get(param).is_a?(String) end |
#send(data, host) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 178 def send(data, host) retries = 0 begin client(host).bulk body: data rescue *client(host).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.}" sleep 2**retries retry end raise ConnectionFailure, "Could not push logs to Elasticsearch after #{retries} retries. #{e.}" end end |
#write(chunk) ⇒ Object
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 |
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 110 def write(chunk) = Hash.new { |h,k| h[k] = [] } dynamic_conf = @dynamic_config.clone chunk.msgpack_each do |tag, time, record| next unless record.is_a? Hash # evaluate all configurations here @dynamic_params.each { |var| k = var[1..-1] 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 if eval(dynamic_conf['logstash_format']) 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']] else record.merge!({"@timestamp" => Time.at(time).to_datetime.to_s}) end if eval(dynamic_conf['utc_index']) target_index = "#{dynamic_conf['logstash_prefix']}-#{Time.at(time).getutc.strftime("#{dynamic_conf['logstash_dateformat']}")}" else target_index = "#{dynamic_conf['logstash_prefix']}-#{Time.at(time).strftime("#{dynamic_conf['logstash_dateformat']}")}" end else target_index = dynamic_conf['index_name'] end if @include_tag_key record.merge!(dynamic_conf['tag_key'] => tag) end = { "index" => {"_index" => target_index, "_type" => dynamic_conf['type_name']} } if dynamic_conf['id_key'] && record[dynamic_conf['id_key']] ['index']['_id'] = record[dynamic_conf['id_key']] end if dynamic_conf['parent_key'] && record[dynamic_conf['parent_key']] ['index']['_parent'] = record[dynamic_conf['parent_key']] end if dynamic_conf['hosts'] host = dynamic_conf['hosts'] else host = "#{dynamic_conf['host']}:#{dynamic_conf['port']}" end [host] << [host] << record end .each do | hKey, array | send(array, hKey) unless array.empty? array.clear end end |