Module: Fluent::PluginHelper::CompatParameters

Defined in:
lib/fluent/plugin_helper/compat_parameters.rb

Constant Summary collapse

BUFFER_PARAMS =

This plugin helper is to bring old-fashioned buffer/other configuration parameters to v0.14 plugin API configurations. This helper is mainly to convert plugins from v0.12 API to v0.14 API safely, without breaking user deployment.

{
  "buffer_type" => "@type",
  "buffer_path" => "path",
  "num_threads"                 => "flush_thread_count",
  "flush_interval"              => "flush_interval",
  "try_flush_interval"          => "flush_thread_interval",
  "queued_chunk_flush_interval" => "flush_thread_burst_interval",
  "disable_retry_limit" => "retry_forever",
  "retry_limit"         => "retry_max_times",
  "max_retry_wait"      => "retry_max_interval",
  "buffer_chunk_limit"  => "chunk_limit_size",
  "buffer_queue_limit"  => "queue_limit_length",
  "buffer_queue_full_action" => "overflow_action",
  "flush_at_shutdown" => "flush_at_shutdown",
}
BUFFER_TIME_SLICED_PARAMS =
{
  "time_slice_format" => nil,
  "time_slice_wait" => "timekey_wait",
  "timezone" => "timekey_zone",
}
PARSER_PARAMS =
{
  "format" => nil,
  "types" => nil,
  "types_delimiter" => nil,
  "types_label_delimiter" => nil,
  "keys" => "keys", # CSVParser, TSVParser (old ValuesParser)
  "time_key"    => "time_key",
  "time_format" => "time_format",
  "localtime" => nil,
  "utc" => nil,
  "delimiter"   => "delimiter",
  "keep_time_key" => "keep_time_key",
  "null_empty_string" => "null_empty_string",
  "null_value_pattern" => "null_value_pattern",
  "json_parser"      => "json_parser", # JSONParser
  "label_delimiter"  => "label_delimiter", # LabeledTSVParser
  "format_firstline" => "format_firstline", # MultilineParser
  "message_key"      => "message_key", # NoneParser
  "with_priority"       => "with_priority", # SyslogParser
  "message_format"      => "message_format", # SyslogParser
  "rfc5424_time_format" => "rfc5424_time_format", # SyslogParser
  # There has been no parsers which can handle timezone in v0.12
}
INJECT_PARAMS =
{
  "include_time_key" => nil,
  "time_key"    => "time_key",
  "time_format" => "time_format",
  "timezone"    => "timezone",
  "include_tag_key" => nil,
  "tag_key" => "tag_key",
  "localtime" => nil,
  "utc" => nil,
}
EXTRACT_PARAMS =
{
  "time_key"    => "time_key",
  "time_format" => "time_format",
  "timezone"    => "timezone",
  "tag_key" => "tag_key",
  "localtime" => nil,
  "utc" => nil,
}
FORMATTER_PARAMS =
{
  "format" => "@type",
  "delimiter" => "delimiter",
  "force_quotes" => "force_quotes", # CsvFormatter
  "keys" => "keys", # TSVFormatter
  "fields" => "fields", # CsvFormatter
  "json_parser" => "json_parser", # JSONFormatter
  "label_delimiter" => "label_delimiter", # LabeledTSVFormatter
  "output_time" => "output_time", # OutFileFormatter
  "output_tag"  => "output_tag",  # OutFileFormatter
  "localtime"   => "localtime",   # OutFileFormatter
  "utc"         => "utc",         # OutFileFormatter
  "timezone"    => "timezone",    # OutFileFormatter
  "message_key" => "message_key", # SingleValueFormatter
  "add_newline" => "add_newline", # SingleValueFormatter
  "output_type" => "output_type", # StdoutFormatter
}

Instance Method Summary collapse

Instance Method Details

#compat_parameters_buffer(conf, default_chunk_key: '') ⇒ Object



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
170
171
172
173
174
175
176
177
178
179
# File 'lib/fluent/plugin_helper/compat_parameters.rb', line 133

