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

memory_report.rb « middleware « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c6c771009c76d14811fceba521a2e24d1b24a8a (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
# frozen_string_literal: true

module Gitlab
  module Middleware
    class MemoryReport
      def initialize(app)
        @app = app
      end

      def call(env)
        request = ActionDispatch::Request.new(env)

        return @app.call(env) unless rendering_memory_profiler?(request)

        begin
          require 'memory_profiler'

          report = MemoryProfiler.report do
            @app.call(env)
          end

          report = report_to_string(report)
          headers = { 'Content-Type' => 'text/plain' }

          [200, headers, [report]]
        rescue StandardError => e
          ::Gitlab::ErrorTracking.track_exception(e)
          [500, { 'Content-Type' => 'text/plain' }, ["Could not generate memory report: #{e}"]]
        end
      end

      private

      def rendering_memory_profiler?(request)
        Rails.env.development? && request.params['performance_bar'] == 'memory'
      end

      def report_to_string(report)
        io = StringIO.new
        report.pretty_print(io, detailed_report: true, scale_bytes: true, normalize_paths: true)
        io.string
      end
    end
  end
end