class Mmtrix::Control::Frameworks::Rails

Control subclass instantiated when Rails is detected. Contains Rails specific configuration, instrumentation, environment values, etc.

Public Instance Methods

env() click to toggle source
# File lib/mmtrix/control/frameworks/rails.rb, line 14
def env
  @env ||= RAILS_ENV.dup
end
init_config(options={}) click to toggle source

In versions of Rails prior to 2.0, the rails config was only available to the init.rb, so it had to be passed on from there. This is a best effort to find a config and use that.

# File lib/mmtrix/control/frameworks/rails.rb, line 46
def init_config(options={})
  @config = options[:config]
  # Install the dependency detection,
  if rails_config && ::Rails.configuration.respond_to?(:after_initialize)
    rails_config.after_initialize do
      # This will insure we load all the instrumentation as late as possible.  If the agent
      # is not enabled, it will load a limited amount of instrumentation.
      DependencyDetection.detect!
    end
  end
  if !Agent.config[:agent_enabled]
    # Might not be running if it does not think mongrel, thin, passenger, etc
    # is running, if it thinks it's a rake task, or if the agent_enabled is false.
    ::Mmtrix::Agent.logger.info("Mmtrix Agent not running.")
  else
    install_developer_mode(rails_config) if Agent.config[:developer_mode]
    install_browser_monitoring(rails_config)
    install_agent_hooks(rails_config)
  end
rescue => e
  ::Mmtrix::Agent.logger.error("Failure during init_config for Rails. Is Rails required in a non-Rails app? Set MM_TRIX_FRAMEWORK=ruby to avoid this message.",
                                 "The Ruby agent will continue running, but Rails-specific features may be missing.",
                                 e)
end
install_agent_hooks(config) click to toggle source
# File lib/mmtrix/control/frameworks/rails.rb, line 71
def install_agent_hooks(config)
  return if @agent_hooks_installed
  @agent_hooks_installed = true
  return if config.nil? || !config.respond_to?(:middleware)
  begin
    require 'mmtrix/rack/agent_hooks'
    return unless Mmtrix::Rack::AgentHooks.needed?
    config.middleware.use Mmtrix::Rack::AgentHooks
    ::Mmtrix::Agent.logger.debug("Installed Mmtrix Agent Hooks middleware")
  rescue => e
    ::Mmtrix::Agent.logger.warn("Error installing Mmtrix Agent Hooks middleware", e)
  end
end
install_browser_monitoring(config) click to toggle source
# File lib/mmtrix/control/frameworks/rails.rb, line 85
def install_browser_monitoring(config)
  return if @browser_monitoring_installed
  @browser_monitoring_installed = true
  return if config.nil? || !config.respond_to?(:middleware) || !Agent.config[:'browser_monitoring.auto_instrument']
  begin
    require 'mmtrix/rack/browser_monitoring'
    config.middleware.use Mmtrix::Rack::BrowserMonitoring
    ::Mmtrix::Agent.logger.debug("Installed Mmtrix Browser Monitoring middleware")
  rescue => e
    ::Mmtrix::Agent.logger.warn("Error installing Mmtrix Browser Monitoring middleware", e)
  end
end
install_developer_mode(rails_config) click to toggle source
# File lib/mmtrix/control/frameworks/rails.rb, line 98
def install_developer_mode(rails_config)
  return if @installed
  @installed = true
  if rails_config && rails_config.respond_to?(:middleware)
    begin
      require 'mmtrix/rack/developer_mode'
      rails_config.middleware.use Mmtrix::Rack::DeveloperMode
      ::Mmtrix::Agent.logger.info("Mmtrix Agent Developer Mode enabled.")
      if env == "production"
        ::Mmtrix::Agent.logger.warn("***Mmtrix Developer Mode is not intended to be enabled in production environments! We highly recommend setting developer_mode: false for the production environment in your mmtrix.yml.")
      end
    rescue => e
      ::Mmtrix::Agent.logger.warn("Error installing Mmtrix Developer Mode", e)
    end
  elsif rails_config
    ::Mmtrix::Agent.logger.warn("Developer mode not available for Rails versions prior to 2.2")
  end
end
rails_config() click to toggle source
# File lib/mmtrix/control/frameworks/rails.rb, line 35
def rails_config
  if defined?(::Rails) && ::Rails.respond_to?(:configuration)
    ::Rails.configuration
  else
    @config
  end
end
rails_root() click to toggle source
# File lib/mmtrix/control/frameworks/rails.rb, line 31
def rails_root
  RAILS_ROOT if defined?(RAILS_ROOT)
end
rails_version() click to toggle source
# File lib/mmtrix/control/frameworks/rails.rb, line 117
def rails_version
  @rails_version ||= Mmtrix::VersionNumber.new(::Rails::VERSION::STRING)
end
root() click to toggle source

Rails can return an empty string from this method, causing the agent not to start even when it is properly in a rails 3 application, so we test the value to make sure it actually has contents, and bail to the parent class if it is empty.

Calls superclass method Mmtrix::Control::Frameworks::Ruby#root
# File lib/mmtrix/control/frameworks/rails.rb, line 22
def root
  root = rails_root.to_s
  if !root.empty?
    root
  else
    super
  end
end

Protected Instance Methods

install_shim() click to toggle source
# File lib/mmtrix/control/frameworks/rails.rb, line 127
def install_shim
  super
  require 'mmtrix/agent/instrumentation/controller_instrumentation'
  if ActiveSupport.respond_to?(:on_load) # rails 3+
    ActiveSupport.on_load(:action_controller) { include Mmtrix::Agent::Instrumentation::ControllerInstrumentation::Shim }
  else
    ActionController::Base.class_eval { include Mmtrix::Agent::Instrumentation::ControllerInstrumentation::Shim }
  end
end
rails_vendor_root() click to toggle source
# File lib/mmtrix/control/frameworks/rails.rb, line 123
def rails_vendor_root
  File.join(root,'vendor','rails')
end