summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/subscribers/rails_cache.rb
blob: 49e5f86e6e6e8be719fc286992419c908fe7d279 (plain)
1
2
3
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
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)
          increment(:cache_read_duration, event.duration)
        end

        def cache_write(event)
          increment(:cache_write_duration, event.duration)
        end

        def cache_delete(event)
          increment(:cache_delete_duration, event.duration)
        end

        def cache_exist?(event)
          increment(:cache_exists_duration, event.duration)
        end

        def increment(key, duration)
          return unless current_transaction

          current_transaction.increment(:cache_duration, duration)
          current_transaction.increment(key, duration)
        end

        private

        def current_transaction
          Transaction.current
        end
      end
    end
  end
end