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

redis_metric.rb « instrumentations « metrics « usage « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 26d963e240772b186d694d61a36bf3a9676ce1c0 (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
# frozen_string_literal: true

module Gitlab
  module Usage
    module Metrics
      module Instrumentations
        # Usage example
        #
        # In metric YAML definition:
        #
        # instrumentation_class: RedisMetric
        # options:
        #   event: pushes
        #   prefix: source_code
        #
        class RedisMetric < BaseMetric
          include Gitlab::UsageDataCounters::RedisCounter

          USAGE_PREFIX = "USAGE_"

          def initialize(time_frame:, options: {})
            super

            raise ArgumentError, "'event' option is required" unless metric_event.present?
            raise ArgumentError, "'prefix' option is required" unless prefix.present?
          end

          def metric_event
            options[:event]
          end

          def prefix
            options[:prefix]
          end

          def include_usage_prefix?
            options.fetch(:include_usage_prefix, true)
          end

          def value
            redis_usage_data do
              total_count(redis_key)
            end
          end

          def suggested_name
            Gitlab::Usage::Metrics::NameSuggestion.for(:redis)
          end

          private

          def redis_key
            key = "#{prefix}_#{metric_event}".upcase
            key.prepend(USAGE_PREFIX) if include_usage_prefix?
            key
          end
        end
      end
    end
  end
end