Class: Fluent::RedisMultiTypeCounterOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::RedisMultiTypeCounterOutput
- Defined in:
- lib/fluent/plugin/out_redis_multi_type_counter.rb
Defined Under Namespace
Classes: Pattern, RecordKey, RecordValueFormatter, RedisMultiTypeCounterException
Instance Attribute Summary collapse
-
#db_number ⇒ Object
readonly
Returns the value of attribute db_number.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #format(tag, time, record) ⇒ Object
-
#initialize ⇒ RedisMultiTypeCounterOutput
constructor
A new instance of RedisMultiTypeCounterOutput.
- #shutdown ⇒ Object
- #start ⇒ Object
- #write(chunk) ⇒ Object
Constructor Details
#initialize ⇒ RedisMultiTypeCounterOutput
Returns a new instance of RedisMultiTypeCounterOutput.
8 9 10 11 12 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 8 def initialize super require 'redis' require 'msgpack' end |
Instance Attribute Details
#db_number ⇒ Object (readonly)
Returns the value of attribute db_number.
4 5 6 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 4 def db_number @db_number end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
4 5 6 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 4 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
4 5 6 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 4 def password @password end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
4 5 6 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 4 def patterns @patterns end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
4 5 6 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 4 def port @port end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
4 5 6 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 4 def redis @redis end |
Instance Method Details
#configure(conf) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 14 def configure(conf) super @host = conf.has_key?('host') ? conf['host'] : 'localhost' @port = conf.has_key?('port') ? conf['port'].to_i : 6379 @password = conf.has_key?('password') ? conf['password'] : nil @db_number = conf.has_key?('db_number') ? conf['db_number'].to_i : nil @patterns = [] conf.elements.select { |element| element.name == 'pattern' }.each { |element| begin @patterns << Pattern.new(element) rescue RedisMultiTypeCounterException => e raise Fluent::ConfigError, e. end } end |
#format(tag, time, record) ⇒ Object
45 46 47 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 45 def format(tag, time, record) [tag, time, record].to_msgpack end |
#shutdown ⇒ Object
41 42 43 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 41 def shutdown @redis.quit end |
#start ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 32 def start super @redis = Redis.new( :host => @host, :port => @port, :password => @password, :thread_safe => true, :db => @db_number ) end |
#write(chunk) ⇒ Object
49 50 51 52 53 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 |
# File 'lib/fluent/plugin/out_redis_multi_type_counter.rb', line 49 def write(chunk) table = {} table.default = 0 chunk.open { |io| begin MessagePack::Unpacker.new(io).each { || (tag, time, record) = @patterns.select { |pattern| pattern.is_match?(record) }.each{ |pattern| count_key = pattern.get_count_key(time, record) count_hash_key = pattern.get_count_hash_key(record) count_zset_key = pattern.get_count_zset_key(record) store_list = pattern.store_list key = RecordKey.new(count_key, count_hash_key, count_zset_key, store_list) if store_list if table[key] == 0 table[key] = [] end table[key] << pattern.get_count_value(record) else table[key] += pattern.get_count_value(record) end } } rescue EOFError # EOFError always occured when reached end of chunk. end } table.each_pair.select { |key, value| value != 0 }.each_slice(@max_pipelining) { |items| @redis.pipelined do items.each do |key, value| if key.count_hash_key != nil @redis.hincrby(key.count_key, key.count_hash_key, value) elsif key.count_zset_key != nil @redis.zincrby(key.count_key, value, key.count_zset_key) else if key.store_list @redis.rpush(key.count_key, value) else @redis.incrby(key.count_key, value) end end end end } end |