fluent-plugin-filter-kv-parser 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/fluent-plugin-filter-kv-parser.gemspec +1 -1
- data/lib/fluent/plugin/filter_key_value_parser.rb +26 -9
- data/test/test_filter_key_value_parser.rb +38 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06f855fd104485ee6469bda9fd116eb9a5a0e04d186297b246d21ee37f82f931
|
4
|
+
data.tar.gz: 14f5e141dff49e437dcb3d9dd047869057d1a9c2ca5de02b695fbfb06d60b107
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a93b2f140f1f0914dbefa08d19d7f9f50033ff4c09e6fe20ca61c01281b27aae32e28051fd87f56723989e3b5ed03b5df3de976a3163deb4355959d121b4e12e
|
7
|
+
data.tar.gz: c7adfcf6a29ff9016495800e3e95c9cb2b23baa9233ce9a621adf63960849e83df20235753e1214f4e813d085af0bf5afd7a958032cd8ba3191ed56157a5bbe6
|
data/README.md
CHANGED
@@ -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.
|
6
|
+
s.version = "0.0.4"
|
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"
|
@@ -11,17 +11,29 @@ module Fluent
|
|
11
11
|
config_param :remove_prefix, :string, default: ''
|
12
12
|
config_param :keys_delimiter, :string, default: '/\s+/'
|
13
13
|
config_param :kv_delimiter_chart, :string, default: '='
|
14
|
+
config_param :filtered_keys, :string, default: nil
|
15
|
+
config_param :filtered_keys_regex, :string, default: nil
|
16
|
+
config_param :filtered_keys_delimiter, :string, default: ','
|
14
17
|
|
15
18
|
|
16
19
|
def configure(conf)
|
17
20
|
super
|
18
|
-
|
21
|
+
|
22
|
+
regex = /^\/.+\/$/
|
23
|
+
|
24
|
+
if regex.match(@keys_delimiter.to_s)
|
19
25
|
@keys_delimiter = Regexp.new(@keys_delimiter[1..-2])
|
20
26
|
end
|
21
27
|
|
22
|
-
if @remove_prefix
|
28
|
+
if regex.match(@remove_prefix.to_s)
|
23
29
|
@remove_prefix = Regexp.new(@remove_prefix[1..-2])
|
24
30
|
end
|
31
|
+
|
32
|
+
if regex.match(@filtered_keys_regex.to_s)
|
33
|
+
@filtered_keys_regex = Regexp.new(@filtered_keys_regex[1..-2])
|
34
|
+
end
|
35
|
+
|
36
|
+
@filtered_keys_list = parse_filtered_keys_parameter
|
25
37
|
end
|
26
38
|
|
27
39
|
def filter(tag, time, record)
|
@@ -29,19 +41,20 @@ module Fluent
|
|
29
41
|
|
30
42
|
log_line = extract_log_line record[@key]
|
31
43
|
|
32
|
-
|
33
|
-
record.merge! regex_filter(log_line)
|
34
|
-
else
|
35
|
-
record.merge! delimiter_filter(log_line)
|
36
|
-
end
|
37
|
-
|
44
|
+
record.merge! extracted_keys(log_line)
|
38
45
|
record.tap { |r| r.delete(@key) if @remove_key }.compact
|
39
46
|
end
|
40
47
|
|
41
48
|
private
|
42
49
|
|
43
50
|
def regex_filter(line)
|
44
|
-
"#{line} ".scan(/(?<key>[
|
51
|
+
"#{line} ".scan(/(?<key>[a-zA-Z_0-9]+)=(?<value>[^=]+)\s/).to_h
|
52
|
+
end
|
53
|
+
|
54
|
+
def extracted_keys(line)
|
55
|
+
keys = @use_regex ? regex_filter(line) : delimiter_filter(line)
|
56
|
+
filtered_keys = @filtered_keys_list.empty? ? keys : keys.slice(*@filtered_keys_list)
|
57
|
+
@filtered_keys_regex.nil? ? filtered_keys : filtered_keys.merge(keys.select{ |k,v| @filtered_keys_regex.match(k.to_s)})
|
45
58
|
end
|
46
59
|
|
47
60
|
def delimiter_filter(line)
|
@@ -53,6 +66,10 @@ module Fluent
|
|
53
66
|
items
|
54
67
|
end
|
55
68
|
|
69
|
+
def parse_filtered_keys_parameter
|
70
|
+
@filtered_keys.to_s.split(@filtered_keys_delimiter)
|
71
|
+
end
|
72
|
+
|
56
73
|
def extract_log_line(line)
|
57
74
|
line.gsub(@remove_prefix,'').strip
|
58
75
|
end
|
@@ -146,9 +146,46 @@ class KeyValueFilterTest < Test::Unit::TestCase
|
|
146
146
|
'log' => "Start Request key=10 skey='this is a miltispace line' akey=20 zkey=30 dkey=4",
|
147
147
|
}
|
148
148
|
filtered = filter(d, [msg]).first[2]
|
149
|
-
puts filtered.inspect
|
150
149
|
assert_equal 6, filtered.count
|
151
150
|
assert_equal "'this is a miltispace line'", filtered['skey']
|
152
151
|
assert_equal false, filtered.key?("log")
|
153
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
|
154
191
|
end
|