Class: Fluent::RedisMultiTypeCounterOutput

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

Defined Under Namespace

Classes: Pattern, RecordKey, RecordValueFormatter, RedisMultiTypeCounterException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRedisMultiTypeCounterOutput

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

#hostObject (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

#passwordObject (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

#patternsObject (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

#portObject (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

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

#shutdownObject



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

def shutdown
  @redis.quit
end

#startObject



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