gitlab-org--gitlab-foss/lib/gitlab/middleware/memory_report.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

46 lines
1.1 KiB
Ruby
Raw Normal View History

# 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