Class: NewRelic::LocalEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/local_environment.rb

Overview

An instance of LocalEnvironment is responsible for determining three things:

  • Framework - :rails, :rails3, :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

Instance Method Summary collapse

Constructor Details

#initializeLocalEnvironment

Returns a new instance of LocalEnvironment.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/new_relic/local_environment.rb', line 25

def initialize
  # Extend self with any any submodules of LocalEnvironment.  These can override
  # the discover methods to discover new framworks and dispatchers.
  NewRelic::LocalEnvironment.constants.each do | const |
    mod = NewRelic::LocalEnvironment.const_get const
    self.extend mod if mod.instance_of? Module
  end

  discover_framework
  discover_dispatcher
  @dispatcher = nil if @dispatcher == :none
  @gems = Set.new
  @plugins = Set.new
  @config = Hash.new
end

Instance Attribute Details

#dispatcherObject Also known as: environment

mongrel, thin, webrick, or possibly nil



18
19
20
# File 'lib/new_relic/local_environment.rb', line 18

def dispatcher
  @dispatcher
end

#dispatcher_instance_idObject

used to distinguish instances of a dispatcher from each other, may be nil



19
20
21
# File 'lib/new_relic/local_environment.rb', line 19

def dispatcher_instance_id
  @dispatcher_instance_id
end

#frameworkObject

rails, rails3, merb, external, ruby, test



20
21
22
# File 'lib/new_relic/local_environment.rb', line 20

def framework
  @framework
end

#mongrelObject (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

#processorsObject (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.



45
46
47
48
49
50
51
# File 'lib/new_relic/local_environment.rb', line 45

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_listObject



53
54
55
56
57
58
# File 'lib/new_relic/local_environment.rb', line 53

def append_gem_list
  @gems += yield
rescue Exception => e
  # puts "#{e}\n  #{e.backtrace.join("\n  ")}"
  raise if @framework == :test
end

#append_plugin_listObject



60
61
62
63
64
65
# File 'lib/new_relic/local_environment.rb', line 60

def append_plugin_list
  @plugins += yield
rescue Exception
  # puts "#{e}\n  #{e.backtrace.join("\n  ")}"
  raise if @framework == :test
end

#delayed_worker=(worker) ⇒ Object

Obsolete method for DelayedJob instrumentation support. Now all DJ instrumentation is bundled in the newrelic_rpm gem and nobody should be invoking this method.



157
158
159
# File 'lib/new_relic/local_environment.rb', line 157

def delayed_worker=(worker)
  $stderr.puts "WARNING: obsolete call to delayed_worker=(worker).  Please remove custom DJ instrumentation."
end

#gather_environment_infoObject

Collect base statistics about the environment and record them for comparison and change detection.



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
121
122
123
124
125
126
127
# File 'lib/new_relic/local_environment.rb', line 78

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
  # This just creates a lot of keys
  # append_environment_value('Application root') { File.expand_path(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

#snapshotObject

Take a snapshot of the environment information for this application Returns an associative array



130
131
132
133
134
135
# File 'lib/new_relic/local_environment.rb', line 130

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_sObject



324
325
326
327
328
329
330
# File 'lib/new_relic/local_environment.rb', line 324

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

#unicornObject



147
148
149
150
151
152
153
# File 'lib/new_relic/local_environment.rb', line 147

def unicorn
  return @unicorn if @unicorn || ! (defined?(::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