Class: Fluent::RedisMultiTypeCounterOutput::Pattern

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf_element) ⇒ Pattern

Returns a new instance of Pattern.



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
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
221
222
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 152

def initialize(conf_element)
  if !conf_element.has_key?('count_key') && !conf_element.has_key?('count_key_format')
    raise RedisMultiTypeCounterException, '"count_key" or "count_key_format" is required.'
  end
  if conf_element.has_key?('count_key') && conf_element.has_key?('count_key_format')
    raise RedisMultiTypeCounterException, 'both "count_key" and "count_key_format" are specified.'
  end

  if conf_element.has_key?('count_key')
    @count_key = conf_element['count_key']
  else
    if conf_element.has_key?('localtime') && conf_element.has_key?('utc')
      raise RedisMultiTypeCounterException, 'both "localtime" and "utc" are specified.'
    end
    is_localtime = true
    if conf_element.has_key?('utc')
      is_localtime = false
    end
    @count_key_format = [conf_element['count_key_format'], is_localtime]
    @record_formatter_for_count_key = RecordValueFormatter.new(@count_key_format[0])
  end

  @store_list = false
  if conf_element.has_key?('store_list') && conf_element['store_list'].downcase == 'true'
    @store_list = true
  end

  if @store_list && (conf_element.has_key?('count_hash_key_format') ||
      conf_element.has_key?('count_zset_key_format'))
    raise RedisMultiTypeCounterException, 'store_list is true, it should be normal type, not hash or zset'
  end

  if conf_element.has_key?('count_hash_key_format') && conf_element.has_key?('count_zset_key_format')
    raise RedisMultiTypeCounterException, 'both "count_hash_key_format" "count_zset_key_format" are specified.'
  end

  if conf_element.has_key?('count_hash_key_format')
    @count_hash_key_format = conf_element['count_hash_key_format']
    @record_formatter_for_count_hash_key = RecordValueFormatter.new(@count_hash_key_format)
  else
    @count_hash_key_format = nil
  end

  if conf_element.has_key?('count_zset_key_format')
    @count_zset_key_format = conf_element['count_zset_key_format']
    @record_formatter_for_count_zset_key = RecordValueFormatter.new(@count_zset_key_format)
  else
    @count_zset_key_format = nil
  end

  if conf_element.has_key?('count_value_key')
    @count_value_key = conf_element['count_value_key']
  else
    @count_value = 1
    if conf_element.has_key?('count_value')
      begin
        @count_value = Integer(conf_element['count_value'])
      rescue
        raise RedisMultiTypeCounterException, 'invalid "count_value", integer required.'
      end
    end
  end

  @matches = {}
  conf_element.each_pair.select { |key, value|
    key =~ /^match_/
  }.each { |key, value|
    name = key['match_'.size .. key.size]
    @matches[name] = Regexp.new(value)
  }
end

Instance Attribute Details

#count_valueObject (readonly)

Returns the value of attribute count_value.



150
151
152
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 150

def count_value
  @count_value
end

#count_value_keyObject (readonly)

Returns the value of attribute count_value_key.



150
151
152
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 150

def count_value_key
  @count_value_key
end

#matchesObject (readonly)

Returns the value of attribute matches.



150
151
152
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 150

def matches
  @matches
end

#store_listObject (readonly)

Returns the value of attribute store_list.



150
151
152
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 150

def store_list
  @store_list
end

Instance Method Details

#get_count_hash_key(record) ⇒ Object



243
244
245
246
247
248
249
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 243

def get_count_hash_key(record)
  if @count_hash_key_format == nil
    return nil
  else
    return @record_formatter_for_count_hash_key.key(record)
  end
end

#get_count_key(time, record) ⇒ Object



233
234
235
236
237
238
239
240
241
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 233

def get_count_key(time, record)
  if @count_key_format == nil
    @count_key
  else
    count_key = @record_formatter_for_count_key.key(record)
    formatter = TimeFormatter.new(count_key, @count_key_format[1])
    formatter.format(time)
  end
end

#get_count_value(record) ⇒ Object



259
260
261
262
263
264
265
266
267
268
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 259

def get_count_value(record)
  if @count_value_key
    ret = record[@count_value_key] || 0
    return ret.kind_of?(Integer) ? ret : 0
  else
    if @count_value
      return @count_value
    end
  end
end

#get_count_zset_key(record) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 251

def get_count_zset_key(record)
  if @count_zset_key_format == nil
    return nil
  else
    return @record_formatter_for_count_zset_key.key(record)
  end
end

#is_match?(record) ⇒ Boolean

Returns:



224
225
226
227
228
229
230
231
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 224

def is_match?(record)
  @matches.each_pair{ |key, value|
    if !record.has_key?(key) || !(record[key] =~ value)
      return false
    end
  }
  return true
end