fluent-plugin-gelf-best 1.2.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: d561e8e54966fefedb1bfcc2ba6a629d334d3514ed378ea3fe982903b9e6b098
4
- data.tar.gz: fc6f78f7a69a1b01d7046ea68c1e2334b9a8149a2c2436b938348303e318ac61
3
+ metadata.gz: b3d0be1e13c195cc69d517d9678fd9ed15128700a203f6fcded95a8acf26ffa5
4
+ data.tar.gz: b5f48294ede888f480c827fcfeb635ae0769631c8e77b0329773927ca64a0aef
5
5
  SHA512:
6
- metadata.gz: 5dd171597df6672a52862682cbc7735998663cf29224f4c0059baae043a09f70d01ad9420875b0131779e817941a9506afae19d03ec8b91cc4591021b36656e9
7
- data.tar.gz: e63ccdd497c1fbf8bab67ed982b9e86e4f24eab987956151671c348ed19848de276603115aaf7444ce9b3ef88b4812dc0d20e84660325b266a6f821e84f63389
6
+ metadata.gz: 1d0a6549e8a686fca58906ca27fd153e7e6d84cac711c59a7e0312f039ad591815567d08d55724d1a88c1d7ef3ae3acef214a652fae50aac09a3fc6145aae24d
7
+ data.tar.gz: 60bebae56b82025efa2f141be0842d1fe966d47d0d633fbe2e18bce7dbb7718635425150ae4bce2c69713b6a6b814efd6a63af508599ea16b293c2086d74c7dc
@@ -1,24 +1,39 @@
1
+ require 'oj'
2
+ require 'date'
3
+ require 'gelf'
4
+
1
5
  module Fluent
2
6
  module GelfPluginUtil
3
- require 'gelf'
4
7
 
5
8
  LEVEL_MAP = {
6
- "0" => GELF::UNKNOWN, "1" => GELF::UNKNOWN, "a" => GELF::UNKNOWN,
7
- "2" => GELF::FATAL, "c" => GELF::FATAL,
8
- "3" => GELF::ERROR,
9
- "4" => GELF::WARN, "w" => GELF::WARN,
10
- "5" => GELF::INFO, "n" => GELF::INFO,
11
- "6" => GELF::INFO, "i" => GELF::INFO,
12
- "7" => GELF::DEBUG, "d" => GELF::DEBUG,
13
- "e" => GELF::ERROR # assuming 'e' stands typically for 'error'
14
- }
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
18
+
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
15
29
 
16
30
  def make_gelfentry(tag, time, record, conf = {})
17
- gelfentry = {"_fluentd_tag" => tag}
18
- gelfentry["timestamp"] = calculate_timestamp(time)
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)}
19
34
 
20
- record.each_pair do |k, v|
21
- gelfentry.merge!(process_record_entry(k, v, conf, gelfentry))
35
+ record.each do |k, v|
36
+ process_record_entry(k, v, conf, gelfentry)
22
37
  end
23
38
 
24
39
  ensure_short_message(gelfentry)
@@ -27,6 +42,13 @@ module Fluent
27
42
 
28
43
  private
29
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
+
30
52
  def calculate_timestamp(time)
31
53
  if defined?(Fluent::EventTime) && time.is_a?(Fluent::EventTime)
32
54
  time.sec + (time.nsec.to_f / 1_000_000_000).round(3)
@@ -38,43 +60,29 @@ module Fluent
38
60
  def process_record_entry(k, v, conf, gelfentry)
39
61
  case k
40
62
  when 'host', 'hostname'
41
- return {'host' => (conf[:use_record_host] ? v : gelfentry['_host'] = v)}
63
+ gelfentry['host'] = conf[:use_record_host] ? v : (gelfentry['_host'] = v)
42
64
  when 'timestamp', 'time'
43
- { 'timestamp' => parse_timestamp(v) }
65
+ gelfentry['timestamp'] = parse_timestamp(v)
44
66
  when 'level'
45
- {'level' => LEVEL_MAP[v.to_s.downcase[0]] || (v.to_s.length >= 2 && v.to_s.downcase[1] != "r" ? GELF::UNKNOWN : v)}
67
+ gelfentry['level'] = LEVEL_MAP[v.to_s.downcase[0]] || GELF::UNKNOWN
46
68
  when 'msec'
47
- conf[:add_msec_time] ? {'timestamp' => "#{time.to_s}.#{v}".to_f} : {'_msec' => v}
69
+ gelfentry['timestamp'] = conf[:add_msec_time] ? "#{time.to_s}.#{v}".to_f : (gelfentry['_msec'] = v)
48
70
  when 'short_message', 'version', 'full_message', 'facility', 'file', 'line'
49
- {k => v}
71
+ gelfentry[k] = v
50
72
  else
51
- {k.start_with?('_') ? k : "_#{k}" => v}
73
+ gelfentry[k.start_with?('_') ? k : "_#{k}"] = v
52
74
  end
53
75
  end
54
76
 
55
77
  def parse_timestamp(v)
56
- if v.is_a?(Integer) || v.is_a?(Float)
57
- v
58
- else
59
- begin
60
- (DateTime.parse(v).strftime("%Q").to_f / 1_000).round(3)
61
- rescue ArgumentError
62
- v
63
- end
64
- end
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
65
81
  end
66
82
 
67
83
  def ensure_short_message(gelfentry)
68
- return if gelfentry['short_message'] && !gelfentry['short_message'].to_s.strip.empty?
69
-
70
- ['_message', '_msg', '_log', '_record'].each do |key|
71
- if gelfentry[key] && !gelfentry[key].to_s.strip.empty?
72
- gelfentry['short_message'] = gelfentry.delete(key)
73
- return
74
- end
75
- end
76
-
77
- gelfentry['short_message'] = '(no message)' unless gelfentry['short_message']
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)'
78
86
  end
79
87
  end
80
88
  end
@@ -1,3 +1,3 @@
1
1
  module FluentPluginGelfBest
2
- VERSION = "1.2.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.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Yamauchi