From cc1200b2c20f62f403295fa5fff44a012e4e11ba Mon Sep 17 00:00:00 2001 From: Kenji Okimoto Date: Mon, 11 Jul 2016 15:06:06 +0900 Subject: [PATCH 01/10] Extract Fluent::Plugin::RegexpParser We can use Fluent::Plugin::RegexpParser as following: ``` @type regexp expression /pattern.+/ ``` --- lib/fluent/compat/parser.rb | 17 ++++++ lib/fluent/plugin/parser.rb | 69 ------------------------ lib/fluent/plugin/parser_apache.rb | 2 +- lib/fluent/plugin/parser_apache_error.rb | 2 +- lib/fluent/plugin/parser_multiline.rb | 6 +-- lib/fluent/plugin/parser_nginx.rb | 2 +- lib/fluent/plugin/parser_regexp.rb | 68 +++++++++++++++++++++++ test/plugin/test_parser_regexp.rb | 16 +++--- 8 files changed, 99 insertions(+), 83 deletions(-) create mode 100644 lib/fluent/plugin/parser_regexp.rb diff --git a/lib/fluent/compat/parser.rb b/lib/fluent/compat/parser.rb index af9bd61736..836ee1eeb9 100644 --- a/lib/fluent/compat/parser.rb +++ b/lib/fluent/compat/parser.rb @@ -18,6 +18,7 @@ require 'fluent/plugin/parser' require 'fluent/mixin' +require 'fluent/plugin/parser_regexp' require 'fluent/plugin/parser_json' require 'fluent/plugin/parser_tsv' require 'fluent/plugin/parser_ltsv' @@ -121,6 +122,22 @@ class TimeParser < Fluent::Plugin::Parser::TimeParser class RegexpParser < Fluent::Plugin::RegexpParser # TODO: warn when deprecated + def initialize(regexp, conf = {}) + super() + + unless conf.empty? + unless conf.is_a?(Config::Element) + conf = Config::Element.new('default_regexp_conf', '', conf, []) + end + configure(conf) + end + + @regexp = regexp + end + + def patterns + {'format' => @regexp, 'time_format' => @time_format} + end end class ValuesParser < Fluent::Plugin::ValuesParser diff --git a/lib/fluent/plugin/parser.rb b/lib/fluent/plugin/parser.rb index b3f085adc0..8faf713869 100644 --- a/lib/fluent/plugin/parser.rb +++ b/lib/fluent/plugin/parser.rb @@ -97,75 +97,6 @@ def parse(value) end end - class RegexpParser < Parser - include Fluent::TypeConverter - - config_param :time_key, :string, default: 'time' - config_param :time_format, :string, default: nil - - def initialize(regexp, conf={}) - super() - - unless conf.empty? - unless conf.is_a?(Config::Element) - conf = Config::Element.new('default_regexp_conf', '', conf, []) - end - configure(conf) - end - - @regexp = regexp - @time_parser = TimeParser.new(@time_format) - @mutex = Mutex.new - end - - def configure(conf) - super - @time_parser = TimeParser.new(@time_format) - end - - def patterns - {'format' => @regexp, 'time_format' => @time_format} - end - - def parse(text) - m = @regexp.match(text) - unless m - yield nil, nil - return - end - - time = nil - record = {} - - m.names.each do |name| - if value = m[name] - if name == @time_key - time = @mutex.synchronize { @time_parser.parse(value) } - if @keep_time_key - record[name] = if @type_converters.nil? - value - else - convert_type(name, value) - end - end - else - record[name] = if @type_converters.nil? - value - else - convert_type(name, value) - end - end - end - end - - if @estimate_current_event - time ||= Fluent::EventTime.now - end - - yield time, record - end - end - class ValuesParser < Parser include Fluent::TypeConverter diff --git a/lib/fluent/plugin/parser_apache.rb b/lib/fluent/plugin/parser_apache.rb index 607933a784..e33b55659a 100644 --- a/lib/fluent/plugin/parser_apache.rb +++ b/lib/fluent/plugin/parser_apache.rb @@ -17,7 +17,7 @@ require 'fluent/plugin/parser' Fluent::Plugin.register_parser('apache', Proc.new{ - Fluent::Plugin::RegexpParser.new( + Fluent::Compat::TextParser::RegexpParser.new( /^(?[^ ]*) [^ ]* (?[^ ]*) \[(?