Skip to content

Commit 5e65da5

Browse files
authored
Merge pull request #20 from mlasevich/master
Add ability to inject a field based on matching pattern
2 parents 2d85cc9 + 4ca910b commit 5e65da5

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,43 @@ Use `format` instead of `<parse></parse>`.
8686
</pattern>
8787
</filter>
8888

89+
### Adding format identity field
90+
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.
94+
95+
Example:
96+
97+
98+
<filter app.**>
99+
@type parser
100+
key_name message
101+
<parse>
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+
108+
<pattern>
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.../
118+
</pattern>
119+
<pattern>
120+
format none
121+
format_name 'unparsed'
122+
</pattern>
123+
</parse>
124+
</filter>
125+
89126
### NOTE
90127

91128
This plugin doesn't work with `multiline` parsers because parser itself doesn't store previous lines.

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
1.1.0

fluent-plugin-multi-format-parser.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
99
gem.version = File.read("VERSION").strip
1010
gem.authors = ["Masahiro Nakagawa"]
1111
gem.email = "[email protected]"
12-
gem.has_rdoc = false
12+
#gem.has_rdoc = false
1313
#gem.platform = Gem::Platform::RUBY
1414
gem.license = 'Apache License (2.0)'
1515
gem.files = `git ls-files`.split("\n")

lib/fluent/plugin/parser_multi_format.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,36 @@ module Fluent
44
module Plugin
55
class MultiFormatParser < Parser
66
Plugin.register_parser('multi_format', self)
7+
config_param :format_key, :string, default: nil
78

89
def initialize
910
super
1011

1112
@parsers = []
13+
@parser_format_names = []
1214
end
1315

1416
def configure(conf)
1517
super
1618

17-
conf.elements.each { |e|
19+
conf.elements.each_with_index { |e, i|
1820
next unless ['pattern', 'format'].include?(e.name)
1921
next if e['format'].nil? && (e['@type'] == 'multi_format')
20-
22+
@parser_format_names << e.delete('format_name') || ""+e['format']+"#"+i.to_s
2123
parser = Plugin.new_parser(e['format'])
2224
parser.configure(e)
2325
@parsers << parser
2426
}
2527
end
2628

2729
def parse(text)
28-
@parsers.each { |parser|
30+
@parsers.each_with_index { |parser, i|
2931
begin
3032
parser.parse(text) { |time, record|
3133
if time && record
34+
if @format_key
35+
record[@format_key] = @parser_format_names[i]
36+
end
3237
yield time, record
3338
return
3439
end

0 commit comments

Comments
 (0)