diff options
Diffstat (limited to 'app/services/service_ping/submit_service.rb')
-rw-r--r-- | app/services/service_ping/submit_service.rb | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/app/services/service_ping/submit_service.rb b/app/services/service_ping/submit_service.rb index c8733bc2f11..343fc00a2f0 100644 --- a/app/services/service_ping/submit_service.rb +++ b/app/services/service_ping/submit_service.rb @@ -6,6 +6,7 @@ module ServicePing STAGING_BASE_URL = 'https://gitlab-services-version-gitlab-com-staging.gs-staging.gitlab.org' USAGE_DATA_PATH = 'usage_data' ERROR_PATH = 'usage_ping_errors' + METADATA_PATH = 'usage_ping_metadata' SubmissionError = Class.new(StandardError) @@ -18,26 +19,27 @@ module ServicePing start = Time.current begin - usage_data = BuildPayloadService.new.execute + usage_data = ServicePing::BuildPayload.new.execute response = submit_usage_data_payload(usage_data) rescue StandardError => e return unless Gitlab::CurrentSettings.usage_ping_enabled? error_payload = { time: Time.current, - uuid: Gitlab::UsageData.add_metric('UuidMetric'), - hostname: Gitlab::UsageData.add_metric('HostnameMetric'), - version: Gitlab::UsageData.alt_usage_data { Gitlab::VERSION }, - message: e.message, + uuid: Gitlab::CurrentSettings.uuid, + hostname: Gitlab.config.gitlab.host, + version: Gitlab.version_info.to_s, + message: "#{e.message.presence || e.class} at #{e.backtrace[0]}", elapsed: (Time.current - start).round(1) } - submit_payload({ error: error_payload }, url: error_url) + submit_payload({ error: error_payload }, path: ERROR_PATH) usage_data = Gitlab::Usage::ServicePingReport.for(output: :all_metrics_values) response = submit_usage_data_payload(usage_data) end - version_usage_data_id = response.dig('conv_index', 'usage_data_id') || response.dig('dev_ops_score', 'usage_data_id') + version_usage_data_id = + response.dig('conv_index', 'usage_data_id') || response.dig('dev_ops_score', 'usage_data_id') 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}" @@ -46,23 +48,32 @@ module ServicePing unless @skip_db_write raw_usage_data = save_raw_usage_data(usage_data) raw_usage_data.update_version_metadata!(usage_data_id: version_usage_data_id) - DevopsReportService.new(response).execute + ServicePing::DevopsReport.new(response).execute end - end - def url - URI.join(base_url, USAGE_DATA_PATH) - end + return unless Feature.enabled?(:measure_service_ping_metric_collection) - def error_url - URI.join(base_url, ERROR_PATH) + submit_payload({ metadata: { metrics: metrics_collection_time(usage_data) } }, path: METADATA_PATH) end private - def submit_payload(payload, url: self.url) + 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) Gitlab::HTTP.post( - url, + URI.join(base_url, path), body: payload.to_json, allow_local_requests: true, headers: { 'Content-type' => 'application/json' } @@ -80,9 +91,13 @@ module ServicePing end def save_raw_usage_data(usage_data) - RawUsageData.safe_find_or_create_by(recorded_at: usage_data[:recorded_at]) do |record| + # 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| record.payload = usage_data end + # rubocop: enable CodeReuse/ActiveRecord end # See https://gitlab.com/gitlab-org/gitlab/-/issues/233615 for details |