class Fluent::Plugin::ScenarioManagerOutput

fluentd output plugin

Constants

BUILTIN_CONFIGURATIONS
DEFAULT_STORAGE_TYPE
PATTERN_MAX_NUM

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_scenario_manager.rb, line 64
def configure(conf)
  super
  # シナリオパラメーターを取得
  @scenarios = []
  conf.elements.select { |element| element.name.match(/^scenario\d\d?$/) }
      .each do |param|
    scenario = {}
    param.each_pair do |key, value|
      scenario.merge!(key => convert_value(value))
    end
    @scenarios.push(scenario)
  end

  # えらーならraiseする
  valid_conf?(conf)

  return unless @scenario_manage_mode

  # シナリオルールの取得
  @rules = []
  @executes = []
  rule, execute = separate_rule_and_exec(conf['if'])
  @rules.push(rule)
  @executes.push(execute)
  (1..PATTERN_MAX_NUM).each do |i|
    next unless conf["elsif#{i}"]

    rule, execute = separate_rule_and_exec(conf["elsif#{i}"])
    @rules.push(rule)
    @executes.push(execute)
  end
end
process(tag, es) click to toggle source
# File lib/fluent/plugin/out_scenario_manager.rb, line 101
def process(tag, es)
  es.each do |time, record|
    # output events to ...
    unless @scenario_manage_mode
      @@executing_scenario = record['label']
      # TODO: actionタグを自由に命名できるようにする
      router.emit("serialized_action", time, record)
      break
    end

    # scenario check
    execute_idx = scenario_detector(record)

    next if execute_idx.nil?

    # execute scenario
    # マッチしたシナリオを実行する(emitする)
    router.emit(@tag || 'detected_scenario', time, generate_record_for_emit(get_scenario(@executes[execute_idx]), record))
  end
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_scenario_manager.rb, line 97
def start
  super
end

Private Instance Methods

convert_value(value) click to toggle source
# File lib/fluent/plugin/out_scenario_manager.rb, line 170
def convert_value(value)
  # Booleanがチェック
  return true if value == 'true'

  return false if value == 'false'

  # 数値データなら数値で返す
  return value.to_i if value.to_i.to_s == value.to_s
  return value.to_f if value.to_f.to_s == value.to_s

  value
end
executing_scenario() click to toggle source
# File lib/fluent/plugin/out_scenario_manager.rb, line 151
def executing_scenario
  @@executing_scenario
end
generate_record_for_emit(value, record) click to toggle source

value は上記のconvert_Valueを使用している前提.

# File lib/fluent/plugin/out_scenario_manager.rb, line 184
def generate_record_for_emit(value, record)
  return value.map{ |k, v| [k,  v.is_a?(String) && v.start_with?('${') && v.end_with?('}') ? instance_eval(v[2..-2]) : v] }.to_h
end
get_scenario(execute) click to toggle source
# File lib/fluent/plugin/out_scenario_manager.rb, line 162
def get_scenario(execute)
  execute_scenario_label = /(execute_scenario )(.+*)/.match(execute)[2]
  @scenarios.each_with_index do |scenario, _idx|
    return scenario if scenario['label'] == execute_scenario_label
  end
  return nil
end
scenario_detector(record) click to toggle source

ruleを調べて、マッチしたらそのindexを返す。 すべてマッチしなかったらnilを返す

# File lib/fluent/plugin/out_scenario_manager.rb, line 144
def scenario_detector(record) # rubocop:disable all
  @rules.each_with_index do |rule, idx|
    return idx if instance_eval(rule)
  end
  nil
end
separate_rule_and_exec(rule) click to toggle source
# File lib/fluent/plugin/out_scenario_manager.rb, line 155
def separate_rule_and_exec(rule)
  separated_str = /(.+*)( then )(.+*)/.match(rule)
  [separated_str[1], separated_str[3]]
rescue StandardError
  raise Fluent::ConfigError, 'out_scenario_manager: scenario rule should contain ~ then ~ .'
end
valid_conf?(conf) click to toggle source
# File lib/fluent/plugin/out_scenario_manager.rb, line 125
def valid_conf?(conf)
  # manage_modeじゃなかったら何もチェックしない
  return true unless @scenario_manage_mode

  # ここで、BUILTIN_CONFIGURATIONS に入っていないものがあった場合はerrorをraise
  elsif_cnt = 0
  conf.each_pair do |k, v|
    elsif_cnt += 1 if k.match(/^elsif\d\d?$/)
    next if BUILTIN_CONFIGURATIONS.include?(k) || k.match(/^elsif\d\d?$/)

    raise(Fluent::ConfigError, 'out_scenario_manager: some weird config is set {' + k.to_s + ':' + v.to_s + '}')
  end

  raise Fluent::ConfigError, 'out_scenario_manager: "if" directive is required' if @if.nil?
  raise Fluent::ConfigError, 'out_scenario_manager: "scenario" define is ruquired at least 1' if @scenarios.size <= 0
end