fluent-plugin-gelf-best 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16f39189270e1bd23e196155e2a7bbead5ceb5620f559404198bc212b30eebef
4
- data.tar.gz: 9fd452c44a244e8bdea46b8db8c498643b8bc47212c0bd339aee7cf111bc7afe
3
+ metadata.gz: d561e8e54966fefedb1bfcc2ba6a629d334d3514ed378ea3fe982903b9e6b098
4
+ data.tar.gz: fc6f78f7a69a1b01d7046ea68c1e2334b9a8149a2c2436b938348303e318ac61
5
5
  SHA512:
6
- metadata.gz: cdcb16ce91a9946ea5e4dfd5977a103c5eb1e81e571376a2562add5312f302532f0939c389a039cb8ef01c4ece2c198eec73b41140fa88eda3b16e9470ea0d46
7
- data.tar.gz: eb382d4a3682754643eefbf1cb16853b5f47baa1d8ffe347e628e4a1ba32116cc25aa5f42296ccf10ad64ce193191ecac45bd493435269d97fa13b78176e94ca
6
+ metadata.gz: 5dd171597df6672a52862682cbc7735998663cf29224f4c0059baae043a09f70d01ad9420875b0131779e817941a9506afae19d03ec8b91cc4591021b36656e9
7
+ data.tar.gz: e63ccdd497c1fbf8bab67ed982b9e86e4f24eab987956151671c348ed19848de276603115aaf7444ce9b3ef88b4812dc0d20e84660325b266a6f821e84f63389
@@ -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,80 @@
1
- # encoding=utf-8
2
1
  module Fluent
3
2
  module GelfPluginUtil
4
-
5
3
  require 'gelf'
6
4
 
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)
5
+ 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
+ }
15
+
16
+ def make_gelfentry(tag, time, record, conf = {})
17
+ gelfentry = {"_fluentd_tag" => tag}
18
+ gelfentry["timestamp"] = calculate_timestamp(time)
19
+
20
+ record.each_pair do |k, v|
21
+ gelfentry.merge!(process_record_entry(k, v, conf, gelfentry))
22
+ end
23
+
24
+ ensure_short_message(gelfentry)
25
+ gelfentry.compact
26
+ end
27
+
28
+ private
29
+
30
+ def calculate_timestamp(time)
31
+ if defined?(Fluent::EventTime) && time.is_a?(Fluent::EventTime)
32
+ time.sec + (time.nsec.to_f / 1_000_000_000).round(3)
11
33
  else
12
- gelfentry['timestamp'] = time
34
+ time
13
35
  end
36
+ end
37
+
38
+ def process_record_entry(k, v, conf, gelfentry)
39
+ case k
40
+ when 'host', 'hostname'
41
+ return {'host' => (conf[:use_record_host] ? v : gelfentry['_host'] = v)}
42
+ when 'timestamp', 'time'
43
+ { 'timestamp' => parse_timestamp(v) }
44
+ 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)}
46
+ when 'msec'
47
+ conf[:add_msec_time] ? {'timestamp' => "#{time.to_s}.#{v}".to_f} : {'_msec' => v}
48
+ when 'short_message', 'version', 'full_message', 'facility', 'file', 'line'
49
+ {k => v}
50
+ else
51
+ {k.start_with?('_') ? k : "_#{k}" => v}
52
+ end
53
+ end
14
54
 
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
55
+ 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
67
63
  end
68
64
  end
65
+ end
66
+
67
+ def ensure_short_message(gelfentry)
68
+ return if gelfentry['short_message'] && !gelfentry['short_message'].to_s.strip.empty?
69
69
 
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)'
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
83
74
  end
84
75
  end
85
76
 
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? }
77
+ gelfentry['short_message'] = '(no message)' unless gelfentry['short_message']
89
78
  end
90
79
  end
91
80
  end
@@ -1,3 +1,3 @@
1
1
  module FluentPluginGelfBest
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.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.2.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