Class: Perfetto::Middleware

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

Overview

Rack middleware

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
9
10
# File 'lib/perfetto/middleware.rb', line 6

def initialize(app, options = {})
  @app = app
  @options = options
  @env_proc = options[:env_proc]
end

Instance Method Details

#call(env) ⇒ Object

rubocop:enable Metrics/MethodLength



29
30
31
32
33
34
35
# File 'lib/perfetto/middleware.rb', line 29

def call(env)
  if Perfetto::Configure.enable_tracing?
    perfetto_traced_call(env)
  else
    @app.call(env)
  end
end

#perfetto_traced_call(env) ⇒ Object

rubocop:disable Metrics/MethodLength



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/perfetto/middleware.rb', line 13

def perfetto_traced_call(env)
  category = "RackMiddleware"
  method = env["REQUEST_METHOD"] || "UNKNOWN"
  path = env["PATH_INFO"] || "UNKNOWN PATH"
  task_name = "#{method} #{path}"
  env_str = @env_proc&.call(env) || { env: "unknown" }.to_json

  Perfetto.trace_event_begin_with_debug_info category, task_name, "env", env_str
  begin
    @app.call(env)
  ensure
    Perfetto.trace_event_end category
  end
end