Class: Fluent::Plugin::ElasticsearchOutputDynamic
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 }
Fluent::Plugin::ElasticsearchOutput::DEFAULT_BUFFER_TYPE
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
Instance Method Summary
collapse
#generate_hash_id_key, included
#append_record_to_messages, #create_time_parser, #expand_placeholders, #flatten_record, #get_escaped_userinfo, #get_parent_of, #initialize, #parse_time, #remove_keys, #update_body
#get_template, #template_exists?, #template_install, #template_put, #templates_hash_install
Instance Attribute Details
#dynamic_config ⇒ Object
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) ⇒ Object
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
68
69
70
71
72
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 38
def client(host)
connection_options = get_connection_options(host)
@_es = nil unless is_existing_connection(connection_options[:hosts])
@_es ||= begin
@current_config = connection_options[:hosts].clone
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(connection_options.merge(
options: {
reload_connections: @reload_connections,
reload_on_failure: @reload_on_failure,
resurrect_after: @resurrect_after,
retry_on_failure: 5,
transport_options: {
headers: { 'Content-Type' => 'application/json' },
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(host)})!" unless es.ping
rescue *es.transport.host_unreachable_exceptions => e
raise ConnectionFailure, "Can not reach Elasticsearch cluster (#{connection_options_description(host)})! #{e.message}"
end
log.info "Connection opened to Elasticsearch cluster => #{connection_options_description(host)}"
es
end
end
|
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 20
def configure(conf)
super
@dynamic_config = {}
DYNAMIC_PARAM_SYMBOLS.each_with_index { |var, i|
value = expand_param(self.instance_variable_get(var), nil, nil, nil)
key = DYNAMIC_PARAM_NAMES[i]
@dynamic_config[key] = value.to_s
}
@current_config = nil
end
|
#connection_options_description(host) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 107
def connection_options_description(host)
get_connection_options(host)[:hosts].map do |host_info|
attributes = host_info.dup
attributes[:password] = 'obfuscated' if attributes.has_key?(:password)
attributes.inspect
end.join(', ')
end
|
34
35
36
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 34
def create_meta_config_map
{'id_key' => '_id', 'parent_key' => '_parent', 'routing_key' => '_routing'}
end
|
#eval_or_val(var) ⇒ Object
236
237
238
239
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 236
def eval_or_val(var)
return var unless var.is_a?(String)
eval(var)
end
|
#expand_param(param, tag, time, record) ⇒ Object
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
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 241
def expand_param(param, tag, time, record)
return param if (param =~ /\${.+}/).nil?
tag_parts = tag.split(@delimiter) unless (param =~ /tag_parts\[.+\]/).nil? || tag.nil?
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
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
101
102
103
104
105
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 74
def get_connection_options(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|
if host_str.match(%r{^[^:]+(\:\d+)?$})
{
host: host_str.split(':')[0],
port: (host_str.split(':')[1] || @port).to_i,
scheme: @scheme
}
else
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}]
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_existing_connection(host) ⇒ Object
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 274
def is_existing_connection(host)
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
269
270
271
272
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 269
def is_valid_expand_param_type(param)
return false if [:@buffer_type].include?(param)
return self.instance_variable_get(param).is_a?(String)
end
|
#multi_workers_ready? ⇒ Boolean
115
116
117
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 115
def multi_workers_ready?
true
end
|
#send_bulk(data, host) ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 214
def send_bulk(data, host)
retries = 0
begin
response = client(host).bulk body: data
if response['errors']
log.error "Could not push log to Elasticsearch: #{response}"
end
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.message}"
sleep 2**retries
retry
end
raise ConnectionFailure, "Could not push logs to Elasticsearch after #{retries} retries. #{e.message}"
rescue Exception
@_es = nil if @reconnect_on_error
raise
end
end
|
#write(chunk) ⇒ Object
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
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
|
# File 'lib/fluent/plugin/out_elasticsearch_dynamic.rb', line 119
def write(chunk)
bulk_message = Hash.new { |h,k| h[k] = '' }
dynamic_conf = @dynamic_config.clone
= {
UPDATE_OP => {},
UPSERT_OP => {},
CREATE_OP => {},
INDEX_OP => {}
}
tag = chunk.metadata.tag
chunk.msgpack_each do |time, record|
next unless record.is_a? Hash
if @hash_config
record = generate_hash_id_key(record)
end
begin
DYNAMIC_PARAM_SYMBOLS.each_with_index { |var, i|
k = DYNAMIC_PARAM_NAMES[i]
v = self.instance_variable_get(var)
if dynamic_conf[k] != v
value = expand_param(v, tag, time, record)
dynamic_conf[k] = value
end
}
rescue => e
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 time_key_exclude_timestamp
else
record.merge!({"@timestamp" => Time.at(time).to_datetime.to_s})
end
end
if eval_or_val(dynamic_conf['logstash_format'])
if eval_or_val(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
target_index = target_index.downcase
if @include_tag_key
record.merge!(dynamic_conf['tag_key'] => tag)
end
meta = {"_index" => target_index, "_type" => dynamic_conf['type_name']}
@meta_config_map.each_pair do |config_name, meta_key|
if dynamic_conf[config_name] && record[dynamic_conf[config_name]]
meta[meta_key] = record[dynamic_conf[config_name]]
end
end
if dynamic_conf['hosts']
host = dynamic_conf['hosts']
else
host = "#{dynamic_conf['host']}:#{dynamic_conf['port']}"
end
if @remove_keys
@remove_keys.each { |key| record.delete(key) }
end
write_op = dynamic_conf["write_operation"]
append_record_to_messages(write_op, meta, [write_op], record, bulk_message[host])
end
bulk_message.each do |hKey, msgs|
send_bulk(msgs, hKey) unless msgs.empty?
msgs.clear
end
end
|