# encoding: utf-8 require 'one_apm/frameworks/ruby' module OneApm class Probe module Frameworks class Rails < OneApm::Probe::Frameworks::Ruby def env @env ||= RAILS_ENV.dup end def root root = rails_root.to_s if !root.empty? root else super end end def rails_root RAILS_ROOT if defined?(RAILS_ROOT) end def rails_config if defined?(::Rails) && ::Rails.respond_to?(:configuration) ::Rails.configuration else @config end end def init_config(options = {}) @config = options[:config] if rails_config && ::Rails.configuration.respond_to?(:after_initialize) rails_config.after_initialize do LibraryDetection.detect! end end if !Manager.config[:agent_enabled] OneApm::Manager.logger.info("OneApm Agent not running.") else install_developer_mode(rails_config) if Manager.config[:developer_mode] install_browser_monitoring(rails_config) install_agent_hooks(rails_config) end end def agent_hooks_installed @@agent_hooks_installed ||= false end def browser_monitoring_installed @@browser_monitoring_installed ||= false end def developer_mode_installed @@developer_mode_installed ||= false end def install_agent_hooks(config) return if agent_hooks_installed @@agent_hooks_installed = true return if config.nil? || !config.respond_to?(:middleware) begin require 'one_apm/rack/middleware_hooks' return unless OneApm::Rack::MiddlewareHooks.needed? config.middleware.use OneApm::Rack::MiddlewareHooks OneApm::Manager.logger.debug("Installed OneApm Agent Hooks middleware") rescue => e OneApm::Manager.logger.warn("Error installing OneApm Agent Hooks middleware", e) end end def install_browser_monitoring(config) return if browser_monitoring_installed @@browser_monitoring_installed = true return if config.nil? || !config.respond_to?(:middleware) || !Manager.config[:'browser_monitoring.auto_instrument'] begin require 'one_apm/rack/browser_monitoring' config.middleware.use OneApm::Rack::BrowserMonitoring OneApm::Manager.logger.debug("Installed OneApm Browser Monitoring middleware") rescue => e OneApm::Manager.logger.warn("Error installing OneApm Browser Monitoring middleware", e) end end def install_developer_mode(rails_config) return if env != "development" return if developer_mode_installed @@developer_mode_installed = true if rails_config && rails_config.respond_to?(:middleware) begin require 'one_apm/rack/developer_mode' rails_config.middleware.use OneApm::Rack::DeveloperMode OneApm::Manager.logger.info("Developer Mode enabled.") rescue => e ::OneApm::Manager.logger.warn("Error installing OneAPM Developer Mode", e) end elsif rails_config ::OneApm::Manager.logger.warn("Developer mode not available for Rails versions prior to 2.2") end end protected def install_shim super require 'one_apm/inst/transaction_base' if ActiveSupport.respond_to?(:on_load) # rails 3+ ActiveSupport.on_load(:action_controller) { include OneApm::Agent::Instrumentation::TransactionBase::Shim } else ActionController::Base.class_eval { include OneApm::Agent::Instrumentation::TransactionBase::Shim } end end end end end end