fluent-plugin-filter-kv-parser 0.0.1 → 0.0.6

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: 7ed4b252f8b033a1adf97ff84262c182e16c2e280d0e8a647fca1c93b28851d1
4
- data.tar.gz: 9b4801ce9b57c7b2a8705c8f85b963616d2e004f6653ed861700fa3e2e4b4cfe
3
+ metadata.gz: 8932148ec7a3bfaa0a6241823bda594b85d5a91b24dca0156fb2e7497e4f5eef
4
+ data.tar.gz: 810626b5c05a84a382719b9bb40393ee3f901b7d3d9b8366114e1b8633e08621
5
5
  SHA512:
6
- metadata.gz: f81e9efa55ee3c61d780633e8108e0b3d6f11f83cfc13c1e20a45d4002fa5431e77eaa22df2e381b44b410aa61e1ba93e2284f1bfc017a5e2870a1d460c4d60d
7
- data.tar.gz: b949db4abb5d6a62c7abaed2eeb825b100a1a49d27f5f8c0dfdb821ccc9b0da170833ee63a72cefe82331c09f5de396dfc69fe3798b7a31c8bb961c9035b5ac8
6
+ metadata.gz: 89089861029085470ada61bb7d34bee99d33c46b3d089b56fc840c4f2881a479fab0a31a0f6ea663171d1c3bb5e7eface8cd5e92cd0c57b4769179e55c6ba610
7
+ data.tar.gz: 8bba97ee414b53f06222af5f6f8bad5ef89e4e2538896a8f9e0a3b5c239ef594f796a7da033843cac4e62f1207eb38591a42827147757c6adfc38f9fdb8cf3b4
data/README.md CHANGED
@@ -20,9 +20,14 @@ gem install fluent-plugin-filter-kv-parser
20
20
  <filter **>
21
21
  type key_value_parser
22
22
  key log
23
+ use_regex true
24
+ remove_key false
23
25
  remove_prefix /^prefix/
24
26
  keys_delimiter /\s+/
25
27
  kv_delimiter_chart '='
28
+ filtered_keys akey,bkey,ckey
29
+ filtered_keys_regex /^sub_[a-zA-Z_0-9]+/
30
+ filter_out_lines_without_keys false
26
31
  </filter>
27
32
 
28
33
  <match **>
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fluent-plugin-filter-kv-parser"
6
- s.version = "0.0.1"
6
+ s.version = "0.0.6"
7
7
  s.authors = ["Al-waleed Shihadeh"]
8
8
  s.email = ["wshihadeh.dev@gmail.com"]
9
9
  s.homepage = "https://github.com/wshihadeh/fluent-plugin-filter-kv-parser.git"
@@ -7,34 +7,72 @@ module Fluent
7
7
 
8
8
  config_param :key, :string, default: 'log'
9
9
  config_param :remove_key, :bool, default: false
10
+ config_param :filter_out_lines_without_keys, :bool, default: false
11
+ config_param :use_regex, :bool, default: false
10
12
  config_param :remove_prefix, :string, default: ''
11
13
  config_param :keys_delimiter, :string, default: '/\s+/'
12
14
  config_param :kv_delimiter_chart, :string, default: '='
15
+ config_param :filtered_keys, :string, default: nil
16
+ config_param :filtered_keys_regex, :string, default: nil
17
+ config_param :filtered_keys_delimiter, :string, default: ','
13
18
 
14
19
 
15
20
  def configure(conf)
16
21
  super
17
- if @keys_delimiter[0] == '/' and @keys_delimiter[-1] == '/'
22
+
23
+ regex = /^\/.+\/$/
24
+
25
+ if regex.match(@keys_delimiter.to_s)
18
26
  @keys_delimiter = Regexp.new(@keys_delimiter[1..-2])
19
27
  end
20
28
 
21
- if @remove_prefix[0] == '/' and @remove_prefix[-1] == '/'
29
+ if regex.match(@remove_prefix.to_s)
22
30
  @remove_prefix = Regexp.new(@remove_prefix[1..-2])
