Class: Fluent::TailMultilineInput

Inherits:
TailInput
  • Object
show all
Defined in:
lib/fluent/plugin/in_tail_multiline.rb

Defined Under Namespace

Classes: MultilineTextParser

Constant Summary collapse

FORMAT_MAX_NUM =
20

Instance Method Summary collapse

Constructor Details

#initializeTailMultilineInput

Returns a new instance of TailMultilineInput.



60
61
62
63
64
65
# File 'lib/fluent/plugin/in_tail_multiline.rb', line 60

def initialize
  super
  @locker = Monitor.new
  @logbuf = nil
  @logbuf_flusher = CallLater::new
end

Instance Method Details

#configure(conf) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fluent/plugin/in_tail_multiline.rb', line 67

def configure(conf)
  if conf['format'].nil?
    invalids = conf.keys.select{|k| k =~ /^format(\d+)$/ and not (1..FORMAT_MAX_NUM).include?($1.to_i)}
    if invalids.size > 0
      raise ConfigError, "invalid number formats (valid format number:1-#{FORMAT_MAX_NUM}):" + invalids.join(",")
    end
    format_index_list = conf.keys.select{|s| s =~ /^format\d+$/}.map{|v| (/^format(\d+)$/.match(v))[1].to_i}
    if (1..format_index_list.max).map{|i| conf["format#{i}"]}.include?(nil)
      raise Fluent::ConfigError, "jump of format index found"
    end
    formats = (1..FORMAT_MAX_NUM).map {|i|
      conf["format#{i}"]
    }.delete_if {|format|
      format.nil?
    }.map {|format|
      format[1..-2]
    }.join
    conf['format'] = '/' + formats + '/'
  end
  super
  if read_newfile_from_head and @pf
    # Tread new file as rotated file
    # Use temp file inode number as previos logfile
    @paths.map {|path|
      pe = @pf[path]
      if pe.read_inode == 0
        require 'tempfile'
        tmpfile = Tempfile.new('gettempinode')
        pe.update(File.stat(tmpfile).ino, 0)
        tmpfile.unlink
      end
    }
  end
end

#configure_parser(conf) ⇒ Object



102
103
104
105
# File 'lib/fluent/plugin/in_tail_multiline.rb', line 102

def configure_parser(conf)
  @parser = MultilineTextParser.new
  @parser.configure(conf)
end

#flush_logbufObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/fluent/plugin/in_tail_multiline.rb', line 141

def flush_logbuf
  time, record = nil,nil
  @locker.synchronize do
    time, record = parse_logbuf(@logbuf)
    @logbuf = nil
  end
  if time && record
    Engine.emit(@tag, time, record)
  end
end

#parse_logbuf(buf) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fluent/plugin/in_tail_multiline.rb', line 152

def parse_logbuf(buf)
  return nil,nil unless buf
  buf.chomp!
  begin
    time, record = @parser.parse(buf)
  rescue
    $log.warn line.dump, :error=>$!.to_s
    $log.debug_backtrace
  end
  return nil,nil unless time && record
  record[@rawdata_key] = buf if @rawdata_key
  return time, record
end

#receive_lines(lines) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fluent/plugin/in_tail_multiline.rb', line 107

def receive_lines(lines)
  @logbuf_flusher.cancel()
  es = MultiEventStream.new
  @locker.synchronize do
    lines.each {|line|
        if @parser.match_firstline(line)
          time, record = parse_logbuf(@logbuf)
          if time && record
            es.add(time, record)
          end
          @logbuf = line
        else
          @logbuf += line if(@logbuf)
        end
    }
  end
  unless es.empty?
    begin
      Engine.emit_stream(@tag, es)
    rescue
      # ignore errors. Engine shows logs and backtraces.
    end
  end
  @logbuf_flusher.call_later(@auto_flush_sec) do
    flush_logbuf()
  end
end

#shutdownObject



135
136
137
138
139
# File 'lib/fluent/plugin/in_tail_multiline.rb', line 135

def shutdown
  super
  flush_logbuf()
  @logbuf_flusher.shutdown()
end