Class: Fluent::Plugin::TailInput::LineFeeder

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/in_tail/line_feeder.rb

Overview

LineFeeder provides the built-in line parsing and emitting for TailInput. To preserve compatibility with TailInput subclasses, TailInput passes its line-processing methods as handlers. This keeps subclass overrides in the call path, and super reaches this built-in implementation.

Defined Under Namespace

Classes: FileFeed

Instance Method Summary collapse

Constructor Details

#initialize(parser:, router_provider:, log:, tag:, tag_prefix:, tag_suffix:, path_key:, emit_unmatched_lines:, multiline_mode:, parse_handler: nil, convert_handler: nil, flush_handler: nil) ⇒ LineFeeder

Returns a new instance of LineFeeder.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fluent/plugin/in_tail/line_feeder.rb', line 29

def initialize(parser:, router_provider:, log:, tag:, tag_prefix:, tag_suffix:, path_key:, emit_unmatched_lines:, multiline_mode:, parse_handler: nil, convert_handler: nil, flush_handler: nil)
  @parser = parser
  @router_provider = router_provider
  @log = log
  @tag = tag
  @tag_prefix = tag_prefix
  @tag_suffix = tag_suffix
  @path_key = path_key
  @emit_unmatched_lines = emit_unmatched_lines
  @multiline_mode = multiline_mode
  @parse_handler = parse_handler || if multiline_mode
                                     method(:parse_multilines)
                                   else
                                     method(:parse_singleline)
                                   end
  @convert_handler = convert_handler || method(:convert_line_to_event)
  @flush_handler = flush_handler || method(:flush_buffer)
end

Instance Method Details

#convert_line_to_event(line, es, tail_watcher) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fluent/plugin/in_tail/line_feeder.rb', line 148

def convert_line_to_event(line, es, tail_watcher)
  begin
    line.chomp!  # remove \n
    @parser.parse(line) { |time, record|
      if time && record
        record[@path_key] ||= tail_watcher.path unless @path_key.nil?
        es.add(time, record)
      else
        if @emit_unmatched_lines
          record = {'unmatched_line' => line}
          record[@path_key] ||= tail_watcher.path unless @path_key.nil?
          es.add(Fluent::EventTime.now, record)
        end
        @log.warn { "pattern not matched: #{line.inspect}" }
      end
    }
  rescue => e
    @log.warn 'invalid line found', file: tail_watcher.path, line: line, error: e.to_s
    @log.debug_backtrace(e.backtrace)
  end
end

#emit(es, tail_watcher) ⇒ Object

Emits the events returned by #parse, with the tag of the file of the watcher which the lines were read from.

Returns:

  • true if no error or unrecoverable error happens in emit action. false if got BufferOverflowError



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/plugin/in_tail/line_feeder.rb', line 69

def emit(es, tail_watcher)
  return true if es.empty?

  begin
    @router_provider.call.emit_stream(tag_for(tail_watcher), es)
  rescue Fluent::Plugin::Buffer::BufferOverflowError
    return false
  rescue
    # ignore non BufferQueueLimitError errors because in_tail can't recover. Engine shows logs and backtraces.
    return true
  end

  return true
end

#feed_lines(lines, tail_watcher) ⇒ Object

Parses and emits a batch for FileFeed and legacy watchers. The flush deadline is started by #parse_multilines for compatibility.

Returns:

  • true if no error or unrecoverable error happens in emit action. false if got BufferOverflowError



58
59
60
# File 'lib/fluent/plugin/in_tail/line_feeder.rb', line 58

def feed_lines(lines, tail_watcher)
  emit(parse(lines, tail_watcher), tail_watcher)
end

#flush_buffer(tw, buf) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fluent/plugin/in_tail/line_feeder.rb', line 84

def flush_buffer(tw, buf)
  buf.chomp!
  @parser.parse(buf) { |time, record|
    if time && record
      record[@path_key] ||= tw.path unless @path_key.nil?
      @router_provider.call.emit(tag_for(tw), time, record)
    else
      if @emit_unmatched_lines
        record = { 'unmatched_line' => buf }
        record[@path_key] ||= tw.path unless @path_key.nil?
        @router_provider.call.emit(tag_for(tw), Fluent::EventTime.now, record)
      end
      @log.warn "got incomplete line at shutdown from #{tw.path}: #{buf.inspect}"
    end
  }
end

#new_file_feed(flush_interval: nil) ⇒ Object

Builds a per-file feed. The flush interval applies only to multiline parsers with a firstline.



50
51
52
53
# File 'lib/fluent/plugin/in_tail/line_feeder.rb', line 50

def new_file_feed(flush_interval: nil)
  timer_flush_interval = (@multiline_mode && @parser.has_firstline?) ? flush_interval : nil
  FileFeed.new(self, flush_interval: timer_flush_interval, flush_handler: @flush_handler)
end

#parse(lines, tail_watcher) ⇒ Object



62
63
64
# File 'lib/fluent/plugin/in_tail/line_feeder.rb', line 62

def parse(lines, tail_watcher)
  @parse_handler.call(lines, tail_watcher)
end

#parse_multilines(lines, tail_watcher) ⇒ Object

Parses multiline input and starts the flush deadline before parsing, as TailInput#parse_multilines did before LineFeeder was extracted.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/fluent/plugin/in_tail/line_feeder.rb', line 180

def parse_multilines(lines, tail_watcher)
  file_feed = tail_watcher.file_feed
  lb = file_feed.line_buffer
  es = Fluent::MultiEventStream.new
  if @parser.has_firstline?
    file_feed.reset_timer
    lines.each { |line|
      if @parser.firstline?(line)
        if lb
          @convert_handler.call(lb, es, tail_watcher)
        end
        lb = line
      else
        if lb.nil?
          if @emit_unmatched_lines
            @convert_handler.call(line, es, tail_watcher)
          end
          @log.warn "got incomplete line before first line from #{tail_watcher.path}: #{line.inspect}"
        else
          lb << line
        end
      end
    }
  else
    lb ||= ''
    lines.each do |line|
      lb << line
      @parser.parse(lb) { |time, record|
        if time && record
          @convert_handler.call(lb, es, tail_watcher)
          lb = ''
        end
      }
    end
  end
  file_feed.line_buffer = lb
  es
end

#parse_singleline(lines, tail_watcher) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/fluent/plugin/in_tail/line_feeder.rb', line 170

def parse_singleline(lines, tail_watcher)
  es = Fluent::MultiEventStream.new
  lines.each { |line|
    @convert_handler.call(line, es, tail_watcher)
  }
  es
end