Class: Fluent::RedisCounterOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::RedisCounterOutput
- Defined in:
- lib/fluent/plugin/out_redis_counter.rb
Defined Under Namespace
Classes: Pattern, RecordValueFormatter, RedisCounterException
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 ⇒ RedisCounterOutput
constructor
A new instance of RedisCounterOutput.
- #shutdown ⇒ Object
- #start ⇒ Object
- #write(chunk) ⇒ Object
Constructor Details
#initialize ⇒ RedisCounterOutput
Returns a new instance of RedisCounterOutput.
8 9 10 11 12 |
# File 'lib/fluent/plugin/out_redis_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_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_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_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_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_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_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_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 RedisCounterException => e raise Fluent::ConfigError, e. end } end |
#format(tag, time, record) ⇒ Object
45 46 47 |
# File 'lib/fluent/plugin/out_redis_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_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_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 |
# File 'lib/fluent/plugin/out_redis_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| table[pattern.get_count_key(time, record)] += pattern.get_count_value(record) } } 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| @redis.incrby(key, value) end end } end |