gitlab-ce/lib/gitlab/internal_events/event_definitions.rb

61 lines
1.7 KiB
Ruby

# frozen_string_literal: true
module Gitlab
module InternalEvents
module EventDefinitions
InvalidMetricConfiguration = Class.new(StandardError)
class << self
def load_configurations
@events = load_metric_definitions
nil
end
def unique_property(event_name)
events[event_name] || raise(InvalidMetricConfiguration, "Unique property not defined for #{event_name}")
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