Class: Metrician::Redis

Inherits:
Reporter show all
Defined in:
lib/metrician/reporters/redis.rb

Constant Summary collapse

CACHE_METRIC =
"cache.command".freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reporter

all, inherited

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/metrician/reporters/redis.rb', line 6

def self.enabled?
  !!defined?(::Redis) &&
    Metrician.configuration[:cache][:enabled]
end

Instance Method Details

#instrumentObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/metrician/reporters/redis.rb', line 11

def instrument
  return if ::Redis::Client.method_defined?(:call_with_metrician_time)
  ::Redis::Client.class_eval do
    def call_with_metrician_time(*args, &blk)
      start_time = Time.now
      begin
        call_without_metrician_time(*args, &blk)
      ensure
        duration = (Time.now - start_time).to_f
        Metrician.gauge(CACHE_METRIC, duration) if Metrician.configuration[:cache][:command][:enabled]
        if Metrician.configuration[:cache][:command_specific][:enabled]
          method_name = args[0].is_a?(Array) ? args[0][0] : args[0]
          Metrician.gauge("#{CACHE_METRIC}.#{method_name}", duration)
        end
      end
    end
    alias_method :call_without_metrician_time, :call
    alias_method :call, :call_with_metrician_time
  end
end