Class: Fluent::ViaqDataModelFilter
Constant Summary
ViaqDataModelFilterSystemd::JOURNAL_FIELD_MAP_SYSTEMD_K, ViaqDataModelFilterSystemd::JOURNAL_FIELD_MAP_SYSTEMD_T, ViaqDataModelFilterSystemd::JOURNAL_FIELD_MAP_SYSTEMD_U, ViaqDataModelFilterSystemd::JOURNAL_TIME_FIELDS
Instance Method Summary
collapse
-
#add_elasticsearch_index_name_field(tag, time, record) ⇒ Object
-
#add_pipeline_metadata(tag, time, record) ⇒ Object
-
#check_for_match_and_format(tag, time, record) ⇒ Object
-
#configure(conf) ⇒ Object
-
#delempty(thing) ⇒ Object
recursively delete empty fields and empty lists/hashes from thing.
-
#filter(tag, time, record) ⇒ Object
-
#isempty(thing) ⇒ Object
if thing doesn’t respond to empty? then assume it isn’t empty e.g.
-
#process_k8s_json_file_fields(tag, time, record, fmtr_type = nil) ⇒ Object
-
#process_sys_var_log_fields(tag, time, record, fmtr_type = nil) ⇒ Object
-
#shutdown ⇒ Object
-
#start ⇒ Object
-
#transform_eventrouter(tag, record) ⇒ Object
#process_journal_fields
Instance Method Details
#add_elasticsearch_index_name_field(tag, time, record) ⇒ Object
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 304
def add_elasticsearch_index_name_field(tag, time, record)
found = false
@elasticsearch_index_names.each do |ein|
if ein.matcher.match(tag)
found = true
return unless ein.enabled
if ein.name_type == :operations_full || ein.name_type == :project_full
field_name = @elasticsearch_index_name_field
need_time = true
else
field_name = @elasticsearch_index_prefix_field
need_time = false
end
case ein.name_type
when :operations_full, :operations_prefix
prefix = ".operations"
when :project_full, :project_prefix
if (k8s = record['kubernetes']).nil?
log.error("record cannot use elasticsearch index name type #{ein.name_type}: record is missing kubernetes field: #{record}")
break
elsif (name = k8s['namespace_name']).nil?
log.error("record cannot use elasticsearch index name type #{ein.name_type}: record is missing kubernetes.namespace_name field: #{record}")
break
elsif (uuid = k8s['namespace_id']).nil?
log.error("record cannot use elasticsearch index name type #{ein.name_type}: record is missing kubernetes.namespace_id field: #{record}")
break
else
prefix = "project." + name + "." + uuid
end
end
if ENV['CDM_DEBUG']
unless tag == ENV['CDM_DEBUG_IGNORE_TAG']
log.error("prefix #{prefix} need_time #{need_time} time #{record[@dest_time_name]}")
end
end
if need_time
ts = DateTime.parse(record[@dest_time_name])
record[field_name] = prefix + "." + ts.strftime("%Y.%m.%d")
else
record[field_name] = prefix
end
if ENV['CDM_DEBUG']
unless tag == ENV['CDM_DEBUG_IGNORE_TAG']
log.error("record[#{field_name}] = #{record[field_name]}")
end
end
break
end
end
unless found
if ENV['CDM_DEBUG']
unless tag == ENV['CDM_DEBUG_IGNORE_TAG']
log.error("no match for tag #{tag}")
end
end
end
end
|
291
292
293
294
295
296
297
298
299
300
301
302
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 291
def add_pipeline_metadata (tag, time, record)
record['pipeline_metadata'] = {} unless record.key?('pipeline_metadata')
pipeline_type = @pipeline_type.to_s
record['pipeline_metadata'][pipeline_type] = {} unless record['pipeline_metadata'][pipeline_type].respond_to?(:fetch)
record['pipeline_metadata'][pipeline_type]['ipaddr4'] = @ipaddr4
record['pipeline_metadata'][pipeline_type]['ipaddr6'] = @ipaddr6
record['pipeline_metadata'][pipeline_type]['inputname'] = 'fluent-plugin-systemd'
record['pipeline_metadata'][pipeline_type]['name'] = 'fluentd'
record['pipeline_metadata'][pipeline_type]['received_at'] = Time.now.utc.to_datetime.rfc3339(6)
record['pipeline_metadata'][pipeline_type]['version'] = @pipeline_version
end
|
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 267
def check_for_match_and_format(tag, time, record)
return unless @formatters
return if @formatter_cache_nomatch[tag]
fmtr = @formatter_cache[tag]
unless fmtr
idx = @formatters.index{|fmtr| fmtr.matcher.match(tag)}
if idx && (fmtr = @formatters[idx]).enabled
@formatter_cache[tag] = fmtr
else
@formatter_cache_nomatch[tag] = true
return
end
end
fmtr.fmtr_func.call(tag, time, record, fmtr.fmtr_type)
if record[@dest_time_name].nil? && record['time'].nil?
record['time'] = Time.at(time).utc.to_datetime.rfc3339(6)
end
if fmtr.fmtr_remove_keys
fmtr.fmtr_remove_keys.each{|k| record.delete(k)}
end
end
|
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
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 146
def configure(conf)
super
@keep_fields = {}
@default_keep_fields.each{|xx| @keep_fields[xx] = true}
.each{|xx| @keep_fields[xx] = true}
@keep_empty_fields_hash = {}
@keep_empty_fields.each do |xx|
@keep_empty_fields_hash[xx] = true
@keep_fields[xx] = true
end
if @use_undefined && @keep_fields.key?(@undefined_name)
raise Fluent::ConfigError, "Do not put [#{@undefined_name}] in default_keep_fields or extra_keep_fields"
end
if (@rename_time || @rename_time_if_not_exist) && @use_undefined && !@keep_fields.key?(@src_time_name)
raise Fluent::ConfigError, "Field [#{@src_time_name}] must be listed in default_keep_fields or extra_keep_fields"
end
if @formatters
@formatters.each do |fmtr|
matcher = ViaqMatchClass.new(fmtr.tag, nil)
fmtr.instance_eval{ @params[:matcher] = matcher }
fmtr.instance_eval{ @params[:fmtr_type] = fmtr.type }
if fmtr.remove_keys
fmtr.instance_eval{ @params[:fmtr_remove_keys] = fmtr.remove_keys.split(',') }
else
fmtr.instance_eval{ @params[:fmtr_remove_keys] = nil }
end
case fmtr.type
when :sys_journal, :k8s_journal
fmtr_func = method(:process_journal_fields)
when :sys_var_log
fmtr_func = method(:process_sys_var_log_fields)
when :k8s_json_file
fmtr_func = method(:process_k8s_json_file_fields)
end
fmtr.instance_eval{ @params[:fmtr_func] = fmtr_func }
end
@formatter_cache = {}
@formatter_cache_nomatch = {}
end
begin
@docker_hostname = File.open('/etc/docker-hostname') { |f| f.readline }.rstrip
rescue
@docker_hostname = nil
end
@ipaddr4 = ENV['IPADDR4'] || '127.0.0.1'
@ipaddr6 = ENV['IPADDR6'] || '::1'
@pipeline_version = (ENV['FLUENTD_VERSION'] || 'unknown fluentd version') + ' ' + (ENV['DATA_VERSION'] || 'unknown data version')
unless @elasticsearch_index_names.empty?
@elasticsearch_index_names.each do |ein|
matcher = ViaqMatchClass.new(ein.tag, nil)
ein.instance_eval{ @params[:matcher] = matcher }
end
end
end
|
#delempty(thing) ⇒ Object
recursively delete empty fields and empty lists/hashes from thing
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 217
def delempty(thing)
if thing.respond_to?(:delete_if)
if thing.kind_of? Hash
thing.delete_if{|k,v| v.nil? || isempty(delempty(v)) || isempty(v)}
else
thing.delete_if{|elem| elem.nil? || isempty(delempty(elem)) || isempty(elem)}
end
end
thing
end
|
#filter(tag, time, record) ⇒ Object
382
383
384
385
386
387
388
389
390
391
392
393
394
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
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 382
def filter(tag, time, record)
if ENV['CDM_DEBUG']
unless tag == ENV['CDM_DEBUG_IGNORE_TAG']
log.error("input #{time} #{tag} #{record}")
end
end
check_for_match_and_format(tag, time, record)
add_pipeline_metadata(tag, time, record)
if @use_undefined
undefined = record.reject{|k,v| @keep_fields.key?(k)}
unless undefined.empty?
record[@undefined_name] = undefined
record.delete_if{|k,v| undefined.key?(k)}
end
end
record.delete_if{|k,v| !@keep_empty_fields_hash.key?(k) && (v.nil? || isempty(delempty(v)) || isempty(v))}
log.warn("Empty record! tag [#{tag}] time [#{time}]") if record.empty?
if (@rename_time || @rename_time_if_missing) && record.key?(@src_time_name)
val = record.delete(@src_time_name)
unless @rename_time_if_missing && record.key?(@dest_time_name)
record[@dest_time_name] = val
end
end
if !@elasticsearch_index_names.empty?
add_elasticsearch_index_name_field(tag, time, record)
elsif ENV['CDM_DEBUG']
unless tag == ENV['CDM_DEBUG_IGNORE_TAG']
log.error("not adding elasticsearch index name or prefix")
end
end
if ENV['CDM_DEBUG']
unless tag == ENV['CDM_DEBUG_IGNORE_TAG']
log.error("output #{time} #{tag} #{record}")
end
end
record
end
|
#isempty(thing) ⇒ Object
if thing doesn’t respond to empty? then assume it isn’t empty e.g. 0.respond_to?(:empty?) == false - the FixNum 0 is not empty
212
213
214
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 212
def isempty(thing)
thing.respond_to?(:empty?) && thing.empty?
end
|
#process_k8s_json_file_fields(tag, time, record, fmtr_type = nil) ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 245
def process_k8s_json_file_fields(tag, time, record, fmtr_type = nil)
record['message'] = record['message'] || record['log']
record['level'] = (record['stream'] == 'stdout') ? 'info' : 'err'
if record.key?('kubernetes') && record['kubernetes'].respond_to?(:fetch) && \
(k8shost = record['kubernetes'].fetch('host', nil))
record['hostname'] = k8shost
elsif @docker_hostname
record['hostname'] = @docker_hostname
end
if record[@dest_time_name].nil?
unless record['time'].nil?
rectime = Time.parse(record['time'])
else
rectime = Time.at(time)
end
record['time'] = rectime.utc.to_datetime.rfc3339(6)
end
transform_eventrouter(tag, record)
end
|
#process_sys_var_log_fields(tag, time, record, fmtr_type = nil) ⇒ Object
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 228
def process_sys_var_log_fields(tag, time, record, fmtr_type = nil)
record['systemd'] = {"t" => {"PID" => record['pid']}, "u" => {"SYSLOG_IDENTIFIER" => record['ident']}}
if record[@dest_time_name].nil?
timeobj = Time.at(time)
if timeobj > Time.now
timeobj = Time.new((timeobj.year - 1), timeobj.month, timeobj.day, timeobj.hour, timeobj.min, timeobj.sec, timeobj.utc_offset)
end
record['time'] = timeobj.utc.to_datetime.rfc3339(6)
end
if record['host'].eql?('localhost') && @docker_hostname
record['hostname'] = @docker_hostname
else
record['hostname'] = record['host']
end
end
|
#shutdown ⇒ Object
206
207
208
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 206
def shutdown
super
end
|
#start ⇒ Object
202
203
204
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 202
def start
super
end
|
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
# File 'lib/fluent/plugin/filter_viaq_data_model.rb', line 366
def transform_eventrouter(tag, record)
return unless @process_kubernetes_events
if record.key?("event")
if record.key?("verb")
record["event"]["verb"] = record.delete("verb")
end
record["kubernetes"] = {} unless record.key?("kubernetes")
record["kubernetes"]["event"] = record.delete("event")
if record["kubernetes"]["event"].key?('message')
((record['pipeline_metadata'] ||= {})[@pipeline_type.to_s] ||= {})['original_raw_message'] = record['message']
end
record['message'] = record["kubernetes"]["event"].delete("message")
record['time'] = record["kubernetes"]["event"]["metadata"].delete("creationTimestamp")
end
end
|