summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/subscribers/rails_cache.rb
blob: aaed2184f44ae448b4e1bab86ba37f9e878b759e (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Gitlab
  module Metrics
    module Subscribers
      # Class for tracking the total time spent in Rails cache calls
      # http://guides.rubyonrails.org/active_support_instrumentation.html
      class RailsCache < ActiveSupport::Subscriber
        attach_to :active_support

        def cache_read(event)
          increment(:cache_read, event.duration)

          return unless current_transaction
          return if event.payload[:super_operation] == :fetch

          if event.payload[:hit]
            current_transaction.increment(:cache_read_hit_count, 1)
          else
            current_transaction.increment(:cache_read_miss_count, 1)
          end
        end

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

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

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

        def cache_fetch_hit(event)
          return unless current_transaction

          current_transaction.increment(:cache_read_hit_count, 1)
        end

        def cache_generate(event)
          return unless current_transaction

          current_transaction.increment(:cache_read_miss_count, 1)
        end

        def increment(key, duration)
          return unless current_transaction

          current_transaction.increment(:cache_duration, duration)
          current_transaction.increment(:cache_count, 1)
          current_transaction.increment("#{key}_duration".to_sym, duration)
          current_transaction.increment("#{key}_count".to_sym, 1)
        end

        private

        def current_transaction
          Transaction.current
        end
      end
    end
  end
end