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

def initialize(app)
  @app = app
  @attempts = 0
  # @enabled = ScoutApm::Agent.instance.context.apm_enabled?
  # XXX: Figure out if this middleware should even know
  @enabled = true
  @started = ScoutApm::Agent.instance.context.started? && ScoutApm::Agent.instance.background_worker_running?
end

Instance Method Details

#attempt_to_start_agentObject



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

def attempt_to_start_agent
  @attempts += 1
  ScoutApm::Agent.instance.start
  @started = ScoutApm::Agent.instance.context.started? && ScoutApm::Agent.instance.background_worker_running?
rescue => e
  ScoutApm::Agent.instance.context.logger("Failed to start via Middleware: #{e.message}\n\t#{e.backtrace.join("\n\t")}")
end

#call(env) ⇒ Object

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



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

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