Class: Gitlab::Middleware::Speedscope

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/middleware/speedscope.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Speedscope

Returns a new instance of Speedscope.


6
7
8
# File 'lib/gitlab/middleware/speedscope.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


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
# File 'lib/gitlab/middleware/speedscope.rb', line 10

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

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

  body = nil

  ::Gitlab::SafeRequestStore[:capturing_flamegraph] = true

  require 'stackprof'

  begin
    mode = stackprof_mode(request)
    flamegraph = ::StackProf.run(
      mode: mode,
      raw: true,
      aggregate: false,
      interval: ::Gitlab::StackProf.interval(mode)
    ) do
      _, _, body = @app.call(env)
    end
  ensure
    body.close if body.respond_to?(:close)
  end

  render_flamegraph(flamegraph, request)
end