Class: NewRelic::LocalEnvironment
- Defined in:
- lib/new_relic/local_environment.rb
Overview
An instance of LocalEnvironment is responsible for determining three things:
-
Framework - :rails, :merb, :ruby, :external, :test
-
Dispatcher - A supported dispatcher, or nil (:mongrel, :thin, :passenger, :webrick, etc)
-
Dispatcher Instance ID, which distinguishes agents on a single host from each other
If the environment can’t be determined, it will be set to nil and dispatcher_instance_id will have nil.
NewRelic::LocalEnvironment should be accessed through NewRelic::Control#env (via the NewRelic::Control singleton).
Instance Attribute Summary collapse
-
#delayed_worker ⇒ Object
Returns the value of attribute delayed_worker.
-
#dispatcher ⇒ Object
(also: #environment)
mongrel, thin, webrick, or possibly nil.
-
#dispatcher_instance_id ⇒ Object
used to distinguish instances of a dispatcher from each other, may be nil.
-
#framework ⇒ Object
rails, merb, external, ruby, test.
-
#mongrel ⇒ Object
readonly
The mongrel instance, if there is one, captured as a convenience.
-
#processors ⇒ Object
readonly
The number of cpus, if detected, or nil.
Instance Method Summary collapse
-
#append_environment_value(name, value = nil) ⇒ Object
Add the given key/value pair to the app environment settings.
- #append_gem_list ⇒ Object
- #append_plugin_list ⇒ Object
-
#gather_environment_info ⇒ Object
Collect base statistics about the environment and record them for comparison and change detection.
-
#initialize ⇒ LocalEnvironment
constructor
A new instance of LocalEnvironment.
-
#snapshot ⇒ Object
Take a snapshot of the environment information for this application Returns an associative array.
- #to_s ⇒ Object
- #unicorn ⇒ Object
Constructor Details
#initialize ⇒ LocalEnvironment
Returns a new instance of LocalEnvironment.
25 26 27 28 29 30 31 32 33 |
# File 'lib/new_relic/local_environment.rb', line 25 def initialize discover_framework discover_dispatcher duck_punch_delayed_worker if defined? Delayed::Worker @dispatcher = nil if @dispatcher == :none @gems = Set.new @plugins = Set.new @config = Hash.new end |
Instance Attribute Details
#delayed_worker ⇒ Object
Returns the value of attribute delayed_worker.
20 21 22 |
# File 'lib/new_relic/local_environment.rb', line 20 def delayed_worker @delayed_worker end |
#dispatcher ⇒ Object Also known as: environment
mongrel, thin, webrick, or possibly nil
17 18 19 |
# File 'lib/new_relic/local_environment.rb', line 17 def dispatcher @dispatcher end |
#dispatcher_instance_id ⇒ Object
used to distinguish instances of a dispatcher from each other, may be nil
18 19 20 |
# File 'lib/new_relic/local_environment.rb', line 18 def dispatcher_instance_id @dispatcher_instance_id end |
#framework ⇒ Object
rails, merb, external, ruby, test
19 20 21 |
# File 'lib/new_relic/local_environment.rb', line 19 def framework @framework end |
#mongrel ⇒ Object (readonly)
The mongrel instance, if there is one, captured as a convenience
21 22 23 |
# File 'lib/new_relic/local_environment.rb', line 21 def mongrel @mongrel end |
#processors ⇒ Object (readonly)
The number of cpus, if detected, or nil
22 23 24 |
# File 'lib/new_relic/local_environment.rb', line 22 def processors @processors end |
Instance Method Details
#append_environment_value(name, value = nil) ⇒ Object
Add the given key/value pair to the app environment settings. Must pass either a value or a block. Block is called to get the value and any raised errors are silently ignored.
39 40 41 42 43 44 45 |
# File 'lib/new_relic/local_environment.rb', line 39 def append_environment_value name, value = nil value = yield if block_given? @config[name] = value if value rescue Exception # puts "#{e}\n #{e.backtrace.join("\n ")}" raise if @framework == :test end |
#append_gem_list ⇒ Object
47 48 49 50 51 52 |
# File 'lib/new_relic/local_environment.rb', line 47 def append_gem_list @gems += yield rescue Exception => e # puts "#{e}\n #{e.backtrace.join("\n ")}" raise if @framework == :test end |
#append_plugin_list ⇒ Object
54 55 56 57 58 59 |
# File 'lib/new_relic/local_environment.rb', line 54 def append_plugin_list @plugins += yield rescue Exception # puts "#{e}\n #{e.backtrace.join("\n ")}" raise if @framework == :test end |
#gather_environment_info ⇒ Object
Collect base statistics about the environment and record them for comparison and change detection.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/new_relic/local_environment.rb', line 72 def gather_environment_info append_environment_value 'Framework', @framework.to_s append_environment_value 'Dispatcher', @dispatcher.to_s if @dispatcher append_environment_value 'Dispatcher instance id', @dispatcher_instance_id if @dispatcher_instance_id append_environment_value('Application root') { File.(NewRelic::Control.instance.root) } append_environment_value('Ruby version'){ RUBY_VERSION } append_environment_value('Ruby description'){ RUBY_DESCRIPTION } if defined? RUBY_DESCRIPTION append_environment_value('Ruby platform') { RUBY_PLATFORM } append_environment_value('Ruby patchlevel') { RUBY_PATCHLEVEL } if defined? ::JRUBY_VERSION append_environment_value('JRuby version') { JRUBY_VERSION } append_environment_value('Java VM version') { ENV_JAVA['java.vm.version']} end append_environment_value('OS version') { `uname -v` } append_environment_value('OS') { `uname -s` } || append_environment_value('OS') { ENV['OS'] } append_environment_value('Arch') { `uname -p` } || append_environment_value('Arch') { ENV['PROCESSOR_ARCHITECTURE'] } # See what the number of cpus is, works only on linux. @processors = append_environment_value('Processors') do processors = File.readlines('/proc/cpuinfo').select { |line| line =~ /^processor\s*:/ }.size raise "Cannot determine the number of processors in /proc/cpuinfo" unless processors > 0 processors end if File.readable? '/proc/cpuinfo' # The current Rails environment (development, test, or production). append_environment_value('Environment') { NewRelic::Control.instance.env } # Look for a capistrano file indicating the current revision: rev_file = File.join(NewRelic::Control.instance.root, "REVISION") if File.readable?(rev_file) && File.size(rev_file) < 64 append_environment_value('Revision') do File.open(rev_file) { | file | file.readline.strip } end end # The name of the database adapter for the current environment. if defined? ActiveRecord append_environment_value 'Database adapter' do ActiveRecord::Base.configurations[RAILS_ENV]['adapter'] end append_environment_value 'Database schema version' do ActiveRecord::Migrator.current_version end end if defined? DataMapper append_environment_value 'DataMapper version' do require 'dm-core/version' DataMapper::VERSION end end end |
#snapshot ⇒ Object
Take a snapshot of the environment information for this application Returns an associative array
123 124 125 126 127 128 |
# File 'lib/new_relic/local_environment.rb', line 123 def snapshot i = @config.to_a i << [ 'Plugin List', @plugins.to_a] if not @plugins.empty? i << [ 'Gems', @gems.to_a] if not @gems.empty? i end |
#to_s ⇒ Object
319 320 321 322 323 324 325 |
# File 'lib/new_relic/local_environment.rb', line 319 def to_s s = "LocalEnvironment[" s << @framework.to_s s << ";dispatcher=#{@dispatcher}" if @dispatcher s << ";instance=#{@dispatcher_instance_id}" if @dispatcher_instance_id s << "]" end |
#unicorn ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/new_relic/local_environment.rb', line 138 def unicorn return @unicorn if @unicorn || ! defined? Unicorn::HttpServer ObjectSpace.each_object(Unicorn::HttpServer) do |unicorn| @unicorn = unicorn end unless defined?(JRuby) && !JRuby.runtime.is_object_space_enabled @unicorn end |