fluent-plugin-multi-format-parser 0.1.1 → 1.1.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
- SHA1:
3
- metadata.gz: b3ccd298b01b05ed96a4494b153bd4ee4abd2b8d
4
- data.tar.gz: 5b8399121244fd4f5939a7acb8c2481c6450d1ce
2
+ SHA256:
3
+ metadata.gz: 6c766b375521ddc5cc16503d3851d73ce3e7c995cd5f78dc3667dbe62966ff0b
4
+ data.tar.gz: 18f41d24351b487385d4433835cf0d8f81a648c86e3b99094e0127d08394a209
5
5
  SHA512:
6
- metadata.gz: fefc45c5044df050487e7189d0071ee6021c29cac9ba4383e91273a08bdbf9cea851cd29ce160eac99df3c9994a775e48eb62f0c1db63bbc157aadccd2a14f3d
7
- data.tar.gz: 8a69a5e16b154f21961b7227ee6e55a8ee7945825a966cc2e2badf5cde8ba9b921d5f671485947795979449c095d05d50089a4df401b826ec25aa1288b20c9d9
6
+ metadata.gz: cfbc21ac9ec34e10cbde396e3d34b4358121cc2430db5fd91f1b60576499531476862f23ed6858373841405cf742ee04bcd800d8218b5a85f9a0c77724361bd8
7
+ data.tar.gz: 14f2c6ecc7b3aed398f040d636797147ccd1c096e0573f8c3392d857972376905be0b1e46e67600d35e4c787f53c65ca7395495ce55da53fd6b2c0b7bb66fd71
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_PATH: "vendor/bundle"
data/README.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  Parse format mixed logs.
4
4
 
5
+ ## Requirements
6
+
7
+ | fluent-plugin-multi-format-parser | fluentd | ruby |
8
+ |-------------------|---------|------|
9
+ | >= 1.0.0 | >= v0.14.0 | >= 2.1 |
10
+ | < 1.0.0 | >= v0.12.0 | >= 1.9 |
11
+
5
12
  ## Installation
6
13
 
7
14
  Use RubyGems:
@@ -10,46 +17,108 @@ Use RubyGems:
10
17
 
11
18
  ## Configuration
12
19
 
13
- This plugin is a parser plugin. After installed, you can use `multi_format` in `format` supported plugins.
14
- Use multiple `<pattern>` to specify multiple format.
20
+ This plugin is a parser plugin. After installed, you can use `multi_format` in `<parse>` supported plugins.
21
+ Use multiple `<pattern>`s to specify multiple parser formats.
15
22
 
16
23
  <source>
17
24
  @type udp
18
25
  tag logs.multi
19
26
 
