diff options
Diffstat (limited to 'lib/gitlab/tracking/service_ping_context.rb')
-rw-r--r-- | lib/gitlab/tracking/service_ping_context.rb | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/lib/gitlab/tracking/service_ping_context.rb b/lib/gitlab/tracking/service_ping_context.rb index 393cd647e7f..d31ca69a10c 100644 --- a/lib/gitlab/tracking/service_ping_context.rb +++ b/lib/gitlab/tracking/service_ping_context.rb @@ -4,21 +4,51 @@ module Gitlab module Tracking class ServicePingContext SCHEMA_URL = 'iglu:com.gitlab/gitlab_service_ping/jsonschema/1-0-0' - ALLOWED_SOURCES = %i[redis_hll].freeze + REDISHLL_SOURCE = :redis_hll + REDIS_SOURCE = :redis - def initialize(data_source:, event:) + ALLOWED_SOURCES = [REDISHLL_SOURCE, REDIS_SOURCE].freeze + + def initialize(data_source:, event: nil, key_path: nil) + check_configuration(data_source, event, key_path) + + @payload = { data_source: data_source } + + payload[:event_name] = event if data_source.eql? REDISHLL_SOURCE + payload[:key_path] = key_path if data_source.eql? REDIS_SOURCE + end + + def to_context + SnowplowTracker::SelfDescribingJson.new(SCHEMA_URL, payload) + end + + def to_h + { + schema: SCHEMA_URL, + data: @payload + } + end + + private + + attr_reader :payload + + def check_configuration(data_source, event, key_path) unless ALLOWED_SOURCES.include?(data_source) - raise ArgumentError, "#{data_source} is not acceptable data source for ServicePingContext" + configuration_error("#{data_source} is not acceptable data source for ServicePingContext") end - @payload = { - data_source: data_source, - event_name: event - } + if REDISHLL_SOURCE.eql?(data_source) && event.nil? + configuration_error("event attribute can not be missing for #{REDISHLL_SOURCE} data source") + end + + return unless REDIS_SOURCE.eql?(data_source) && key_path.nil? + + configuration_error("key_path attribute can not be missing for #{REDIS_SOURCE} data source") end - def to_context - SnowplowTracker::SelfDescribingJson.new(SCHEMA_URL, @payload) + def configuration_error(message) + Gitlab::ErrorTracking.track_and_raise_for_dev_exception(ArgumentError.new(message)) end end end |