Welcome to mirror list, hosted at ThFree Co, Russian Federation.

metrics.rb « cache « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 00d4e6e4d4e281c7503dbc6d6e93fff2e3ed676d (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
64
65
66
67
68
69
# frozen_string_literal: true

# Instrumentation for cache efficiency metrics
module Gitlab
  module Cache
    class Metrics
      DEFAULT_BUCKETS = [0, 1, 5].freeze

      def initialize(cache_metadata)
        @cache_metadata = cache_metadata
      end

      # Increase cache hit counter
      #
      def increment_cache_hit
        counter.increment(labels.merge(cache_hit: true))
      end

      # Increase cache miss counter
      #
      def increment_cache_miss
        counter.increment(labels.merge(cache_hit: false))
      end

      # Measure the duration of cacheable action
      #
      # @example
      #   observe_cache_generation do
      #     cacheable_action
      #   end
      #
      def observe_cache_generation(&block)
        real_start = Gitlab::Metrics::System.monotonic_time

        value = yield

        histogram.observe({}, Gitlab::Metrics::System.monotonic_time - real_start)

        value
      end

      private

      attr_reader :cache_metadata

      def counter
        @counter ||= Gitlab::Metrics.counter(:redis_hit_miss_operations_total, "Hit/miss Redis cache counter")
      end

      def histogram
        @histogram ||= Gitlab::Metrics.histogram(
          :redis_cache_generation_duration_seconds,
          'Duration of Redis cache generation',
          labels,
          DEFAULT_BUCKETS
        )
      end

      def labels
        @labels ||= {
          caller_id: cache_metadata.caller_id,
          cache_identifier: cache_metadata.cache_identifier,
          feature_category: cache_metadata.feature_category,
          backing_resource: cache_metadata.backing_resource
        }
      end
    end
  end
end