Class: Fluent::Plugin::MultiConditionSelectorOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_multi_condition_selector.rb

Defined Under Namespace

Classes: RubyPlaceholderExpander

Constant Summary collapse

PATTERN_MAX_NUM =
20
BUILTIN_CONFIGURATIONS =
%W(@id @type @label type output_tag remove_keys renew_record keep_keys enable_ruby renew_time_key auto_typecast tag_else record_else tag)

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
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
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fluent/plugin/out_multi_condition_selector.rb', line 54

def configure(conf)
  super
  # ここで、BUILTIN_CONFIGURATIONS に入っていないものがあった場合はerrorをraise
  conf.each_pair { |k, v|
    next if BUILTIN_CONFIGURATIONS.include?(k) || k.match(/^condition\d\d?$/) || k.match(/^tag\d\d?$/)

    raise Fluent::ConfigError, 'out_multi_condition_selector: some weird config is set {'+k.to_s+':'+v.to_s+'}'
  }

  # conditionsを読み込む
  @conditions = []
  conf.elements.select { |element| element.name.match(/^condition?$/) }
      .each do |param|
    condition = {}
    data_record = {}

    param.elements.select { |element| element.name.match(/^data?$/) }.each do |data|
      data.each_pair do |key, value|
        data_record.merge!(key => convert_value(value))
      end
    end

    param.each_pair do |key, value|
      condition.merge!(key => convert_value(value))
    end

    condition.merge!("data" => data_record)
    @conditions.push(condition)
  end


  if @remove_keys
    @remove_keys = @remove_keys.split(',')
  end
  if @keep_keys
    raise Fluent::ConfigError, 'out_multi_condition_selector: `renew_record` must be true to use `keep_keys`' unless @renew_record
    @keep_keys = @keep_keys.split(',')
  end

  placeholder_expander_params = {
      :log           => log,
      :auto_typecast => @auto_typecast, # It should always be true
  }

  @placeholder_expander =
      if @enable_ruby
        # require utilities which would be used in ruby placeholders
        require 'pathname'
        require 'uri'
        require 'cgi'
        RubyPlaceholderExpander.new(placeholder_expander_params)
      else
        p 'WARN!! Hey! You should enable ruby!!!'
        PlaceholderExpander.new(placeholder_expander_params)
      end


  @hostname = Socket.gethostname
end

#process(tag, es) ⇒ Object



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
# File 'lib/fluent/plugin/out_multi_condition_selector.rb', line 114

def process(tag, es)
  tag_parts = tag.split('.')
  tag_prefix = tag_prefix(tag_parts)
  tag_suffix = tag_suffix(tag_parts)
  placeholder_values = {
      'tag'        => tag,
      'tags'       => tag_parts, # for old version compatibility
      'tag_parts'  => tag_parts,
      'tag_prefix' => tag_prefix,
      'tag_suffix' => tag_suffix,
      'hostname'   => @hostname,
  }
  es.each {|time, record|
    placeholder_values.merge!({
                                  'time'     => @placeholder_expander.time_value(time),
                                  'record'   => record,
                              })
                              
    # TODO: ここの処理よくないって evaluate
    matched_conditions, aditional_data = evaluate_condition(@conditions, placeholder_values )

    if (@at_least_one && matched_conditions.size > 0) || @at_least_one == false
      new_record = reform(record, aditional_data, placeholder_values)
      if @renew_time_key && new_record.has_key?(@renew_time_key)
        time = new_record[@renew_time_key].to_i
      end
      @remove_keys.each {|k| new_record.delete(k) } if @remove_keys
      new_record.merge!("conditions" => matched_conditions)
      router.emit(@tag, time, new_record)
    end
  }
rescue => e
  log.warn "out_multi_condition_selector: #{e.class} #{e.message} #{e.backtrace.first}"
end