Class: Fluent::ExecFilterOutput

Inherits:
BufferedOutput show all
Defined in:
lib/fluent/plugin/out_exec_filter.rb

Defined Under Namespace

Classes: ChildProcess, Formatter, JSONFormatter, JSONParser, MessagePackFormatter, MessagePackParser, Parser, TSVFormatter, TSVParser

Constant Summary collapse

SUPPORTED_FORMAT =
{
  'tsv' => :tsv,
  'json' => :json,
  'msgpack' => :msgpack,
}

Instance Attribute Summary

Attributes included from Configurable

#config

Instance Method Summary collapse

Methods inherited from BufferedOutput

#calc_retry_wait, #emit, #enqueue_buffer, #flush_secondary, #force_flush, #submit_flush, #try_flush, #write_abort

Methods inherited from Output

#secondary_init

Methods included from PluginId

#plugin_id, #require_id

Methods included from Configurable

included

Constructor Details

#initializeExecFilterOutput

Returns a new instance of ExecFilterOutput.



24
25
26
# File 'lib/fluent/plugin/out_exec_filter.rb', line 24

def initialize
  super
end

Instance Method Details

#before_shutdownObject



188
189
190
191
# File 'lib/fluent/plugin/out_exec_filter.rb', line 188

def before_shutdown
  super
  sleep 0.5  # TODO wait time before killing child process
end

#configure(conf) ⇒ Object



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
101
102
103
104
105
106
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fluent/plugin/out_exec_filter.rb', line 76

def configure(conf)
  if tag_key = conf['tag_key']
    # TODO obsoleted?
    @in_tag_key = tag_key
    @out_tag_key = tag_key
  end

  if time_key = conf['time_key']
    # TODO obsoleted?
    @in_time_key = time_key
    @out_time_key = time_key
  end

  if time_format = conf['time_format']
    # TODO obsoleted?
    @in_time_format = time_format
    @out_time_format = time_format
  end

  super

  if localtime = conf['localtime']
    @localtime = true
  elsif utc = conf['utc']
    @localtime = false
  end

  if !@tag && !@out_tag_key
    raise ConfigError, "'tag' or 'out_tag_key' option is required on exec_filter output"
  end

  if @in_time_key
    if f = @in_time_format
      tf = TimeFormatter.new(f, @localtime)
      @time_format_proc = tf.method(:format)
    else
      @time_format_proc = Proc.new {|time| time.to_s }
    end
  elsif @in_time_format
    $log.warn "in_time_format effects nothing when in_time_key is not specified: #{conf}"
  end

  if @out_time_key
    if f = @out_time_format
      @time_parse_proc = Proc.new {|str| Time.strptime(str, f).to_i }
    else
      @time_parse_proc = Proc.new {|str| str.to_i }
    end
  elsif @out_time_format
    $log.warn "out_time_format effects nothing when out_time_key is not specified: #{conf}"
  end

  if @remove_prefix
    @removed_prefix_string = @remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  end
  if @add_prefix
    @added_prefix_string = @add_prefix + '.'
  end

  case @in_format
  when :tsv
    if @in_keys.empty?
      raise ConfigError, "in_keys option is required on exec_filter output for tsv in_format"
    end
    @formatter = TSVFormatter.new(@in_keys)
  when :json
    @formatter = JSONFormatter.new
  when :msgpack
    @formatter = MessagePackFormatter.new
  end

  case @out_format
  when :tsv
    if @out_keys.empty?
      raise ConfigError, "out_keys option is required on exec_filter output for tsv in_format"
    end
    @parser = TSVParser.new(@out_keys, method(:on_message))
  when :json
    @parser = JSONParser.new(method(:on_message))
  when :msgpack
    @parser = MessagePackParser.new(method(:on_message))
  end

  @respawns = if @child_respawn.nil? or @child_respawn == 'none' or @child_respawn == '0'
                0
              elsif @child_respawn == 'inf' or @child_respawn == '-1'
                -1
              elsif @child_respawn =~ /^\d+$/
                @child_respawn.to_i
              else
                raise ConfigError, "child_respawn option argument invalid: none(or 0), inf(or -1) or positive number"
              end
end

#format_stream(tag, es) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/fluent/plugin/out_exec_filter.rb', line 202

def format_stream(tag, es)
  if @remove_prefix
    if (tag[0, @removed_length] == @removed_prefix_string and tag.length > @removed_length) or tag == @removed_prefix
      tag = tag[@removed_length..-1] || ''
    end
  end

  out = ''

  es.each {|time,record|
    if @in_time_key
      record[@in_time_key] = @time_format_proc.call(time)
    end
    if @in_tag_key
      record[@in_tag_key] = tag
    end
    @formatter.call(record, out)
  }

  out
end

#on_message(record) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/fluent/plugin/out_exec_filter.rb', line 355

def on_message(record)
  if val = record.delete(@out_time_key)
    time = @time_parse_proc.call(val)
  else
    time = Engine.now
  end

  if val = record.delete(@out_tag_key)
    tag = if @add_prefix
            @added_prefix_string + val
          else
            val
          end
  else
    tag = @tag
  end

  Engine.emit(tag, time, record)

rescue
  $log.error "exec_filter failed to emit", :error=>$!.to_s, :record=>Yajl.dump(record)
  $log.warn_backtrace $!.backtrace
end

#shutdownObject



193
194
195
196
197
198
199
200
# File 'lib/fluent/plugin/out_exec_filter.rb', line 193

def shutdown
  super

  @children.reject! {|c|
    c.shutdown
    true
  }
end

#startObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fluent/plugin/out_exec_filter.rb', line 171

def start
  super

  @children = []
  @rr = 0
  begin
    @num_children.times do
      c = ChildProcess.new(@parser, @respawns)
      c.start(@command)
      @children << c
    end
  rescue
    shutdown
    raise
  end
end

#write(chunk) ⇒ Object



224
225
226
227
# File 'lib/fluent/plugin/out_exec_filter.rb', line 224

def write(chunk)
  r = @rr = (@rr + 1) % @children.length
  @children[r].write chunk
end