summaryrefslogtreecommitdiff
path: root/app/services/service_ping
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/service_ping')
-rw-r--r--app/services/service_ping/build_payload_service.rb27
-rw-r--r--app/services/service_ping/permit_data_categories_service.rb34
-rw-r--r--app/services/service_ping/submit_service.rb94
3 files changed, 155 insertions, 0 deletions
diff --git a/app/services/service_ping/build_payload_service.rb b/app/services/service_ping/build_payload_service.rb
new file mode 100644
index 00000000000..2bef3d32103
--- /dev/null
+++ b/app/services/service_ping/build_payload_service.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+module ServicePing
+ class BuildPayloadService
+ def execute
+ return {} unless allowed_to_report?
+
+ raw_payload
+ end
+
+ private
+
+ def allowed_to_report?
+ product_intelligence_enabled? && !User.single_user&.requires_usage_stats_consent?
+ end
+
+ def product_intelligence_enabled?
+ ::Gitlab::CurrentSettings.usage_ping_enabled?
+ end
+
+ def raw_payload
+ @raw_payload ||= ::Gitlab::UsageData.data(force_refresh: true)
+ end
+ end
+end
+
+ServicePing::BuildPayloadService.prepend_mod_with('ServicePing::BuildPayloadService')
diff --git a/app/services/service_ping/permit_data_categories_service.rb b/app/services/service_ping/permit_data_categories_service.rb
new file mode 100644
index 00000000000..ff48c022b56
--- /dev/null
+++ b/app/services/service_ping/permit_data_categories_service.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+module ServicePing
+ class PermitDataCategoriesService
+ STANDARD_CATEGORY = 'Standard'
+ SUBSCRIPTION_CATEGORY = 'Subscription'
+ OPERATIONAL_CATEGORY = 'Operational'
+ OPTIONAL_CATEGORY = 'Optional'
+ CATEGORIES = [
+ STANDARD_CATEGORY,
+ SUBSCRIPTION_CATEGORY,
+ OPERATIONAL_CATEGORY,
+ OPTIONAL_CATEGORY
+ ].to_set.freeze
+
+ def execute
+ return [] unless product_intelligence_enabled?
+
+ CATEGORIES
+ end
+
+ def product_intelligence_enabled?
+ pings_enabled? && !User.single_user&.requires_usage_stats_consent?
+ end
+
+ private
+
+ def pings_enabled?
+ ::Gitlab::CurrentSettings.usage_ping_enabled?
+ end
+ end
+end
+
+ServicePing::PermitDataCategoriesService.prepend_mod_with('ServicePing::PermitDataCategoriesService')
diff --git a/app/services/service_ping/submit_service.rb b/app/services/service_ping/submit_service.rb
new file mode 100644
index 00000000000..5c03aa46e18
--- /dev/null
+++ b/app/services/service_ping/submit_service.rb
@@ -0,0 +1,94 @@
+# frozen_string_literal: true
+
+module ServicePing
+ class SubmitService
+ PRODUCTION_URL = 'https://version.gitlab.com/usage_data'
+ STAGING_URL = 'https://gitlab-services-version-gitlab-com-staging.gs-staging.gitlab.org/usage_data'
+
+ METRICS = %w[leader_issues instance_issues percentage_issues leader_notes instance_notes
+ percentage_notes leader_milestones instance_milestones percentage_milestones
+ leader_boards instance_boards percentage_boards leader_merge_requests
+ instance_merge_requests percentage_merge_requests leader_ci_pipelines
+ instance_ci_pipelines percentage_ci_pipelines leader_environments instance_environments
+ percentage_environments leader_deployments instance_deployments percentage_deployments
+ leader_projects_prometheus_active instance_projects_prometheus_active
+ percentage_projects_prometheus_active leader_service_desk_issues instance_service_desk_issues
+ percentage_service_desk_issues].freeze
+
+ SubmissionError = Class.new(StandardError)
+
+ def execute
+ return unless ServicePing::PermitDataCategoriesService.new.product_intelligence_enabled?
+
+ begin
+ usage_data = BuildPayloadService.new.execute
+ raw_usage_data, response = submit_usage_data_payload(usage_data)
+ rescue StandardError
+ return unless Gitlab::CurrentSettings.usage_ping_enabled?
+
+ usage_data = Gitlab::UsageData.data(force_refresh: true)
+ raw_usage_data, 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')
+
+ 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
+
+ raw_usage_data.update_version_metadata!(usage_data_id: version_usage_data_id)
+
+ store_metrics(response)
+ end
+
+ private
+
+ def submit_payload(usage_data)
+ Gitlab::HTTP.post(
+ url,
+ body: usage_data.to_json,
+ allow_local_requests: true,
+ headers: { 'Content-type' => 'application/json' }
+ )
+ end
+
+ def submit_usage_data_payload(usage_data)
+ raise SubmissionError, 'Usage data is blank' if usage_data.blank?
+
+ raw_usage_data = save_raw_usage_data(usage_data)
+
+ response = submit_payload(usage_data)
+
+ raise SubmissionError, "Unsuccessful response code: #{response.code}" unless response.success?
+
+ [raw_usage_data, response]
+ end
+
+ def save_raw_usage_data(usage_data)
+ RawUsageData.safe_find_or_create_by(recorded_at: usage_data[:recorded_at]) do |record|
+ record.payload = usage_data
+ end
+ end
+
+ def store_metrics(response)
+ metrics = response['conv_index'] || response['dev_ops_score'] # leaving dev_ops_score here, as the response data comes from the gitlab-version-com
+
+ return unless metrics.present?
+
+ DevOpsReport::Metric.create!(
+ metrics.slice(*METRICS)
+ )
+ end
+
+ # See https://gitlab.com/gitlab-org/gitlab/-/issues/233615 for details
+ def url
+ if Rails.env.production?
+ PRODUCTION_URL
+ else
+ STAGING_URL
+ end
+ end
+ end
+end
+
+ServicePing::SubmitService.prepend_mod