fluent-plugin-gelf-best 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16f39189270e1bd23e196155e2a7bbead5ceb5620f559404198bc212b30eebef
4
- data.tar.gz: 9fd452c44a244e8bdea46b8db8c498643b8bc47212c0bd339aee7cf111bc7afe
3
+ metadata.gz: b3d0be1e13c195cc69d517d9678fd9ed15128700a203f6fcded95a8acf26ffa5
4
+ data.tar.gz: b5f48294ede888f480c827fcfeb635ae0769631c8e77b0329773927ca64a0aef
5
5
  SHA512:
6
- metadata.gz: cdcb16ce91a9946ea5e4dfd5977a103c5eb1e81e571376a2562add5312f302532f0939c389a039cb8ef01c4ece2c198eec73b41140fa88eda3b16e9470ea0d46
7
- data.tar.gz: eb382d4a3682754643eefbf1cb16853b5f47baa1d8ffe347e628e4a1ba32116cc25aa5f42296ccf10ad64ce193191ecac45bd493435269d97fa13b78176e94ca
6
+ metadata.gz: 1d0a6549e8a686fca58906ca27fd153e7e6d84cac711c59a7e0312f039ad591815567d08d55724d1a88c1d7ef3ae3acef214a652fae50aac09a3fc6145aae24d
7
+ data.tar.gz: 60bebae56b82025efa2f141be0842d1fe966d47d0d633fbe2e18bce7dbb7718635425150ae4bce2c69713b6a6b814efd6a63af508599ea16b293c2086d74c7dc
@@ -4,7 +4,7 @@ require "yajl"
4
4
 
5
5
  module Fluent
6
6
  module TextFormatter
7
- class GELFFormatter < Formatter
7
+ class GelfFormatter < Formatter
8
8
 
9
9
  unless method_defined?(:log)
10
10
  define_method('log') { $log }
@@ -1,91 +1,88 @@
1
- # encoding=utf-8
1
+ require 'oj'
2
+ require 'date'
3
+ require 'gelf'
4
+
2
5
  module Fluent
3
6
  module GelfPluginUtil
4
7
 
5
- require 'gelf'
8
+ LEVEL_MAP = {
9
+ '0' => GELF::UNKNOWN, '1' => GELF::UNKNOWN, 'a' => GELF::UNKNOWN,
10
+ '2' => GELF::FATAL, 'c' => GELF::FATAL,
11
+ '3' => GELF::ERROR,
12
+ '4' => GELF::WARN, 'w' => GELF::WARN,
13
+ '5' => GELF::INFO, 'n' => GELF::INFO,
14
+ '6' => GELF::INFO, 'i' => GELF::INFO,
15
+ '7' => GELF::DEBUG, 'd' => GELF::DEBUG,
16
+ 'e' => GELF::ERROR
17
+ }.freeze
6
18
 
7
- def make_gelfentry(tag,time,record, conf = {})
8
- gelfentry = { '_tag' => tag }
9
- if defined? Fluent::EventTime and time.is_a? Fluent::EventTime then
10
- gelfentry['timestamp'] = time.sec + (time.nsec.to_f/1000000000).round(3)
11
- else
12
- gelfentry['timestamp'] = time
19
+ def get_internal_json_out(record, key)
20
+ json_data = parse_json_field(record[key])
21
+ return record unless json_data
22
+
23
+ record.delete(key)
24
+ merge_record_with_json(record, json_data)
25
+ rescue Oj::ParseError, EncodingError
26
+ # Return original record if JSON parsing fails
27
+ record
28
+ end
29
+
30
+ def make_gelfentry(tag, time, record, conf = {})
31
+ record = get_internal_json_out(record, "message")
32
+ record = get_internal_json_out(record, "log")
33
+ gelfentry = {"_fluentd_tag" => tag, "timestamp" => calculate_timestamp(time)}
34
+
35
+ record.each do |k, v|
36
+ process_record_entry(k, v, conf, gelfentry)
13
37
  end
14
38
 
