2022-11-25 23:54:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Tracking
|
|
|
|
class ServicePingContext
|
|
|
|
SCHEMA_URL = 'iglu:com.gitlab/gitlab_service_ping/jsonschema/1-0-0'
|
2023-03-04 22:38:38 +05:30
|
|
|
REDISHLL_SOURCE = :redis_hll
|
|
|
|
REDIS_SOURCE = :redis
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
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)
|
2022-11-25 23:54:43 +05:30
|
|
|
unless ALLOWED_SOURCES.include?(data_source)
|
2023-03-04 22:38:38 +05:30
|
|
|
configuration_error("#{data_source} is not acceptable data source for ServicePingContext")
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
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")
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def configuration_error(message)
|
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(ArgumentError.new(message))
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|