lib/tracebin/middleware.rb in tracebin-0.0.9 vs lib/tracebin/middleware.rb in tracebin-0.0.10

- old
+ new

@@ -1,42 +1,40 @@ require 'tracebin/timer' require 'tracebin/puppet_master' module Tracebin class Middleware - attr_reader :config + attr_reader :config, :logger def initialize(app) @app = app @config = Tracebin::Agent.config + @logger = Tracebin::Agent.logger - Tracebin::Agent.start! unless Tracebin::Agent.started? + start_agent end def call(env) dup.__call(env) end def __call(env) - path = env['REQUEST_PATH'] - ignored_paths = config.ignored_paths.map { |root| %r{^#{root}} } - - if ignored_paths.any? { |root| !!root.match(path) } - @app.call env + if agent_disabled?(env) + return @app.call env else - timer = Timer.new - timer.start! + @tracebin_timer = Timer.new + @tracebin_timer.start! status, headers, response = @app.call(env) - timer.transaction_name = fetch_endpoint_name env + @tracebin_timer.transaction_name = fetch_endpoint_name(env) - timer.stop! + @tracebin_timer.stop! - PuppetMaster.new(timer).process + PuppetMaster.new(@tracebin_timer).process - [status, headers, response] + return [status, headers, response] end end private @@ -44,8 +42,22 @@ if controller = env['action_controller.instance'] "#{controller.class}##{controller.params['action']}" else 'RackTransaction' end + end + + def start_agent + Tracebin::Agent.start! + rescue => e + @logger.warn "TRACEBIN: Failed to start agent: #{e.message}" + end + + def agent_disabled?(env) + path = env['REQUEST_PATH'] + ignored_paths = config.ignored_paths.map { |root| %r{^#{root}} } + + !Tracebin::Agent.started? || + ignored_paths.any? { |root| !!root.match(path) } end end end