23
31
  end
32
+
33
+ if regex.match(@filtered_keys_regex.to_s)
34
+ @filtered_keys_regex = Regexp.new(@filtered_keys_regex[1..-2])
35
+ end
36
+
37
+ @filtered_keys_list = parse_filtered_keys_parameter
24
38
  end
25
39
 
26
40
  def filter(tag, time, record)
27
- log_line = extract_log_line record[@key]
28
- log_line.split(@keys_delimiter).each do |kv|
29
- key, value = kv.split(@kv_delimiter_chart, 2)
30
- record[key] = value
31
- end
41
+ return if record[@key].nil?
42
+
43
+ keys = extracted_keys(extract_log_line(record[@key]))
32
44
 
33
- record.tap { |r| r.delete(@key) if @remove_key }
45
+ return if @filter_out_lines_without_keys && keys.empty?
46
+
47
+ record.merge! keys
48
+ record.tap { |r| r.delete(@key) if @remove_key }.compact
34
49
  end
35
50
 
36
51
  private
37
52
 
53
+ def regex_filter(line)
54
+ "#{line} ".scan(/(?<key>[a-zA-Z_0-9]+)=(?<value>([^=]+|[^ ]+))\s/).to_h
55
+ end
56
+
57
+ def extracted_keys(line)
58
+ keys = @use_regex ? regex_filter(line) : delimiter_filter(line)
59
+ filtered_keys = @filtered_keys_list.empty? ? keys : keys.slice(*@filtered_keys_list)
60
+ @filtered_keys_regex.nil? ? filtered_keys : filtered_keys.merge(keys.select{ |k,v| @filtered_keys_regex.match(k.to_s)})
61
+ end
62
+
63
+ def delimiter_filter(line)
64
+ items = {}
65
+ line.split(@keys_delimiter).each do |kv|
66
+ key, value = kv.split(@kv_delimiter_chart, 2)
67
+ items[key] = value if value
68
+ end
69
+ items
70
+ end
71
+
72
+ def parse_filtered_keys_parameter
73
+ @filtered_keys.to_s.split(@filtered_keys_delimiter)
74
+ end
75
+
38
76
  def extract_log_line(line)
39
77
  line.gsub(@remove_prefix,'').strip
40
78
  end
@@ -83,4 +83,133 @@ class KeyValueFilterTest < Test::Unit::TestCase
83
83
  assert_equal 5, filtered.count
84
84
  assert_equal false, filtered.key?("log")
85
85
  end
