class Fluent::Redis_SlowlogInput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_redis_slowlog.rb, line 11
def initialize
  super
  require 'redis'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_redis_slowlog.rb, line 16
def configure(conf)
  super
  @log_id = 0
  @get_interval = @interval
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_redis_slowlog.rb, line 38
def shutdown
  super
  @redis.quit
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_redis_slowlog.rb, line 22
def start
  super
  @redis = Redis.new(
    :host => @host, 
    :port => @port,
    :thread_safe => true
  )
  pong = @redis.ping
  begin
      unless pong == 'PONG'
          raise "fluent-plugin-redis-slowlog: cannot connect redis"
      end
  end
  @watcher = Thread.new(&method(:watch))
end

Private Instance Methods

output( last_id = 0) click to toggle source
# File lib/fluent/plugin/in_redis_slowlog.rb, line 51
def output( last_id = 0) 
  slow_logs = []
  slow_logs = @redis.slowlog('get', logsize)

  log_id = slow_logs[0][0]
  slow_logs.reverse.each do |log|
    unless log[0] > last_id
      next
    end
    log_hash = { id: log[0], timestamp: Time.at(log[1]), exec_time: log[2], command: log[3] }
    Fluent::Engine.emit(tag, Time.now.to_i, log_hash)
  end
  return log_id
end
watch() click to toggle source
# File lib/fluent/plugin/in_redis_slowlog.rb, line 44
def watch
  while true
    sleep @get_interval
    @log_id = output( @log_id )
  end
end