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

#[], #app, #dispatcher, #init_plugin, #settings, #start_agent, #to_s

Methods included from LanguageSupport::Control

included

Methods included from LoggingMethods

#find_or_create_file_path, #log, #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 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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/new_relic/control/frameworks/rails.rb', line 35

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.  See
      # delayed_job_injection.rb
      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.
    log!("New Relic Agent not running.", :debug)
  else
    log! "Starting the New Relic Agent."
    install_developer_mode rails_config if Agent.config[:developer_mode]
    install_browser_monitoring(rails_config)
  end
end

#install_browser_monitoring(config) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/new_relic/control/frameworks/rails.rb', line 57

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 'new_relic/rack/browser_monitoring'
    config.middleware.use NewRelic::Rack::BrowserMonitoring
    log!("Installed New Relic Browser Monitoring middleware", :info)
  rescue => e
    log!("Error installing New Relic Browser Monitoring middleware: #{e.inspect}", :error)
  end
end

#install_developer_mode(rails_config) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/new_relic/control/frameworks/rails.rb', line 70

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 => 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



93
94
95
96
97
98
99
100
101
102
# File 'lib/new_relic/control/frameworks/rails.rb', line 93

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 => e
  super
end

#loggerObject



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

def logger
  ::RAILS_DEFAULT_LOGGER
end

#rails_configObject



24
25
26
27
28
29
30
# File 'lib/new_relic/control/frameworks/rails.rb', line 24

def rails_config
  if defined?(::Rails) && ::Rails.respond_to?(:configuration)
    ::Rails.configuration
  else
    @config
  end
end

#rails_versionObject



111
112
113
# File 'lib/new_relic/control/frameworks/rails.rb', line 111

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



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

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