Class: Fluent::Plugin::ConditionCheckerOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_condition_checker.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)

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



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

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

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

  #tagを読み込み
  tags = []
  (1..PATTERN_MAX_NUM).each do |i|
    next unless conf["tag#{i}"]
    tags.push(conf["tag#{i}"]) # tags[i+1] で、欲しいtagにアクセスできる
  end

  #conditionを読み込み
  @conditions = []
    (1..PATTERN_MAX_NUM).each do |i|
      next unless conf["condition#{i}"]
      @conditions.push(conf["condition#{i}"])
  end
  if tags.size != @conditions.size
    raise Fluent::ConfigError, 'match the numbers of tags and conditions; number of tags: '+tags.size.to_s+', number of conditions: '+@conditions.size.to_s
  end


  # maps[i] で、欲しいconditionに対応するrecordにアクセスすることができる。
  # FIXME: conf.elements.forEachでいい感じに回せそう
  maps = []
  (1..PATTERN_MAX_NUM).each do |i|
    next unless conf["condition#{i}"] # 対応するcondition{i}が定義されているものだけ読み込む
    conf.elements.select { |element| element.name == 'record'+i.to_s }.each { |element|
      recordTmp = {}
      element.each_pair { |k, v|
        element.has_key?(k) # to suppress unread configuration warning
        recordTmp.merge!({k => parse_value(v)})
        # map_if_false[k] = parse_value(v)
      }
      maps[i] = recordTmp
    }
  end

  map_else ={}
  conf.elements.select { |element| element.name == 'record_else' }.each { |element|
    recordTmp = {}
    element.each_pair { |k, v|
    element.has_key?(k) # to suppress unread configuration warning
      recordTmp.merge!({k => parse_value(v)})
    }
    map_else = recordTmp
  }

  if @remove_keys
    @remove_keys = @remove_keys.split(',')
  end
  if @keep_keys
    raise Fluent::ConfigError, 'out_condition_checker: `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


  @maps = @placeholder_expander.preprocess_map(maps)
  @tags = @placeholder_expander.preprocess_map(tags)
  @tag_else = @placeholder_expander.preprocess_map(conf['tag_else'])
  @map_else = @placeholder_expander.preprocess_map(map_else)

  @hostname = Socket.gethostname
end

#process(tag, es) ⇒ Object



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
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fluent/plugin/out_condition_checker.rb', line 150

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
    result, idx = evaluate_condition(@conditions, placeholder_values)
    placeholder_values.merge!({ 'result'   => result })

    if idx
      new_tag, new_record = reform(@tags[idx], @maps[idx+1], record, placeholder_values)
    else
      # TODO: tag_elseは使えなくするoption作る"
      new_tag, new_record = reform(@tag_else, @map_else, record, placeholder_values)
      # return
    end

    if new_tag
      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
      router.emit(new_tag, time, new_record)
    end
  }
rescue => e
  log.warn "condition_checker: #{e.class} #{e.message} #{e.backtrace.first}"
end