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

event_definitions.rb « internal_events « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f3c8092bcb0f4aa096d1483f6761df7c70461155 (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
70
71
# frozen_string_literal: true

module Gitlab
  module InternalEvents
    module EventDefinitions
      InvalidMetricConfiguration = Class.new(StandardError)

      class << self
        VALID_UNIQUE_VALUES = %w[user.id project.id namespace.id].freeze

        def load_configurations
          @events = load_metric_definitions
          nil
        end

        def unique_property(event_name)
          unique_value = events[event_name]&.to_s

          raise(InvalidMetricConfiguration, "Unique property not defined for #{event_name}") unless unique_value

          unless VALID_UNIQUE_VALUES.include?(unique_value)
            raise(InvalidMetricConfiguration, "Invalid unique value '#{unique_value}' for #{event_name}")
          end

          unique_value.split('.').first.to_sym
        end

        def known_event?(event_name)
          events.key?(event_name)
        end

        private

        def events
          load_configurations if @events.nil? || Gitlab::Usage::MetricDefinition.metric_definitions_changed?

          @events
        end

        def load_metric_definitions
          all_events = {}

          Gitlab::Usage::MetricDefinition.all.each do |metric_definition|
            next unless metric_definition.available?

            process_events(all_events, metric_definition.events)
          rescue StandardError => e
            Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
          end

          all_events
        end

        def process_events(all_events, metric_events)
          metric_events.each do |event_name, event_unique_attribute|
            unless all_events[event_name]
              all_events[event_name] = event_unique_attribute
              next
            end

            next if event_unique_attribute.nil? || event_unique_attribute == all_events[event_name]

            raise InvalidMetricConfiguration,
              "The same event cannot have several unique properties defined. " \
              "Event: #{event_name}, unique values: #{event_unique_attribute}, #{all_events[event_name]}"
          end
        end
      end
    end
  end
end