lib/fluent/plugin/gelf_plugin_util.rb in fluent-plugin-gelf-best-1.3.2 vs lib/fluent/plugin/gelf_plugin_util.rb in fluent-plugin-gelf-best-1.3.3

- old
+ new

@@ -1,28 +1,26 @@ -require 'oj' -require 'date' -require 'gelf' - module Fluent module GelfPluginUtil + require "gelf" + require "date" LEVEL_MAP = { - '0' => GELF::UNKNOWN, '1' => GELF::UNKNOWN, 'a' => GELF::UNKNOWN, - '2' => GELF::FATAL, 'c' => GELF::FATAL, - '3' => GELF::ERROR, - '4' => GELF::WARN, 'w' => GELF::WARN, - '5' => GELF::INFO, 'n' => GELF::INFO, - '6' => GELF::INFO, 'i' => GELF::INFO, - '7' => GELF::DEBUG, 'd' => GELF::DEBUG, - 'e' => GELF::ERROR + "0" => GELF::UNKNOWN, "1" => GELF::UNKNOWN, "a" => GELF::UNKNOWN, + "2" => GELF::FATAL, "c" => GELF::FATAL, + "3" => GELF::ERROR, + "4" => GELF::WARN, "w" => GELF::WARN, + "5" => GELF::INFO, "n" => GELF::INFO, + "6" => GELF::INFO, "i" => GELF::INFO, + "7" => GELF::DEBUG, "d" => GELF::DEBUG, + "e" => GELF::ERROR # assuming 'e' stands typically for 'error' }.freeze def make_gelfentry(tag, time, record, conf = {}) - gelfentry = {"_fluentd_tag" => tag, "timestamp" => calculate_timestamp(time)} + gelfentry = {'_fluentd_tag' => tag, 'timestamp' => calculate_timestamp(time)} - record.each do |k, v| - process_record_entry(k, v, conf, gelfentry) + record.each_pair do |k, v| + gelfentry.merge!(process_record_entry(k, v, conf, gelfentry)) end ensure_short_message(gelfentry) gelfentry.compact end @@ -38,31 +36,45 @@ end def process_record_entry(k, v, conf, gelfentry) case k when 'host', 'hostname' - gelfentry['host'] = conf[:use_record_host] ? v : (gelfentry['_host'] = v) + return {'host' => (conf[:use_record_host] ? v : gelfentry['_host'] = v)} when 'timestamp', 'time' - gelfentry['timestamp'] = parse_timestamp(v) + { 'timestamp' => parse_timestamp(v) } when 'level' - gelfentry['level'] = LEVEL_MAP[v.to_s.downcase[0]] || GELF::UNKNOWN + {'level' => LEVEL_MAP[v.to_s.downcase[0]] || (v.to_s.length >= 2 && v.to_s.downcase[1] != "r" ? GELF::UNKNOWN : v)} when 'msec' - gelfentry['timestamp'] = conf[:add_msec_time] ? "#{time.to_s}.#{v}".to_f : (gelfentry['_msec'] = v) + conf[:add_msec_time] ? {'timestamp' => "#{time.to_s}.#{v}".to_f} : {'_msec' => v} when 'short_message', 'version', 'full_message', 'facility', 'file', 'line' - gelfentry[k] = v + {k => v} else - gelfentry[k.start_with?('_') ? k : "_#{k}"] = v + {k.start_with?('_') ? k : "_#{k}" => v} end end def parse_timestamp(v) - return v if v.is_a?(Integer) || v.is_a?(Float) - - DateTime.parse(v).strftime("%Q").to_f / 1_000 rescue v + if v.is_a?(Integer) || v.is_a?(Float) + v + else + begin + (DateTime.parse(v).strftime("%Q").to_f / 1_000).round(3) + rescue ArgumentError + v + end + end end def ensure_short_message(gelfentry) - default_key = ['_message', '_msg', '_log', '_record'].find { |key| gelfentry[key]&.strip&.empty? == false } - gelfentry['short_message'] = default_key ? gelfentry.delete(default_key) : '(no message)' + return if gelfentry['short_message'] && !gelfentry['short_message'].to_s.strip.empty? + + ['_message', '_msg', '_log', '_record'].each do |key| + if gelfentry[key] && !gelfentry[key].to_s.strip.empty? + gelfentry['short_message'] = gelfentry.delete(key) + return + end + end + + gelfentry['short_message'] = '(no message)' unless gelfentry['short_message'] end end end