27
+ <parse>
28
+ @type multi_format
29
+ <pattern>
30
+ format apache
31
+ </pattern>
32
+ <pattern>
33
+ format json
34
+ time_key timestamp
35
+ </pattern>
36
+ <pattern>
37
+ format none
38
+ </pattern>
39
+ </parse>
40
+ </source>
41
+
42
+ `multi_format` tries pattern matching from top to bottom and returns parsed result when matched.
43
+
44
+ Available format patterns and parameters are depends on Fluentd parsers.
45
+ See [parser plugin document](http://docs.fluentd.org/v1.0/articles/parser-plugin-overview) for more details.
46
+
47
+ ### For v1.0
48
+
49
+ Put `<pattern>`s inside `<parse>`.
50
+
51
+ <filter app.**>
52
+ @type parser
53
+ key_name message
54
+ <parse>
55
+ @type multi_format
56
+ <pattern>
57
+ format json
58
+ </pattern>
59
+ <pattern>
60
+ format regexp
61
+ expression /...your regexp pattern.../
62
+ </pattern>
63
+ <pattern>
64
+ format none
65
+ </pattern>
66
+ </parse>
67
+ </filter>
68
+
69
+ ### For v0.12
70
+
71
+ Use `format` instead of `<parse></parse>`.
72
+
73
+ <filter app.**>
74
+ @type parser
75
+ key_name message
76
+
20
77
  format multi_format
21
78
  <pattern>
22
- format apache
79
+ format json
23
80
  </pattern>
24
81
  <pattern>
25
- format json
26
- time_key timestamp
82
+ format /...your regexp pattern.../
27
83
  </pattern>
28
84
  <pattern>
29
85
  format none
30
86
  </pattern>
31
- </match>
87
+ </filter>
32
88
 
33
- `multi_format` tries pattern matching from top to bottom and returns parsed result when matched.
89
+ ### Adding format identity field
34
90
 
35
- Available format patterns and parameters are depends on Fluentd parsers.
36
- See [in_tail format document](http://docs.fluentd.org/articles/in_tail) for more details.
91
+ Sometimes it may be useful to know which pattern was used. Since pareser usage
92
+ may not support retagging, there is an option to add a format name field and/or
93
+ index field.
37
94
 
38
- ### For v0.14
95
+ Example:
39
96
 
40
- This plugin handles `pattern` section manually, so v0.14's automatic parameter conversion doesn't work well.
41
- If you want to use this plugin with v0.14, you need to use v0.14 parser syntax like below
42
97
 
43
98
  <filter app.**>
44
99
  @type parser
45
100
  key_name message
46
- <parse> # Use <parse> section for parser parameters
101
+ <parse>
47
102
  @type multi_format
103
+
104
+ # if set, add this key to record with value being pattern format name
105
+ # (format_name key)
106
+ format_key 'format'
107
+
48
108
  <pattern>
49
109
  format json
110
+ # set format name for this pattern. If unset, uses format name
111
+ # followed by index (in this case would be 'json#0')
112
+ format_name 'json'
113
+ </pattern>
114
+ <pattern>
115
+ format regexp
116
+ format_name 'MyRefex'
117
+ expression /...your regexp pattern.../
50
118
  </pattern>
51
119
  <pattern>
52
120
  format none
121
+ format_name 'unparsed'
53
122
  </pattern>
54
123
  </parse>
55
124
  </filter>
@@ -68,6 +137,6 @@ This plugin doesn't work with `multiline` parsers because parser itself doesn't
68
137
  <td>Copyright</td><td>Copyright (c) 2014- Masahiro Nakagawa</td>
69
138
  </tr>
70
139
  <tr>
71
- <td>License</td><td>MIT License</td>
140
+ <td>License</td><td>Apache License (2.0)</td>
72
141
  </tr>
73
142
  </table>
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  require 'bundler'
3
- Bundler::GemHelper.install_tasks
3
+ require 'bundler/gem_tasks'
4
4
 
5
5
  require 'rake/testtask'
6
6
 
@@ -11,4 +11,3 @@ Rake::TestTask.new(:test) do |test|
11
11
  end
12
12
 
13
13
  task :default => [:build]
14
-
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 1.1.0
@@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
9
9
  gem.version = File.read("VERSION").strip
10
10
  gem.authors = ["Masahiro Nakagawa"]
11
11
  gem.email = "repeatedly@gmail.com"
12
- gem.has_rdoc = false
12
+ #gem.has_rdoc = false
13
13
  #gem.platform = Gem::Platform::RUBY
14
14
  gem.license = 'Apache License (2.0)'
15
15
  gem.files = `git ls-files`.split("\n")
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
17
17
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  gem.require_paths = ['lib']
19
19
 
20
- gem.add_dependency "fluentd", [">= 0.10.46", "< 2"]
20
+ gem.add_dependency "fluentd", [">= 0.14.0", "< 2"]
21
21
  gem.add_development_dependency "rake", ">= 0.9.2"
22
+ gem.add_development_dependency("test-unit", ["~> 3.3"])
22
23
  end
@@ -1,98 +1,49 @@
1
- require 'fluent/parser'
1
+ require 'fluent/plugin/parser'
2
2
 
3
3
  module Fluent
4
- class TextParser
5
- if defined?(::Fluent::Parser)
6
- class MultiFormatParser < Parser
7
- Plugin.register_parser('multi_format', self)
4
+ module Plugin
5
+ class MultiFormatParser < Parser
6
+ Plugin.register_parser('multi_format', self)
7
+ config_param :format_key, :string, default: nil
8
8
 
9
- def initialize
10
- super
9
+ def initialize
10
+ super
11
11
 
12
- @parsers = []
13
- end
14
-
15
- def configure(conf)
16
- super
17
-
18
- conf.elements.each { |e|
19
- next unless ['pattern', 'format'].include?(e.name)
20
- next if e['format'].nil? && (e['@type'] == 'multi_format')
21
-
22
- parser = Plugin.new_parser(e['format'])
23
- parser.configure(e)
24
- @parsers << parser
25
- }
26
- end
27
-
28
- def parse(text)
29
- @parsers.each { |parser|
30
- begin
31
- parser.parse(text) { |time, record|
32
- if time && record
33
- yield time, record
34
- return
35
- end
36
- }
37
- rescue # ignore parser error
38
- end
39
- }
40
-
41
- yield nil, nil
42
- end
12
+ @parsers = []
13
+ @parser_format_names = []
43
14
  end
44
- else # support old API. Will be removed.
45
- class MultiFormatParser
46
- include Configurable
47
-
48
- def initialize
49
- super
50
15
 
51
- @parsers = []
52
- end
53
-
54
- def configure(conf)
55
- super
56
-
57
- conf.elements.each { |e|
58
- next unless ['pattern', 'format'].include?(e.name)
59
-
60
- parser = TextParser.new
61
- parser.configure(e)
62
- @parsers << parser.parser
63
- }
64
- end
16
+ def configure(conf)
17
+ super
18
+
19
+ conf.elements.each_with_index { |e, i|
20
+ next unless ['pattern', 'format'].include?(e.name)
21
+ next if e['format'].nil? && (e['@type'] == 'multi_format')
22
+ @parser_format_names << e.delete('format_name') || ""+e['format']+"#"+i.to_s
23
+ parser = Plugin.new_parser(e['format'])
24
+ parser.configure(e)
25
+ @parsers << parser
26
+ }
27
+ end
65
28
 
66
- def parse(text)
67
- @parsers.each { |parser|
68
- begin
69
- parser.call(text) { |time, record|
70
- if time && record
71
- if block_given?
72
- yield time, record
73
- return
74
- else
75
- return time, record
76
- end
29
+ def parse(text)
30
+ @parsers.each_with_index { |parser, i|
31
+ begin
32
+ parser.parse(text) { |time, record|
33
+ if time && record
34
+ if @format_key
35
+ record[@format_key] = @parser_format_names[i]
77
36
  end
78
- }
79
- rescue # ignore parser error
80
- end
81
- }
82
-
83
- if block_given?
84
- yield nil, nil
85
- else
86
- return nil, nil
37
+ yield time, record
38
+ return
39
+ end
40
+ }
41
+ rescue # ignore parser error
87
42
  end
88
- end
43
+ }
89
44
 
90
- def call(*a, &b)
91
- parse(*a, &b)
92
- end
45
+ yield nil, nil
93
46
  end
94
-
95
- register_template('multi_format', Proc.new { MultiFormatParser.new })
96
47
  end
97
48
  end
98
49
  end
@@ -0,0 +1,61 @@
1
+ require 'fluent/config'
2
+ require 'fluent/test'
3
+ require 'fluent/test/driver/parser'
4
+ require 'fluent/plugin/parser'
5
+ require 'fluent/plugin/parser_multi_format'
6
+ require 'test/unit'
7
+
8
+ class MultiFormatParserTest < ::Test::Unit::TestCase
9
+ def setup
10
+ Fluent::Test.setup
11
+ end
12
+
13
+ def create_driver(conf)
14
+ conf = Fluent::Config.parse(conf, "(test)", "(test_dir)", true)
15
+ Fluent::Test::Driver::Parser.new(Fluent::Plugin::MultiFormatParser.new).configure(conf)
16
+ end
17
+
18
+ def test_configure
19
+ conf = %[
20
+ @type multi_format
21
+ <pattern>
22
+ format apache
23
+ </pattern>
24
+ <pattern>
25
+ format json
26
+ time_key timestamp
27
+ </pattern>
28
+ <pattern>
29
+ format none
30
+ </pattern>
31
+ ]
32
+ d = create_driver(conf)
33
+ parsers = d.instance.instance_variable_get(:@parsers)
34
+ assert_instance_of(Fluent::Plugin::ApacheParser, parsers[0])
35
+ assert_instance_of(Fluent::Plugin::JSONParser, parsers[1])
36
+ assert_instance_of(Fluent::Plugin::NoneParser, parsers[2])
37
+ end
38
+
39
+ def test_parse
40
+ conf = %[
41
+ @type multi_format
42
+ <pattern>
43
+ format apache
44
+ </pattern>
45
+ <pattern>
46
+ format json
47
+ time_key timestamp
48
+ </pattern>
49
+ <pattern>
50
+ format none
51
+ </pattern>
52
+ ]
53
+ d = create_driver(conf)
54
+ d.instance.parse('{"k":"v"}') { |t, r|
55
+ assert_equal({"k" => "v"}, r)
56
+ }
57
+ d.instance.parse('hello') { |t, r|
58
+ assert_equal({"message" => "hello"}, r)
59
+ }
60
+ end
61
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-multi-format-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masahiro Nakagawa
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-10 00:00:00.000000000 Z
11
+ date: 2023-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.10.46
19
+ version: 0.14.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '2'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 0.10.46
29
+ version: 0.14.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '2'
@@ -44,12 +44,27 @@ dependencies:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: 0.9.2
47
+ - !ruby/object:Gem::Dependency
48
+ name: test-unit
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.3'
47
61
  description: Multi format parser plugin for Fluentd
48
62
  email: repeatedly@gmail.com
49
63
  executables: []
50
64
  extensions: []
51
65
  extra_rdoc_files: []
52
66
  files:
67
+ - ".bundle/config"
53
68
  - ".travis.yml"
54
69
  - Gemfile
55
70
  - README.md
@@ -57,11 +72,12 @@ files:
57
72
  - VERSION
58
73
  - fluent-plugin-multi-format-parser.gemspec
59
74
  - lib/fluent/plugin/parser_multi_format.rb
75
+ - test/test_parser_multi_format.rb
60
76
  homepage: https://github.com/repeatedly/fluent-plugin-multi-format-parser
61
77
  licenses:
62
78
  - Apache License (2.0)
63
79
  metadata: {}
64
- post_install_message:
80
+ post_install_message:
65
81
  rdoc_options: []
66
82
  require_paths:
67
83
  - lib
@@ -76,9 +92,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
92
  - !ruby/object:Gem::Version
77
93
  version: '0'
78
94
  requirements: []
79
- rubyforge_project:
80
- rubygems_version: 2.5.1
81
- signing_key:
95
+ rubygems_version: 3.4.1
96
+ signing_key:
82
97
  specification_version: 4
83
98
  summary: Multi format parser plugin for Fluentd
84
- test_files: []
99
+ test_files:
100
+ - test/test_parser_multi_format.rb