diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 15:11:03 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 15:11:03 +0300 |
commit | 027f19b39c73b3b98e7cf305fead871626e8b717 (patch) | |
tree | d97ff241c76a5655f8f58444ccf76695b1b89bcf /lib/gitlab/memory | |
parent | a7b422860c90eecd1b98845d234a8347686fbdcf (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/memory')
-rw-r--r-- | lib/gitlab/memory/reports_daemon.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/memory/reports_uploader.rb | 35 | ||||
-rw-r--r-- | lib/gitlab/memory/upload_and_cleanup_reports.rb | 85 |
3 files changed, 121 insertions, 1 deletions
diff --git a/lib/gitlab/memory/reports_daemon.rb b/lib/gitlab/memory/reports_daemon.rb index ed1da8baab5..0dfc31235e7 100644 --- a/lib/gitlab/memory/reports_daemon.rb +++ b/lib/gitlab/memory/reports_daemon.rb @@ -7,7 +7,7 @@ module Gitlab DEFAULT_SLEEP_MAX_DELTA_S = 600 # 0..10 minutes DEFAULT_SLEEP_BETWEEN_REPORTS_S = 120 # 2 minutes - DEFAULT_REPORTS_PATH = '/tmp' + DEFAULT_REPORTS_PATH = Dir.tmpdir def initialize(**options) super diff --git a/lib/gitlab/memory/reports_uploader.rb b/lib/gitlab/memory/reports_uploader.rb new file mode 100644 index 00000000000..846a9e5fcae --- /dev/null +++ b/lib/gitlab/memory/reports_uploader.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Gitlab + module Memory + class ReportsUploader + # This is no-op currently, it will only write logs. + # The uploader implementation will be done in the next MR(s). For more details, check: + # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/97155#note_1099244930 + def upload(path) + log_upload_requested(path) + + false # nothing is uploaded in the current implementation + end + + private + + def log_upload_requested(path) + Gitlab::AppLogger.info(log_labels.merge(perf_report_status: 'upload requested', perf_report_path: path)) + end + + def log_labels + { + message: "Diagnostic reports", + class: self.class.name, + pid: $$, + worker_id: worker_id + } + end + + def worker_id + ::Prometheus::PidProvider.worker_id + end + end + end +end diff --git a/lib/gitlab/memory/upload_and_cleanup_reports.rb b/lib/gitlab/memory/upload_and_cleanup_reports.rb new file mode 100644 index 00000000000..b14f24fe13c --- /dev/null +++ b/lib/gitlab/memory/upload_and_cleanup_reports.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +module Gitlab + module Memory + class UploadAndCleanupReports + DEFAULT_SLEEP_TIME_SECONDS = 900 # 15 minutes + + def initialize( + sleep_time_seconds: ENV['GITLAB_DIAGNOSTIC_REPORTS_UPLOADER_SLEEP_S']&.to_i || DEFAULT_SLEEP_TIME_SECONDS, + reports_path: ENV["GITLAB_DIAGNOSTIC_REPORTS_PATH"]) + + @sleep_time_seconds = sleep_time_seconds + @reports_path = reports_path + + unless @reports_path.present? + log_error_reports_path_missing + return + end + + @uploader = ReportsUploader.new + + @alive = true + end + + attr_reader :sleep_time_seconds, :reports_path, :uploader, :alive + + def call + log_started + + while alive + sleep(sleep_time_seconds) + + next unless Feature.enabled?(:gitlab_diagnostic_reports_uploader, type: :ops) + + files_to_process.each { |path| upload_and_cleanup!(path) } + end + end + + private + + def upload_and_cleanup!(path) + cleanup!(path) if uploader.upload(path) + rescue StandardError => error + log_exception(error) + end + + def cleanup!(path) + File.unlink(path) if File.exist?(path) + rescue Errno::ENOENT + # Path does not exist: Ignore. We already check `File.exist?`. Rescue to be extra safe. + end + + def files_to_process + Dir.entries(reports_path) + .map { |path| File.join(reports_path, path) } + .select { |path| File.file?(path) } + end + + def log_error_reports_path_missing + Gitlab::AppLogger.error(log_labels.merge(perf_report_status: "path is not configured")) + end + + def log_started + Gitlab::AppLogger.info(log_labels.merge(perf_report_status: "started")) + end + + def log_exception(error) + Gitlab::ErrorTracking.log_exception(error, log_labels) + end + + def log_labels + { + message: "Diagnostic reports", + class: self.class.name, + pid: $$, + worker_id: worker_id + } + end + + def worker_id + ::Prometheus::PidProvider.worker_id + end + end + end +end |