class Tracebin::Middleware

Attributes

config[R]
logger[R]

Public Class Methods

new(app) click to toggle source
# File lib/tracebin/middleware.rb, line 9
def initialize(app)
  @app = app

  @config = Tracebin::Agent.config
  @logger = Tracebin::Agent.logger

  start_agent_parent_process
end

Public Instance Methods

__call(env) click to toggle source
# File lib/tracebin/middleware.rb, line 22
def __call(env)
  start_agent_child_process

  if agent_disabled?(env)
    @logger.debug "TRACEBIN: Tracebin disabled for this request."
    return @app.call env
  else
    @tracebin_timer = Timer.new
    @tracebin_timer.start!

    status, headers, response = @app.call(env)

    @tracebin_timer.transaction_name = fetch_endpoint_name(env)

    @tracebin_timer.stop!

    PuppetMaster.new(@tracebin_timer).process

    return [status, headers, response]
  end
end
call(env) click to toggle source
# File lib/tracebin/middleware.rb, line 18
def call(env)
  dup.__call(env)
end

Private Instance Methods

agent_disabled?(env) click to toggle source
# File lib/tracebin/middleware.rb, line 64
def agent_disabled?(env)
  path = env['REQUEST_PATH']
  ignored_paths = config.ignored_paths.map { |root| %r{^#{root}} }

  !Tracebin::Agent.child_process_started? ||
    ignored_paths.any? { |root| !!root.match(path) }
end
fetch_endpoint_name(env) click to toggle source
# File lib/tracebin/middleware.rb, line 46
def fetch_endpoint_name(env)
  if controller = env['action_controller.instance']
    "#{controller.class}##{controller.params['action']}"
  elsif route = env['sinatra.route']
    route
  else
    'RackTransaction'
  end
end
start_agent_child_process() click to toggle source
# File lib/tracebin/middleware.rb, line 56
def start_agent_child_process
  Tracebin::Agent.start_child_process
end
start_agent_parent_process() click to toggle source
# File lib/tracebin/middleware.rb, line 60
def start_agent_parent_process
  Tracebin::Agent.start_parent_process
end