lib/fluent/plugin/grok.rb in fluent-plugin-grok-parser-0.0.4 vs lib/fluent/plugin/grok.rb in fluent-plugin-grok-parser-0.1.0

- old
+ new

@@ -7,11 +7,14 @@ # See https://github.com/jordansissel/ruby-grok/blob/master/lib/grok-pure.rb PATTERN_RE = \ /%\{ # match '%{' not prefixed with '\' (?<name> # match the pattern name (?<pattern>[A-z0-9]+) - (?::(?<subname>[@\[\]A-z0-9_:.-]+))? + (?::(?<subname>[@\[\]A-z0-9_:.-]+?) + (?::(?<type>(?:string|bool|integer|float| + time(?::.+)?| + array(?::.)?)))?)? ) \}/x attr_reader :parsers attr_reader :multiline_start_regexp @@ -49,37 +52,42 @@ end private def expand_pattern_expression(grok_pattern, conf) - regexp = expand_pattern(grok_pattern) + regexp, types = expand_pattern(grok_pattern) $log.info "Expanded the pattern #{conf['grok_pattern']} into #{regexp}" options = nil if @multiline_mode options = Regexp::MULTILINE end + unless types.empty? + conf["types"] = types.map{|subname,type| "#{subname}:#{type}" }.join(",") + end TextParser::RegexpParser.new(Regexp.new(regexp, options), conf) rescue GrokPatternNotFoundError => e raise e rescue => e $log.error e.backtrace.join("\n") end def expand_pattern(pattern) # It's okay to modify in place. no need to expand it more than once. + type_map = {} while true m = PATTERN_RE.match(pattern) break unless m curr_pattern = @pattern_map[m["pattern"]] raise GrokPatternNotFoundError unless curr_pattern - replacement_pattern = if m["subname"] - "(?<#{m["subname"]}>#{curr_pattern})" - else - curr_pattern - end + if m["subname"] + replacement_pattern = "(?<#{m["subname"]}>#{curr_pattern})" + type_map[m["subname"]] = m["type"] || "string" + else + replacement_pattern = curr_pattern + end pattern.sub!(m[0]) do |s| replacement_pattern end end - pattern + [pattern, type_map] end end end