rails_cache.rb 1.04 KB
Newer Older
1 2 3 4 5 6 7 8
module Gitlab
  module Metrics
    module Subscribers
      # Class for tracking the total time spent in Rails cache calls
      class RailsCache < ActiveSupport::Subscriber
        attach_to :active_support

        def cache_read(event)
9
          increment(:cache_read, event.duration)
10 11 12
        end

        def cache_write(event)
13
          increment(:cache_write, event.duration)
14 15 16
        end

        def cache_delete(event)
17
          increment(:cache_delete, event.duration)
18 19 20
        end

        def cache_exist?(event)
21
          increment(:cache_exists, event.duration)
22 23 24 25 26 27
        end

        def increment(key, duration)
          return unless current_transaction

          current_transaction.increment(:cache_duration, duration)
28 29 30
          current_transaction.increment(:cache_count, 1)
          current_transaction.increment("#{key}_duration".to_sym, duration)
          current_transaction.increment("#{key}_count".to_sym, 1)
31 32 33 34 35 36 37 38 39 40 41
        end

        private

        def current_transaction
          Transaction.current
        end
      end
    end
  end
end