debian-mirror-gitlab/app/services/service_ping/submit_service.rb

129 lines
3.8 KiB
Ruby
Raw Normal View History

2021-09-30 23:02:18 +05:30
# frozen_string_literal: true
module ServicePing
class SubmitService
2021-11-11 11:23:49 +05:30
PRODUCTION_BASE_URL = 'https://version.gitlab.com'
STAGING_BASE_URL = 'https://gitlab-services-version-gitlab-com-staging.gs-staging.gitlab.org'
USAGE_DATA_PATH = 'usage_data'
2022-04-04 11:22:00 +05:30
ERROR_PATH = 'usage_ping_errors'
2022-07-16 23:28:13 +05:30
METADATA_PATH = 'usage_ping_metadata'
2021-09-30 23:02:18 +05:30
SubmissionError = Class.new(StandardError)
2022-07-23 23:45:48 +05:30
def initialize(skip_db_write: false, payload: nil)
2022-01-26 12:08:38 +05:30
@skip_db_write = skip_db_write
2022-07-23 23:45:48 +05:30
@payload = payload
2022-01-26 12:08:38 +05:30
end
2021-09-30 23:02:18 +05:30
def execute
2021-10-27 15:23:28 +05:30
return unless ServicePing::ServicePingSettings.product_intelligence_enabled?
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
start_time = Time.current
2022-04-04 11:22:00 +05:30
2022-10-11 01:57:18 +05:30
begin
response = submit_usage_data_payload
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
raise SubmissionError, "Unsuccessful response code: #{response.code}" unless response.success?
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
handle_response(response)
submit_metadata_payload
rescue StandardError => e
submit_error_payload(e, start_time)
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
raise
2022-01-26 12:08:38 +05:30
end
2022-04-04 11:22:00 +05:30
end
2021-09-30 23:02:18 +05:30
private
2022-07-23 23:45:48 +05:30
attr_reader :payload, :skip_db_write
2022-08-13 15:12:31 +05:30
def metadata(service_ping_payload)
{
metadata: {
uuid: service_ping_payload[:uuid],
metrics: metrics_collection_time(service_ping_payload)
}
}
end
2022-07-16 23:28:13 +05:30
def metrics_collection_time(payload, parents = [])
return [] unless payload.is_a?(Hash)
payload.flat_map do |key, metric_value|
key_path = parents.dup.append(key)
if metric_value.respond_to?(:duration)
{ name: key_path.join('.'), time_elapsed: metric_value.duration }
else
metrics_collection_time(metric_value, key_path)
end
end
end
def submit_payload(payload, path: USAGE_DATA_PATH)
2021-09-30 23:02:18 +05:30
Gitlab::HTTP.post(
2022-07-16 23:28:13 +05:30
URI.join(base_url, path),
2023-01-13 00:05:48 +05:30
body: Gitlab::Json.dump(payload),
2021-09-30 23:02:18 +05:30
allow_local_requests: true,
headers: { 'Content-type' => 'application/json' }
)
end
2022-10-11 01:57:18 +05:30
def submit_usage_data_payload
raise SubmissionError, 'Usage data payload is blank' if payload.blank?
submit_payload(payload)
end
def handle_response(response)
version_usage_data_id =
response.dig('conv_index', 'usage_data_id') || response.dig('dev_ops_score', 'usage_data_id')
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
unless version_usage_data_id.is_a?(Integer) && version_usage_data_id > 0
raise SubmissionError, "Invalid usage_data_id in response: #{version_usage_data_id}"
end
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
return if skip_db_write
raw_usage_data = save_raw_usage_data(payload)
raw_usage_data.update_version_metadata!(usage_data_id: version_usage_data_id)
ServicePing::DevopsReport.new(response).execute
end
def submit_error_payload(error, start_time)
current_time = Time.current
error_payload = {
time: current_time,
uuid: Gitlab::CurrentSettings.uuid,
hostname: Gitlab.config.gitlab.host,
version: Gitlab.version_info.to_s,
message: "#{error.message.presence || error.class} at #{error.backtrace[0]}",
elapsed: (current_time - start_time).round(1)
}
submit_payload({ error: error_payload }, path: ERROR_PATH)
end
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
def submit_metadata_payload
submit_payload(metadata(payload), path: METADATA_PATH)
2021-09-30 23:02:18 +05:30
end
def save_raw_usage_data(usage_data)
2022-07-16 23:28:13 +05:30
# safe_find_or_create_by! was originally called here.
# We merely switched to `find_or_create_by!`
# rubocop: disable CodeReuse/ActiveRecord
RawUsageData.find_or_create_by(recorded_at: usage_data[:recorded_at]) do |record|
2021-09-30 23:02:18 +05:30
record.payload = usage_data
end
2022-07-16 23:28:13 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2021-09-30 23:02:18 +05:30
end
# See https://gitlab.com/gitlab-org/gitlab/-/issues/233615 for details
2021-11-11 11:23:49 +05:30
def base_url
Rails.env.production? ? PRODUCTION_BASE_URL : STAGING_BASE_URL
2021-09-30 23:02:18 +05:30
end
end
end
ServicePing::SubmitService.prepend_mod