15
- record.each_pair do |k,v|
16
- case k
17
- when 'host' then
18
- if conf[:use_record_host] then
19
- gelfentry['host'] = v
20
- else
21
- gelfentry['_host'] = v
22
- end
23
- when 'level' then
24
- case v.to_s.downcase[0]
25
- # emergency and alert aren't supported by gelf-rb
26
- when "0" then
27
- gelfentry['level'] = GELF::UNKNOWN
28
- when "1", "a" then
29
- gelfentry['level'] = GELF::UNKNOWN
30
- when "2", "c" then
31
- gelfentry['level'] = GELF::FATAL
32
- when "3" then
33
- gelfentry['level'] = GELF::ERROR
34
- when "4", "w" then
35
- gelfentry['level'] = GELF::WARN
36
- # gelf-rb also skips notice
37
- when "5", "n" then
38
- gelfentry['level'] = GELF::INFO
39
- when "6", "i" then
40
- gelfentry['level'] = GELF::INFO
41
- when "7", "d" then
42
- gelfentry['level'] = GELF::DEBUG
43
- when "e" then
44
- if v.to_s.length >= 2 and v.to_s.downcase[1] != "r" then
45
- gelfentry['level'] = GELF::UNKNOWN
46
- else
47
- gelfentry['level'] = GELF::ERROR
48
- end
49
- else
50
- gelfentry['_level'] = v
51
- end
52
- when 'msec' then
53
- # msec must be three digits (leading/trailing zeroes)
54
- if conf[:add_msec_time] then
55
- gelfentry['timestamp'] = "#{time.to_s}.#{v}".to_f
56
- else
57
- gelfentry['_msec'] = v
58
- end
59
- when 'short_message', 'full_message', 'facility', 'line', 'file' then
60
- gelfentry[k] = v
61
- else
62
- if !k.start_with?('_')
63
- gelfentry['_'+k] = v
64
- else
65
- gelfentry[k] = v
66
- end
67
- end
39
+ ensure_short_message(gelfentry)
40
+ gelfentry.compact
41
+ end
42
+
43
+ private
44
+
45
+ def parse_json_field(json_field)
46
+ return nil unless json_field
47
+ json = Oj.load(json_field.strip)
48
+ return nil unless json.is_a?(Hash)
49
+ json
50
+ end
51
+
52
+ def calculate_timestamp(time)
53
+ if defined?(Fluent::EventTime) && time.is_a?(Fluent::EventTime)
54
+ time.sec + (time.nsec.to_f / 1_000_000_000).round(3)
55
+ else
56
+ time
68
57
  end
58
+ end
69
59
 
70
- if !gelfentry.key?('short_message') or gelfentry['short_message'].to_s.strip.empty? then
71
- # allow other non-empty fields to masquerade as the short_message if it is unset
72
- if gelfentry.key?('_message') and !gelfentry['_message'].to_s.strip.empty? then
73
- gelfentry['short_message'] = gelfentry.delete('_message')
74
- elsif gelfentry.key?('_msg') and !gelfentry['_msg'].to_s.strip.empty? then
75
- gelfentry['short_message'] = gelfentry.delete('_msg')
76
- elsif gelfentry.key?('_log') and !gelfentry['_log'].to_s.strip.empty? then
77
- gelfentry['short_message'] = gelfentry.delete('_log')
78
- elsif gelfentry.key?('_record') and !gelfentry['_record'].to_s.strip.empty? then
79
- gelfentry['short_message'] = gelfentry.delete('_record')
80
- else
81
- # we must have a short_message, so provide placeholder
82
- gelfentry['short_message'] = '(no message)'
83
- end
60
+ def process_record_entry(k, v, conf, gelfentry)
61
+ case k
62
+ when 'host', 'hostname'
63
+ gelfentry['host'] = conf[:use_record_host] ? v : (gelfentry['_host'] = v)
64
+ when 'timestamp', 'time'
65
+ gelfentry['timestamp'] = parse_timestamp(v)
66
+ when 'level'
67
+ gelfentry['level'] = LEVEL_MAP[v.to_s.downcase[0]] || GELF::UNKNOWN
68
+ when 'msec'
69
+ gelfentry['timestamp'] = conf[:add_msec_time] ? "#{time.to_s}.#{v}".to_f : (gelfentry['_msec'] = v)
70
+ when 'short_message', 'version', 'full_message', 'facility', 'file', 'line'
71
+ gelfentry[k] = v
72
+ else
73
+ gelfentry[k.start_with?('_') ? k : "_#{k}"] = v
84
74
  end
75
+ end
76
+
77
+ def parse_timestamp(v)
78
+ return v if v.is_a?(Integer) || v.is_a?(Float)
79
+
80
+ DateTime.parse(v).strftime("%Q").to_f / 1_000 rescue v
81
+ end
85
82
 
86
- # I realize the nulls are will be treated as unset keys, but it does
87
- # tend to make for larger files and data transmissions.
88
- return gelfentry.delete_if{ |k,v| v.nil? }
83
+ def ensure_short_message(gelfentry)
84
+ default_key = ['_message', '_msg', '_log', '_record'].find { |key| gelfentry[key]&.strip&.empty? == false }
85
+ gelfentry['short_message'] = default_key ? gelfentry.delete(default_key) : '(no message)'
89
86
  end
90
87
  end
91
88
  end
@@ -1,3 +1,3 @@
1
1
  module FluentPluginGelfBest
2
- VERSION = "1.1.0"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-gelf-best
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Yamauchi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-04-17 00:00:00.000000000 Z
13
+ date: 2024-04-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: fluentd