debian-mirror-gitlab/lib/gitlab/memory/reporter.rb

143 lines
3.9 KiB
Ruby
Raw Normal View History

2023-03-04 22:38:38 +05:30
# frozen_string_literal: true
module Gitlab
module Memory
class Reporter
2023-03-17 16:20:25 +05:30
COMPRESS_CMD = %w[gzip --fast].freeze
2023-03-04 22:38:38 +05:30
attr_reader :reports_path
def initialize(reports_path: nil, logger: Gitlab::AppLogger)
@reports_path = reports_path || ENV["GITLAB_DIAGNOSTIC_REPORTS_PATH"] || Dir.mktmpdir
@logger = logger
@worker_id = ::Prometheus::PidProvider.worker_id
@worker_uuid = SecureRandom.uuid
init_prometheus_metrics
end
def run_report(report)
return false unless report.active?
@logger.info(
log_labels(
message: 'started',
perf_report: report.name
))
start_monotonic_time = Gitlab::Metrics::System.monotonic_time
start_thread_cpu_time = Gitlab::Metrics::System.thread_cpu_time
report_file = store_report(report)
cpu_s = Gitlab::Metrics::System.thread_cpu_duration(start_thread_cpu_time)
duration_s = Gitlab::Metrics::System.monotonic_time - start_monotonic_time
@logger.info(
log_labels(
message: 'finished',
perf_report: report.name,
cpu_s: cpu_s.round(2),
duration_s: duration_s.round(2),
perf_report_file: report_file,
perf_report_size_bytes: file_size(report_file)
))
@report_duration_counter.increment({ report: report.name }, duration_s)
true
rescue StandardError => e
@logger.error(
log_labels(
message: 'failed',
perf_report: report.name,
error: e.inspect
))
false
end
private
def store_report(report)
# Store report in tmp subdir while it is still streaming.
# This will clearly separate finished reports from the files we are still writing to.
tmp_dir = File.join(@reports_path, 'tmp')
FileUtils.mkdir_p(tmp_dir)
report_file = file_name(report)
tmp_file_path = File.join(tmp_dir, report_file)
2023-04-23 21:23:45 +05:30
write_compressed_file(report, tmp_file_path)
2023-03-17 16:20:25 +05:30
File.join(@reports_path, report_file).tap do |report_file_path|
FileUtils.mv(tmp_file_path, report_file_path)
end
end
2023-04-23 21:23:45 +05:30
def write_compressed_file(report, path)
2023-03-04 22:38:38 +05:30
io_r, io_w = IO.pipe
2023-03-17 16:20:25 +05:30
err_r, err_w = IO.pipe
2023-03-04 22:38:38 +05:30
pid = nil
2023-03-17 16:20:25 +05:30
status = nil
File.open(path, 'wb') do |file|
2023-03-04 22:38:38 +05:30
extras = {
in: io_r,
out: file,
2023-03-17 16:20:25 +05:30
err: err_w
2023-03-04 22:38:38 +05:30
}
2023-03-17 16:20:25 +05:30
pid = Process.spawn(*COMPRESS_CMD, **extras)
2023-03-04 22:38:38 +05:30
io_r.close
2023-03-17 16:20:25 +05:30
err_w.close
2023-03-04 22:38:38 +05:30
report.run(io_w)
io_w.close
2023-03-17 16:20:25 +05:30
_, status = Process.wait2(pid)
2023-03-04 22:38:38 +05:30
end
2023-03-17 16:20:25 +05:30
errors = err_r.read&.strip
err_r.close
raise StandardError, "exit #{status.exitstatus}: #{errors}" if !status&.success? && errors.present?
2023-03-04 22:38:38 +05:30
ensure
2023-03-17 16:20:25 +05:30
[io_r, io_w, err_r, err_w].each(&:close)
2023-03-04 22:38:38 +05:30
# Make sure we don't leave any running processes behind.
Gitlab::ProcessManagement.signal(pid, :KILL) if pid
end
def log_labels(**extra_labels)
{
pid: $$,
worker_id: @worker_id,
perf_report_worker_uuid: @worker_uuid
}.merge(extra_labels)
end
def file_name(report)
timestamp = Time.current.strftime('%Y-%m-%d.%H:%M:%S:%L')
report_id = [@worker_id, @worker_uuid].join(".")
[report.name, timestamp, report_id, 'gz'].compact_blank.join('.')
end
def file_size(file_path)
File.size(file_path.to_s)
rescue Errno::ENOENT
0
end
def init_prometheus_metrics
default_labels = { pid: @worker_id }
@report_duration_counter = Gitlab::Metrics.counter(
:gitlab_diag_report_duration_seconds_total,
'Total time elapsed for running diagnostic report',
default_labels
)
end
end
end
end