def compat_parameters_buffer(conf, default_chunk_key: '')
  # return immediately if <buffer> section exists, or any buffer-related parameters don't exist
  return unless conf.elements('buffer').empty?
  return if (BUFFER_PARAMS.keys + BUFFER_TIME_SLICED_PARAMS.keys).all?{|k| !conf.has_key?(k) }

  # TODO: warn obsolete parameters if these are deprecated
  buffer_params = BUFFER_PARAMS.merge(BUFFER_TIME_SLICED_PARAMS)
  hash = compat_parameters_copy_to_subsection_attributes(conf, buffer_params) do |compat_key, value|
    if compat_key == 'buffer_queue_full_action' && value == 'exception'
      'throw_exception'
    else
      value
    end
  end

  chunk_key = default_chunk_key

  if conf.has_key?('time_slice_format')
    chunk_key = 'time'
    hash['timekey'] = case conf['time_slice_format']
                      when /\%S/ then 1
                      when /\%M/ then 60
                      when /\%H/ then 3600
                      when /\%d/ then 86400
                      else
                        raise Fluent::ConfigError, "time_slice_format only with %Y or %m is too long"
                      end
    if conf.has_key?('localtime') || conf.has_key?('utc')
      if conf.has_key?('localtime') && conf.has_key?('utc')
        raise Fluent::ConfigError, "both of utc and localtime are specified, use only one of them"
      elsif conf.has_key?('localtime')
        hash['timekey_use_utc'] = !(Fluent::Config.bool_value(conf['localtime']))
      elsif conf.has_key?('utc')
        hash['timekey_use_utc'] = Fluent::Config.bool_value(conf['utc'])
      end
    end
  else
    if chunk_key == 'time'
      hash['timekey'] = 86400 # TimeSliceOutput.time_slice_format default value is '%Y%m%d'
    end
  end

  e = Fluent::Config::Element.new('buffer', chunk_key, hash, [])
  conf.elements << e

  conf
end

#compat_parameters_convert(conf, *types, **kwargs) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fluent/plugin_helper/compat_parameters.rb', line 112

def compat_parameters_convert(conf, *types, **kwargs)
  types.each do |type|
    case type
    when :buffer
      compat_parameters_buffer(conf, **kwargs)
    when :inject
      compat_parameters_inject(conf)
    when :extract
      compat_parameters_extract(conf)
    when :parser
      compat_parameters_parser(conf)
    when :formatter
      compat_parameters_formatter(conf)
    else
      raise "BUG: unknown compat_parameters type: #{type}"
    end
  end

  conf
end

#compat_parameters_copy_to_subsection_attributes(conf, params, &block) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/fluent/plugin_helper/compat_parameters.rb', line 327

def compat_parameters_copy_to_subsection_attributes(conf, params, &block)
  hash = {}
  params.each do |compat, current|
    next unless current
    if conf.has_key?(compat)
      if block_given?
        hash[current] = block.call(compat, conf[compat])
      else
        hash[current] = conf[compat]
      end
    end
  end
  hash
end

#compat_parameters_extract(conf) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/fluent/plugin_helper/compat_parameters.rb', line 230

def compat_parameters_extract(conf)
  return unless conf.elements('extract').empty?
  return if EXTRACT_PARAMS.keys.all?{|k| !conf.has_key?(k) } && !conf.has_key?('format')

  # TODO: warn obsolete parameters if these are deprecated
  hash = compat_parameters_copy_to_subsection_attributes(conf, EXTRACT_PARAMS)

  if conf.has_key?('time_as_epoch') && Fluent::Config.bool_value(conf['time_as_epoch'])
    hash['time_key'] ||= 'time'
    hash['time_type'] = 'unixtime'
  elsif conf.has_key?('format') && conf["format"].start_with?("/") && conf["format"].end_with?("/") # old-style regexp parser
    hash['time_key'] ||= 'time'
    hash['time_type'] ||= 'string'
  end
  if conf.has_key?('localtime') || conf.has_key?('utc')
    if conf.has_key?('localtime') && conf.has_key?('utc')
      raise Fluent::ConfigError, "both of utc and localtime are specified, use only one of them"
    elsif conf.has_key?('localtime')
      hash['localtime'] = Fluent::Config.bool_value(conf['localtime'])
    elsif conf.has_key?('utc')
      hash['localtime'] = !(Fluent::Config.bool_value(conf['utc']))
      # Specifying "localtime false" means using UTC in TimeFormatter
      # And specifying "utc" is different from specifying "timezone +0000"(it's not always UTC).
      # There are difference between "Z" and "+0000" in timezone formatting.
      # TODO: add kwargs to TimeFormatter to specify "using localtime", "using UTC" or "using specified timezone" in more explicit way
    end
  end

  e = Fluent::Config::Element.new('extract', '', hash, [])
  conf.elements << e

  conf
end

#compat_parameters_formatter(conf) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/fluent/plugin_helper/compat_parameters.rb', line 310

