Class: Metrician::Memcache

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

Constant Summary collapse

CACHE_METRIC =
"cache.command".freeze
METHODS =
%i[get delete cas prepend append replace decrement increment add set].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reporter

all, inherited

Class Method Details

.dalli_gem?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/metrician/reporters/memcache.rb', line 11

def self.dalli_gem?
  !!defined?(::Dalli) && !!defined?(::Dalli::Client)
end

.enabled?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/metrician/reporters/memcache.rb', line 22

def self.enabled?
  (memcached_gem? || dalli_gem?) &&
    Metrician.configuration[:cache][:enabled]
end

.memcached_gem?Boolean

Returns:

  • (Boolean)


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

def self.memcached_gem?
  !!defined?(::Memcached)
end

Instance Method Details

#client_classesObject



15
16
17
18
19
20
# File 'lib/metrician/reporters/memcache.rb', line 15

def client_classes
  classes = []
  classes << Memcached if self.class.memcached_gem?
  classes << Dalli::Client if self.class.dalli_gem?
  classes
end

#instrumentObject



27
28
29
30
31
# File 'lib/metrician/reporters/memcache.rb', line 27

def instrument
  client_classes.each do |client_class|
    instrument_class(client_class)
  end
end

#instrument_class(client_class) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/metrician/reporters/memcache.rb', line 33

def instrument_class(client_class)
  return if client_class.method_defined?(:get_with_metrician_time)
  METHODS.each do |method_name|
    next unless client_class.method_defined?(method_name)
    client_class.class_eval <<-RUBY
      def #{method_name}_with_metrician_time(*args, &blk)
        start_time = Time.now
        begin
          #{method_name}_without_metrician_time(*args, &blk)
        ensure
          duration = (Time.now - start_time).to_f
          Metrician.gauge(::Metrician::Memcache::CACHE_METRIC, duration) if Metrician.configuration[:cache][:command][:enabled]
          Metrician.gauge("#{::Metrician::Memcache::CACHE_METRIC}.#{method_name}", duration) if Metrician.configuration[:cache][:command_specific][:enabled]
        end
      end
      alias #{method_name}_without_metrician_time #{method_name}
      alias #{method_name} #{method_name}_with_metrician_time
    RUBY
  end
end