Class: ScoutApm::Middleware

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

Constant Summary collapse

MAX_ATTEMPTS =
5

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



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

def initialize(app)
  @app = app
  @attempts = 0
  @enabled = ScoutApm::Agent.instance.apm_enabled?
  @started = ScoutApm::Agent.instance.started? && ScoutApm::Agent.instance.background_worker_running?
end

Instance Method Details

#attempt_to_start_agentObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/scout_apm/middleware.rb', line 22

def attempt_to_start_agent
  @attempts += 1
  ScoutApm::Agent.instance.start(:skip_app_server_check => true)
  ScoutApm::Agent.instance.start_background_worker
  @started = ScoutApm::Agent.instance.started? && ScoutApm::Agent.instance.background_worker_running?
rescue => e
  # Can't be sure of any logging here, so fall back to ENV var and STDOUT
  if ENV["SCOUT_LOG_LEVEL"] == "debug"
    STDOUT.puts "Failed to start via Middleware: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
  end
end

#call(env) ⇒ Object

If we get a web request in, then we know we’re running in some sort of app server



13
14
15
16
17
18
19
20
# File 'lib/scout_apm/middleware.rb', line 13

def call(env)
  if !@enabled || @started || @attempts > MAX_ATTEMPTS
    @app.call(env)
  else
    attempt_to_start_agent
    @app.call(env)
  end
end