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
# File 'lib/scout_apm/middleware.rb', line 5

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

Instance Method Details

#attempt_to_start_agentObject



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

def attempt_to_start_agent
  @attempts += 1
  ScoutApm::Agent.instance.start(:skip_app_server_check => true)
  @started = ScoutApm::Agent.instance.started?
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



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

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