Class: Fluent::RedisCounterOutput

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

Defined Under Namespace

Classes: Pattern, RecordValueFormatter, RedisCounterException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRedisCounterOutput

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_numberObject (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

#hostObject (readonly)

Returns the value of attribute host.



4
5
6
# File 'lib/fluent/plugin/out_redis_counter.rb', line 4

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



4
5
6
# File 'lib/fluent/plugin/out_redis_counter.rb', line 4

def password
  @password
end

#patternsObject (readonly)

Returns the value of attribute patterns.



4
5
6
# File 'lib/fluent/plugin/out_redis_counter.rb', line 4

def patterns
  @patterns
end

#portObject (readonly)

Returns the value of attribute port.



4
5
6
# File 'lib/fluent/plugin/out_redis_counter.rb', line 4

def port
  @port
end

#redisObject (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.message
    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

#shutdownObject



41
42
43
# File 'lib/fluent/plugin/out_redis_counter.rb', line 41

def shutdown
  @redis.quit
end

#startObject



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 { |message|
        (tag, time, record) = message
        @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