Module: Strumbar::Instrumentation::Redis

Defined in:
lib/strumbar/instrumentation/redis.rb

Class Method Summary collapse

Class Method Details

.load(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/strumbar/instrumentation/redis.rb', line 4

def self.load(options={})
  options[:rate] ||= Strumbar.default_rate

  Strumbar.subscribe 'query.redis' do |client, event|
    client.increment 'query.redis', options[:rate]
    client.increment('failure.redis', options[:rate]) if event.payload[:failure]

    command = case event.payload[:command]
      when NilClass then nil
      when String then event.payload[:command]
      else event.payload[:command].join('_')
    end

    command.gsub!(/:/, '_') unless command.nil?

    client.timing "#{command}.redis", event.duration, options[:rate]
  end

  unless ::Redis::Client.instance_methods.include? :call_with_instrumentation
    ::Redis::Client.class_eval do
      def call_with_instrumentation command, &block
        Strumbar.strum 'query.redis', command: command do |payload|
          call_without_instrumentation command, &block
          begin
            reply = call_without_instrumentation command, &block 
            payload[:failure] = false
          rescue CommandError
            payload[:failure] = true
            raise
          end

          reply
        end
      end

      alias_method :call_without_instrumentation, :call
      alias_method :call, :call_with_instrumentation
    end
  end
end