Skip to content

Commit 9d17dd4

Browse files
authored
Merge pull request #59 from fluent/support-time-format-parameter-in-grok-section
Support time_format parameter in grok section
2 parents 2e3e2c8 + f82b0d7 commit 9d17dd4

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

lib/fluent/plugin/grok.rb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def initialize(plugin, conf)
3636
if @plugin.respond_to?(:keep_time_key)
3737
@keep_time_key = @plugin.keep_time_key
3838
end
39+
if @plugin.respond_to?(:time_format)
40+
@time_format = @plugin.time_format
41+
end
3942
end
4043

4144
def add_patterns_from_file(path)
@@ -48,10 +51,10 @@ def add_patterns_from_file(path)
4851

4952
def setup
5053
if @plugin.grok_pattern
51-
@parsers[:grok_pattern] = expand_pattern_expression(@plugin.grok_pattern, @conf)
54+
@parsers[:grok_pattern] = expand_pattern_expression_grok_pattern(@plugin.grok_pattern, @conf)
5255
else
5356
@plugin.grok_confs.each.with_index do |grok_conf, index|
54-
@parsers[grok_conf.name || index] = expand_pattern_expression(grok_conf.pattern, grok_conf)
57+
@parsers[grok_conf.name || index] = expand_pattern_expression_grok_section(grok_conf)
5558
end
5659
end
5760
@parsers.reject! do |key, parser|
@@ -64,7 +67,7 @@ def setup
6467

6568
private
6669

67-
def expand_pattern_expression(grok_pattern, conf)
70+
def expand_pattern_expression_grok_pattern(grok_pattern, conf)
6871
regexp, types = expand_pattern(grok_pattern)
6972
$log.info "Expanded the pattern #{grok_pattern} into #{regexp}"
7073
_conf = conf.to_h
@@ -83,6 +86,37 @@ def expand_pattern_expression(grok_pattern, conf)
8386
nil
8487
end
8588

89+
def expand_pattern_expression_grok_section(conf)
90+
regexp, types = expand_pattern(conf.pattern)
91+
$log.info "Expanded the pattern #{conf.pattern} into #{regexp}"
92+
_conf = conf.to_h
93+
unless types.empty?
94+
_conf["types"] = types.map{|subname,type| "#{subname}:#{type}" }.join(",")
95+
end
96+
if conf["multiline"] || @multiline_mode
97+
_conf["multiline"] = conf["multiline"] || @multiline_mode
98+
end
99+
if conf["keep_time_key"] || @keep_time_key
100+
_conf["keep_time_key"] = conf["keep_time_key"] || @keep_time_key
101+
end
102+
if conf["time_key"]
103+
_conf["time_key"] = conf["time_key"]
104+
end
105+
if conf["time_format"] || @time_format
106+
_conf["time_format"] = conf["time_format"] || @time_format
107+
end
108+
_conf["expression"] = regexp
109+
config = Fluent::Config::Element.new("parse", "", _conf, [])
110+
parser = Fluent::Plugin::RegexpParser.new
111+
parser.configure(config)
112+
parser
113+
rescue GrokPatternNotFoundError => e
114+
raise e
115+
rescue => e
116+
$log.error(error: e)
117+
nil
118+
end
119+
86120
def expand_pattern(pattern)
87121
# It's okay to modify in place. no need to expand it more than once.
88122
type_map = {}

lib/fluent/plugin/parser_grok.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class GrokParser < Parser
2222
config_param :name, :string, default: nil
2323
desc "The pattern of grok"
2424
config_param :pattern, :string
25+
config_param :keep_time_key, :bool, default: false
26+
config_param :time_key, :string, default: "time"
27+
config_param :time_format, :string, default: nil
2528
end
2629

2730
def initialize

test/test_grok_parser.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,34 @@ class GrokParserTest < ::Test::Unit::TestCase
321321
end
322322
end
323323

324+
sub_test_case "grok section" do
325+
test "complex pattern" do
326+
d = create_driver(%[
327+
<grok>
328+
pattern %{COMBINEDAPACHELOG}
329+
time_key timestamp
330+
time_format %d/%b/%Y:%H:%M:%S %z
331+
</grok>
332+
])
333+
expected_record = {
334+
"clientip" => "127.0.0.1",
335+
"ident" => "192.168.0.1",
336+
"auth" => "-",
337+
"verb" => "GET",
338+
"request" => "/",
339+
"httpversion" => "1.1",
340+
"response" => "200",
341+
"bytes" => "777",
342+
"referrer" => "\"-\"",
343+
"agent" => "\"Opera/12.0\""
344+
}
345+
d.instance.parse('127.0.0.1 192.168.0.1 - [28/Feb/2013:12:00:00 +0900] "GET / HTTP/1.1" 200 777 "-" "Opera/12.0"') do |time, record|
346+
assert_equal(expected_record, record)
347+
assert_equal(event_time("28/Feb/2013:12:00:00 +0900", format: "%d/%b/%Y:%H:%M:%S %z"), time)
348+
end
349+
end
350+
end
351+
324352
private
325353

326354
def create_driver(conf)

0 commit comments

Comments
 (0)