Class: Fluent::ParserFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParserFilter

Returns a new instance of ParserFilter.



17
18
19
20
# File 'lib/fluent/plugin/filter_parser.rb', line 17

def initialize
  super
  require 'time'
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



15
16
17
# File 'lib/fluent/plugin/filter_parser.rb', line 15

def parser
  @parser
end

Instance Method Details

#configure(conf) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fluent/plugin/filter_parser.rb', line 22

def configure(conf)
  super

  @parser = Fluent::TextParser.new
  @parser.estimate_current_event = false
  @parser.configure(conf)
  if !@time_parse && @parser.parser.respond_to?("time_key=".to_sym)
    # disable parse time
    @parser.parser.time_key = nil
  end

  self
end

#filter_stream(tag, es) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluent/plugin/filter_parser.rb', line 36

def filter_stream(tag, es)
  new_es = Fluent::MultiEventStream.new
  es.each do |time,record|
    raw_value = record[@key_name]
    if raw_value.nil?
      log.warn "#{@key_name} does not exist" unless @ignore_key_not_exist
      new_es.add(time, handle_parsed(tag, record, time, {})) if @reserve_data
      next
    end
    begin
      @parser.parse(raw_value) do |t,values|
        if values
          t ||= time
          r = handle_parsed(tag, record, t, values)
          new_es.add(t, r)
        else
          log.warn "pattern not match with data '#{raw_value}'" unless @suppress_parse_error_log
          if @reserve_data
            t = time
            r = handle_parsed(tag, record, time, {})
            new_es.add(t, r)
          end
        end
      end
    rescue Fluent::TextParser::ParserError => e
      log.warn e.message unless @suppress_parse_error_log
    rescue ArgumentError => e
      if @replace_invalid_sequence
        unless e.message.index("invalid byte sequence in") == 0
          raise
        end
        replaced_string = replace_invalid_byte(raw_value)
        @parser.parse(replaced_string) do |t,values|
          if values
            t ||= time
            r = handle_parsed(tag, record, t, values)
            new_es.add(t, r)
          else
            log.warn "pattern not match with data '#{raw_value}'" unless @suppress_parse_error_log
            if @reserve_data
              t = time
              r = handle_parsed(tag, record, time, {})
              new_es.add(t, r)
            end
          end
        end
      else
        raise
      end
    rescue => e
      log.warn "parse failed #{e.message}" unless @suppress_parse_error_log
    end
  end
  new_es
end