summaryrefslogtreecommitdiff
path: root/lib/gitlab/usage/metrics/instrumentations/redis_hll_metric.rb
blob: 17009f7638ec7913ec32f365e1c750aaa4e98686 (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
# frozen_string_literal: true

module Gitlab
  module Usage
    module Metrics
      module Instrumentations
        class RedisHLLMetric < BaseMetric
          # Usage example
          #
          # In metric YAML defintion
          # instrumentation_class: RedisHLLMetric
          #   events:
          #     - g_analytics_valuestream
          # end
          def initialize(metric_definition)
            super

            raise ArgumentError, "options events are required" unless metric_events.present?
          end

          def metric_events
            options[:events]
          end

          def value
            redis_usage_data do
              event_params = time_constraints.merge(event_names: metric_events)

              Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(**event_params)
            end
          end

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

          private

          def time_constraints
            case time_frame
            when '28d'
              monthly_time_range
            when '7d'
              weekly_time_range
            else
              raise "Unknown time frame: #{time_frame} for RedisHLLMetric"
            end
          end
        end
      end
    end
  end
end