Welcome to mirror list, hosted at ThFree Co, Russian Federation.

reporter.rb « memory « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4f3f950b95396ebbf4a5aee4312348fbb21d462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

module Gitlab
  module Memory
    class Reporter
      def initialize
        @worker_uuid = SecureRandom.uuid

        init_prometheus_metrics
      end

      def run_report(report)
        start_monotonic_time = Gitlab::Metrics::System.monotonic_time
        start_thread_cpu_time = Gitlab::Metrics::System.thread_cpu_time

        file_path = report.run(report_id)

        cpu_s = Gitlab::Metrics::System.thread_cpu_duration(start_thread_cpu_time)
        duration_s = Gitlab::Metrics::System.monotonic_time - start_monotonic_time

        log_report(name: report.name, cpu_s: cpu_s, duration_s: duration_s, size: file_size(file_path))

        @report_duration_counter.increment({ report: report.name }, duration_s)
      end

      private

      def log_report(name:, duration_s:, cpu_s:, size:)
        Gitlab::AppLogger.info(
          message: 'finished',
          pid: $$,
          worker_id: worker_id,
          perf_report: name,
          duration_s: duration_s.round(2),
          cpu_s: cpu_s.round(2),
          perf_report_size_bytes: size,
          perf_report_worker_uuid: @worker_uuid
        )
      end

      def report_id
        [worker_id, @worker_uuid].join(".")
      end

      def worker_id
        ::Prometheus::PidProvider.worker_id
      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