Skip to content

Commit 03cc38e

Browse files
committed
Add expand_pattern_expression_grok_section
Signed-off-by: Kenji Okimoto <okimoto@clear-code.com>
1 parent 9784ba8 commit 03cc38e

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

lib/fluent/plugin/grok.rb

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def add_patterns_from_file(path)
4848

4949
def setup
5050
if @plugin.grok_pattern
51-
@parsers[:grok_pattern] = expand_pattern_expression(@plugin.grok_pattern, @conf)
51+
@parsers[:grok_pattern] = expand_pattern_expression_grok_pattern(@plugin.grok_pattern, @conf)
5252
else
5353
@plugin.grok_confs.each.with_index do |grok_conf, index|
54-
@parsers[grok_conf.name || index] = expand_pattern_expression(grok_conf.pattern, grok_conf)
54+
@parsers[grok_conf.name || index] = expand_pattern_expression_grok_section(grok_conf)
5555
end
5656
end
5757
@parsers.reject! do |key, parser|
@@ -64,17 +64,44 @@ def setup
6464

6565
private
6666

67-
def expand_pattern_expression(grok_pattern, conf)
67+
def expand_pattern_expression_grok_pattern(grok_pattern, conf)
6868
regexp, types = expand_pattern(grok_pattern)
6969
$log.info "Expanded the pattern #{grok_pattern} into #{regexp}"
7070
_conf = conf.to_h
7171
unless types.empty?
7272
_conf["types"] = types.map{|subname,type| "#{subname}:#{type}" }.join(",")
7373
end
74-
_conf["multiline"] = conf["multiline"] || @multiline_mode
75-
_conf["keep_time_key"] = conf["keep_time_key"] || @keep_time_key
76-
_conf["time_key"] = conf["time_key"] || "time"
77-
_conf["time_format"] = conf["time_format"] || @time_format
74+
_conf = _conf.merge("expression" => regexp, "multiline" => @multiline_mode, "keep_time_key" => @keep_time_key)
75+
config = Fluent::Config::Element.new("parse", nil, _conf, [])
76+
parser = Fluent::Plugin::RegexpParser.new
77+
parser.configure(config)
78+
parser
79+
rescue GrokPatternNotFoundError => e
80+
raise e
81+
rescue => e
82+
$log.error(error: e)
83+
nil
84+
end
85+
86+
def expand_pattern_expression_grok_section(conf)
87+
regexp, types = expand_pattern(conf.pattern)
88+
$log.info "Expanded the pattern #{conf.pattern} into #{regexp}"
89+
_conf = conf.to_h
90+
unless types.empty?
91+
_conf["types"] = types.map{|subname,type| "#{subname}:#{type}" }.join(",")
92+
end
93+
if conf["multiline"] || @multiline_mode
94+
_conf["multiline"] = conf["multiline"] || @multiline_mode
95+
end
96+
if conf["keep_time_key"] || @keep_time_key
97+
_conf["keep_time_key"] = conf["keep_time_key"] || @keep_time_key
98+
end
99+
if conf["time_key"]
100+
_conf["time_key"] = conf["time_key"]
101+
end
102+
if conf["time_format"] || @time_format
103+
_conf["time_format"] = conf["time_format"] || @time_format
104+
end
78105
_conf["expression"] = regexp
79106
config = Fluent::Config::Element.new("parse", "", _conf, [])
80107
parser = Fluent::Plugin::RegexpParser.new

0 commit comments

Comments
 (0)