def compat_parameters_formatter(conf)
  return unless conf.elements('format').empty?
  return if FORMATTER_PARAMS.keys.all?{|k| !conf.has_key?(k) }

  # TODO: warn obsolete parameters if these are deprecated
  hash = compat_parameters_copy_to_subsection_attributes(conf, FORMATTER_PARAMS)

  if conf.has_key?('time_as_epoch') && Fluent::Config.bool_value(conf['time_as_epoch'])
    hash['time_type'] = 'unixtime'
  end

  e = Fluent::Config::Element.new('format', '', hash, [])
  conf.elements << e

  conf
end

#compat_parameters_inject(conf) ⇒ Object



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
218
219
220
# File 'lib/fluent/plugin_helper/compat_parameters.rb', line 181

def compat_parameters_inject(conf)
  return unless conf.elements('inject').empty?
  return if INJECT_PARAMS.keys.all?{|k| !conf.has_key?(k) }

  # TODO: warn obsolete parameters if these are deprecated
  hash = compat_parameters_copy_to_subsection_attributes(conf, INJECT_PARAMS)

  if conf.has_key?('include_time_key') && Fluent::Config.bool_value(conf['include_time_key'])
    hash['time_key'] ||= 'time'
    hash['time_type'] ||= 'string'
  end
  if conf.has_key?('time_as_epoch') && Fluent::Config.bool_value(conf['time_as_epoch'])
    hash['time_key'] ||= 'time'
    hash['time_type'] = 'unixtime'
  end
  if conf.has_key?('localtime') || conf.has_key?('utc')
    utc = to_bool(conf['utc'])
    localtime = to_bool(conf['localtime'])
    if conf.has_key?('localtime') && conf.has_key?('utc') && !(localtime ^ utc)
      raise Fluent::ConfigError, "both of utc and localtime are specified, use only one of them"
    elsif conf.has_key?('localtime')
      hash['localtime'] = Fluent::Config.bool_value(conf['localtime'])
    elsif conf.has_key?('utc')
      hash['localtime'] = !(Fluent::Config.bool_value(conf['utc']))
      # Specifying "localtime false" means using UTC in TimeFormatter
      # And specifying "utc" is different from specifying "timezone +0000"(it's not always UTC).
      # There are difference between "Z" and "+0000" in timezone formatting.
      # TODO: add kwargs to TimeFormatter to specify "using localtime", "using UTC" or "using specified timezone" in more explicit way
    end
  end

  if conf.has_key?('include_tag_key') && Fluent::Config.bool_value(conf['include_tag_key'])
    hash['tag_key'] ||= 'tag'
  end

  e = Fluent::Config::Element.new('inject', '', hash, [])
  conf.elements << e

  conf
end

#compat_parameters_parser(conf) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/fluent/plugin_helper/compat_parameters.rb', line 264

def compat_parameters_parser(conf)
  return unless conf.elements('parse').empty?
  return if PARSER_PARAMS.keys.all?{|k| !conf.has_key?(k) }

  # TODO: warn obsolete parameters if these are deprecated
  hash = compat_parameters_copy_to_subsection_attributes(conf, PARSER_PARAMS)

  if conf["format"]
    if conf["format"].start_with?("/") && conf["format"].end_with?("/")
      hash["@type"] = "regexp"
      hash["expression"] = conf["format"][1..-2]
    else
      hash["@type"] = conf["format"]
    end
  end

  if conf["types"]
    delimiter = conf["types_delimiter"] || ','
    label_delimiter = conf["types_label_delimiter"] || ':'
    types = {}
    conf['types'].split(delimiter).each do |pair|
      key, value = pair.split(label_delimiter, 2)
      types[key] = value
    end
    hash["types"] = JSON.dump(types)
  end
  if conf.has_key?('localtime') || conf.has_key?('utc')
    if conf.has_key?('localtime') && conf.has_key?('utc')
      raise Fluent::ConfigError, "both of utc and localtime are specified, use only one of them"
    elsif conf.has_key?('localtime')
      hash['localtime'] = Fluent::Config.bool_value(conf['localtime'])
    elsif conf.has_key?('utc')
      hash['localtime'] = !(Fluent::Config.bool_value(conf['utc']))
      # Specifying "localtime false" means using UTC in TimeFormatter
      # And specifying "utc" is different from specifying "timezone +0000"(it's not always UTC).
      # There are difference between "Z" and "+0000" in timezone formatting.
      # TODO: add kwargs to TimeFormatter to specify "using localtime", "using UTC" or "using specified timezone" in more explicit way
    end
  end

  e = Fluent::Config::Element.new('parse', '', hash, [])
  conf.elements << e

  conf
end

#to_bool(v) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/fluent/plugin_helper/compat_parameters.rb', line 222

def to_bool(v)
  if  v.is_a?(FalseClass) || v == 'false' || v.nil?
    false
  else
    true
  end
end