86
+
87
+ test 'test_empty_line' do
88
+ d = create_driver(%[
89
+ key log
90
+ remove_key true
91
+ remove_prefix /^[^ ]+\s[^ ]+/
92
+ ])
93
+ msg = {
94
+ 'time' => '2013-02-12 22:01:15 UTC',
95
+ 'log' => 'Start Request',
96
+ }
97
+ filtered = filter(d, [msg]).first[2]
98
+ assert_equal 1, filtered.count
99
+ assert_equal false, filtered.key?("log")
100
+ end
101
+
102
+ test 'test_two_log_key' do
103
+ d = create_driver(%[
104
+ key log
105
+ remove_key true
106
+ remove_prefix /^[^ ]+\s[^ ]+/
107
+ ])
108
+ msg = {
109
+ 'time' => '2013-02-12 22:01:15 UTC',
110
+ 'log' => 'Start Request key=10 akey=20 zkey=30 dkey=40',
111
+ }
112
+ msg2 = {
113
+ 'time' => '2013-02-12 22:01:15 UTC',
114
+ 'log' => 'Start Request key=10 akey=20 zkey=30 dkey=40',
115
+ }
116
+ filtered = filter(d, [msg, msg2])
117
+ assert_equal 2, filtered.count
118
+ end
119
+
120
+ test 'test_no_log_key' do
121
+ d = create_driver(%[
122
+ key log
123
+ remove_key true
124
+ remove_prefix /^[^ ]+\s[^ ]+/
125
+ ])
126
+ msg = {
127
+ 'time' => '2013-02-12 22:01:15 UTC',
128
+ }
129
+ msg2 = {
130
+ 'time' => '2013-02-12 22:01:15 UTC',
131
+ 'log' => 'Start Request key=10 akey=20 zkey=30 dkey=40',
132
+ }
133
+ filtered = filter(d, [msg, msg2])
134
+ assert_equal 1, filtered.count
135
+ end
136
+
137
+ test 'test_with_space_and_regex' do
138
+ d = create_driver(%[
139
+ key log
140
+ remove_key true
141
+ remove_prefix /^[^ ]+\s[^ ]+/
142
+ use_regex true
143
+ ])
144
+ msg = {
145
+ 'time' => '2013-02-12 22:01:15 UTC',
146
+ 'log' => "Start Request key=10 skey='this is a miltispace line' akey=20 zkey=30 dkey=4",
147
+ }
148
+ filtered = filter(d, [msg]).first[2]
149
+ assert_equal 6, filtered.count
150
+ assert_equal "'this is a miltispace line'", filtered['skey']
151
+ assert_equal false, filtered.key?("log")
152
+ end
153
+
154
+ test 'test_filter_keys' do
155
+ d = create_driver(%[
156
+ key log
157
+ remove_key true
158
+ remove_prefix /^[^ ]+\s[^ ]+/
159
+ use_regex true
160
+ filtered_keys key,gkeyn,nkey,skey,akey,zkey
161
+ ])
162
+ msg = {
163
+ 'time' => '2013-02-12 22:01:15 UTC',
164
+ 'log' => "Start Request key=10 gkey=100 nkey=108 skey='this is a miltispace line' akey=20 zkey=30 dkey=4",
165
+ }
166
+ filtered = filter(d, [msg]).first[2]
167
+ assert_equal 6, filtered.count
168
+ assert_equal "'this is a miltispace line'", filtered['skey']
169
+ assert_equal false, filtered.key?("log")
170
+ end
171
+
172
+ test 'test_filter_keys_with_regex' do
173
+ d = create_driver(%[
174
+ key log
175
+ remove_key true
176
+ remove_prefix /^[^ ]+\s[^ ]+/
177
+ use_regex true
178
+ filtered_keys key,gkeyn,nkey,skey,akey,zkey
179
+ filtered_keys_regex /^sub_[a-zA-Z_0-9]+/
180
+ ])
181
+ msg = {
182
+ 'time' => '2013-02-12 22:01:15 UTC',
183
+ 'log' => "Start Request sub_key=0 sub_akey=11 sub_zkey=12 key=10 gkey=100 nkey=108 skey='this is a miltispace line' akey=20 zkey=30 dkey=4",
184
+ }
185
+ filtered = filter(d, [msg]).first[2]
186
+ assert_equal 9, filtered.count
187
+ assert_equal "'this is a miltispace line'", filtered['skey']
188
+ assert_equal "0", filtered['sub_key']
189
+ assert_equal false, filtered.key?("log")
190
+ end
191
+
192
+ test 'test_filter_out_lines_without_keys' do
193
+ d = create_driver(%[
194
+ key log
195
+ remove_key true
196
+ use_regex true
197
+ filtered_keys_regex /^sub_[a-zA-Z_0-9]+/
198
+ filter_out_lines_without_keys true
199
+ ])
200
+ msg = {
201
+ 'time' => '2013-02-12 22:01:15 UTC',
202
+ 'log' => "akey=10 bkey=11 ckey=11",
203
+ }
204
+
205
+ msg2 = {
206
+ 'time' => '2013-02-12 22:01:15 UTC',
207
+ 'log' => "Start Request to test lines without any keys",
208
+ }
209
+ filtered = filter(d, [msg, msg2])
210
+
211
+ assert_equal 1, filtered.count
212
+ assert_equal 4, filtered.first[2].count
213
+ assert_equal "10", filtered.first[2]['akey']
214
+ end
86
215
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-filter-kv-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Al-waleed Shihadeh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-05 00:00:00.000000000 Z
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd