Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v2_3_6
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 83 lines (70 sloc) 2.82 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
require 'yaml'
require 'newrelic/agent/method_tracer'

def to_stderr(s)
  STDERR.puts "** [NewRelic] " + s
end

# Initializer for the NewRelic Agent
config_filename = File.join(File.dirname(__FILE__), '..','..','..','config','newrelic.yml')
begin catch (:disabled) do
  begin
    newrelic_config_file = File.read(config_filename)
  rescue => e
    to_stderr e
    to_stderr "Could not read configuration file #{config_filename}."
    to_stderr "Be sure to put newrelic.yml into your config directory."
    to_stderr "Agent is disabled."
    throw :disabled
  end

  begin
    newrelic_agent_config = YAML.load(newrelic_config_file)[RAILS_ENV] || {}
  rescue Exception => e
    to_stderr "Error parsing #{config_filename}"
    to_stderr "#{e}"
    to_stderr "Agent is disabled."
    throw :disabled
  end
  newrelic_agent_config.freeze
  ::NR_CONFIG_FILE = newrelic_agent_config
  
  ::RPM_AGENT_ENABLED = newrelic_agent_config['enabled']
  ::RPM_DEVELOPER = newrelic_agent_config['developer']
  
  # Check to see if the agent should be enabled or not

  # note if the agent is not turned on via the enabled flag in the
  # configuration file, the application will be untouched, and it will
  # behave exaclty as if the agent were never installed in the first place.

  ::RPM_TRACERS_ENABLED = ::RPM_DEVELOPER || ::RPM_AGENT_ENABLED

  if !::RPM_TRACERS_ENABLED || ENV['NEWRELIC_ENABLE'] && ENV['NEWRELIC_ENABLE'] =~ /false|off|no/i
    require 'newrelic/shim_agent'
    throw :disabled
  end
  
  require 'newrelic/agent'
  
  agent = NewRelic::Agent.instance
  agent.start(newrelic_agent_config)
  
  # When (and only when) RPM is running in developer mode, a few pages
  # are added to your application that present performance information
  # on the last 100 http requests your application has handled, allowing
  # you to diagnose performance problems on your desktop.
  #
  # to see this information, visit http://localhost:3000/newrelic
  if ::RPM_DEVELOPER
    controller_path = File.join(File.dirname(__FILE__), 'ui', 'controllers')
    helper_path = File.join(File.dirname(__FILE__), 'ui', 'helpers')
    $LOAD_PATH << controller_path
    $LOAD_PATH << helper_path
    Dependencies.load_paths << controller_path
    Dependencies.load_paths << helper_path
    config.controller_paths << controller_path
    require 'newrelic_routing'
    
    # inform user that the dev edition is available if we are running inside
    # a webserver process
    if agent.local_port
      to_stderr "NewRelic Agent (Developer Mode) enabled."
      to_stderr "To view performance information, go to http://localhost:#{agent.local_port}/newrelic"
    end
  end
end
rescue Exception => e
  to_stderr "Error initializing New Relic plugin"
  to_stderr "#{e}"
  to_stderr e.backtrace.join("\n")
  to_stderr "Agent is disabled."
end
Something went wrong with that request. Please try again.