Class: NewRelic::Control::Frameworks::Rails

Inherits:
Ruby show all
Defined in:
lib/new_relic/control/frameworks/rails.rb

Overview

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

Direct Known Subclasses

Rails3

Instance Attribute Summary

Attributes included from InstanceMethods

#local_env

Attributes included from LoggingMethods

#log_file

Instance Method Summary collapse

Methods inherited from Ruby

#config_file

Methods included from ClassMethods

#instance, #load_framework_class, #load_test_framework, #local_env, #new_instance, #newrelic_root

Methods included from InstanceMethods

#agent_enabled?, #app, #init_plugin, #start_agent, #to_s

Methods included from LanguageSupport::Control

included

Methods included from LoggingMethods

#find_or_create_file_path, #log, #log_file_name, #log_path, #log_to_stdout?, #set_log_format!, #set_log_level!, #setup_log, #should_log?

Methods included from Instrumentation

#add_instrumentation, #install_instrumentation, #load_instrumentation_files, #load_samplers

Methods included from ServerMethods

#api_server, #cert_file_path, #convert_to_ip_address, #http_connection, #proxy_server, #resolve_ip_address, #server, #server_from_host

Methods included from Configuration

#[], #[]=, #apdex_t, #app_names, #browser_monitoring_auto_instrument?, #capture_params, #developer_mode?, #disable_backtrace_cleanup?, #disable_serialization=, #disable_serialization?, #dispatcher, #dispatcher_instance_id, #fetch, #has_slow_sql_config?, #license_key, #log_file_path, #merge_defaults, #merge_options, #merge_server_side_config, #monitor_mode?, #multi_threaded?, #post_size_limit, #remove_server_controlled_configs, #send_data_on_exit, #settings, #sync_startup, #use_ssl?, #use_textmate?, #validate_seed, #validate_token, #verify_certificate?

Methods included from Profiling

#profiling=, #profiling?, #profiling_available?

Instance Method Details

#envObject



10
11
12
# File 'lib/new_relic/control/frameworks/rails.rb', line 10

def env
  @env ||= RAILS_ENV.dup
end

#init_config(options = {}) ⇒ Object

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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/new_relic/control/frameworks/rails.rb', line 27

def init_config(options={})
  rails_config = options[:config]
  if !rails_config && defined?(::Rails) && ::Rails.respond_to?(:configuration)
    rails_config = ::Rails.configuration
  end
  # 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.  See 
      # delayed_job_injection.rb
      DependencyDetection.detect!
    end
  end          
  if !agent_enabled?
    # Might not be running if it does not think mongrel, thin, passenger, etc
    # is running, if it things it's a rake task, or if the agent_enabled is false.
    log! "New Relic Agent not running."
  else
    log! "Starting the New Relic Agent."
    install_developer_mode rails_config if developer_mode?
    install_browser_monitoring(rails_config)
  end
end

#install_browser_monitoring(config) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/new_relic/control/frameworks/rails.rb', line 52

def install_browser_monitoring(config)
  return if @browser_monitoring_installed
  @browser_monitoring_installed = true
  return if config.nil? || !config.respond_to?(:middleware) || ! browser_monitoring_auto_instrument?
  begin
    require 'new_relic/rack/browser_monitoring'
    config.middleware.use NewRelic::Rack::BrowserMonitoring
    log!("Installed New Relic Browser Monitoring middleware", :info)
  rescue Exception => e
    log!("Error installing New Relic Browser Monitoring middleware: #{e.inspect}", :error)
  end
end

#install_developer_mode(rails_config) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/new_relic/control/frameworks/rails.rb', line 65

def install_developer_mode(rails_config)
  return if @installed
  @installed = true
  if rails_config && rails_config.respond_to?(:middleware)
    begin
      require 'new_relic/rack/developer_mode'
      rails_config.middleware.use NewRelic::Rack::DeveloperMode

      # inform user that the dev edition is available if we are running inside
      # a webserver process
      if @local_env.dispatcher_instance_id
        port = @local_env.dispatcher_instance_id.to_s =~ /^\d+/ ? ":#{local_env.dispatcher_instance_id}" : ":port"
        log!("NewRelic Agent Developer Mode enabled.")
        log!("To view performance information, go to http://localhost#{port}/newrelic")
      end
    rescue Exception => e
      log!("Error installing New Relic Developer Mode: #{e.inspect}", :error)
    end
  elsif rails_config
    log!("Developer mode not available for Rails versions prior to 2.2", :warn)
  end
end

#log!(msg, level = :info) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/new_relic/control/frameworks/rails.rb', line 88

def log!(msg, level=:info)
  if should_log?
    logger = ::Rails.respond_to?(:logger) ? ::Rails.logger : ::RAILS_DEFAULT_LOGGER
    logger.send(level, msg)
  else
    super
  end
rescue Exception => e
  super
end

#loggerObject



20
21
22
# File 'lib/new_relic/control/frameworks/rails.rb', line 20

def logger
  ::RAILS_DEFAULT_LOGGER
end

#rails_versionObject



106
107
108
# File 'lib/new_relic/control/frameworks/rails.rb', line 106

def rails_version
  @rails_version ||= NewRelic::VersionNumber.new(::Rails::VERSION::STRING)
end

#rootObject



13
14
15
16
17
18
19
# File 'lib/new_relic/control/frameworks/rails.rb', line 13

def root
  if defined?(RAILS_ROOT) && RAILS_ROOT.to_s != ''
    RAILS_ROOT.to_s
  else
    super
  end
end

#to_stdout(message) ⇒ Object



99
100
101
102
103
104
# File 'lib/new_relic/control/frameworks/rails.rb', line 99

def to_stdout(message)
  logger = ::Rails.respond_to?(:logger) ? ::Rails.logger : ::RAILS_DEFAULT_LOGGER
  logger.info(message)
rescue Exception => e
  super
end