2021-04-27 12:10:12 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Middleware
|
|
|
|
class Speedscope
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2021-04-29 15:10:07 +00:00
|
|
|
request = ActionDispatch::Request.new(env)
|
2021-04-27 12:10:12 +00:00
|
|
|
|
2021-04-29 15:10:07 +00:00
|
|
|
return @app.call(env) unless rendering_flamegraph?(request)
|
2021-04-27 12:10:12 +00:00
|
|
|
|
2021-04-29 15:10:07 +00:00
|
|
|
body = nil
|
2021-04-27 12:10:12 +00:00
|
|
|
|
2021-04-29 15:10:07 +00:00
|
|
|
::Gitlab::SafeRequestStore[:capturing_flamegraph] = true
|
2021-04-27 12:10:12 +00:00
|
|
|
|
2021-04-29 15:10:07 +00:00
|
|
|
require 'stackprof'
|
|
|
|
|
|
|
|
begin
|
2021-04-27 12:10:12 +00:00
|
|
|
flamegraph = ::StackProf.run(
|
|
|
|
mode: :wall,
|
|
|
|
raw: true,
|
|
|
|
aggregate: false,
|
2021-04-29 15:10:07 +00:00
|
|
|
interval: ::Gitlab::StackProf::DEFAULT_INTERVAL_US
|
2021-04-27 12:10:12 +00:00
|
|
|
) do
|
|
|
|
_, _, body = @app.call(env)
|
|
|
|
end
|
2021-04-29 15:10:07 +00:00
|
|
|
ensure
|
2021-04-27 12:10:12 +00:00
|
|
|
body.close if body.respond_to?(:close)
|
|
|
|
end
|
|
|
|
|
2021-04-29 15:10:07 +00:00
|
|
|
render_flamegraph(flamegraph, request)
|
2021-04-27 12:10:12 +00:00
|
|
|
end
|
|
|
|
|
2021-04-29 15:10:07 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def rendering_flamegraph?(request)
|
|
|
|
request.params['performance_bar'] == 'flamegraph' && ::Gitlab::PerformanceBar.allowed_for_user?(request.env['warden']&.user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_flamegraph(graph, request)
|
2021-04-27 12:10:12 +00:00
|
|
|
headers = { 'Content-Type' => 'text/html' }
|
2021-04-29 15:10:07 +00:00
|
|
|
path = request.env['PATH_INFO'].sub('//', '/')
|
|
|
|
|
2021-04-30 12:12:30 +00:00
|
|
|
speedscope_path = ::Gitlab::Utils.append_path(::Gitlab.config.gitlab.relative_url_root, '/-/speedscope/index.html')
|
2021-04-27 12:10:12 +00:00
|
|
|
|
|
|
|
html = <<~HTML
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<style>
|
|
|
|
body { margin: 0; height: 100vh; }
|
|
|
|
#speedscope-iframe { width: 100%; height: 100%; border: none; }
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
2021-04-29 15:10:07 +00:00
|
|
|
<script type="text/javascript" nonce="#{request.content_security_policy_nonce}">
|
2021-04-27 12:10:12 +00:00
|
|
|
var graph = #{Gitlab::Json.generate(graph)};
|
|
|
|
var json = JSON.stringify(graph);
|
|
|
|
var blob = new Blob([json], { type: 'text/plain' });
|
|
|
|
var objUrl = encodeURIComponent(URL.createObjectURL(blob));
|
|
|
|
var iframe = document.createElement('IFRAME');
|
|
|
|
iframe.setAttribute('id', 'speedscope-iframe');
|
|
|
|
document.body.appendChild(iframe);
|
2021-04-30 12:12:30 +00:00
|
|
|
var iframeUrl = '#{speedscope_path}#profileURL=' + objUrl + '&title=' + 'Flamegraph for #{CGI.escape(path)}';
|
2021-04-27 12:10:12 +00:00
|
|
|
iframe.setAttribute('src', iframeUrl);
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
[200, headers, [html]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|