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, :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.



24
25
26
27
28
29
30
31
# File 'lib/new_relic/local_environment.rb', line 24

def initialize
  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



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

def dispatcher
  @dispatcher
end

#dispatcher_instance_idObject

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

#frameworkObject

rails, merb, external, ruby, test



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

def framework
  @framework
end

#mongrelObject (readonly)

The mongrel instance, if there is one, captured as a convenience



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

def mongrel
  @mongrel
end

#processorsObject (readonly)

The number of cpus, if detected, or nil



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

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.



37
38
39
40
41
42
43
# File 'lib/new_relic/local_environment.rb', line 37

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



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

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

#append_plugin_listObject



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

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, which is now in the rpm_contrib gem.



146
147
148
# File 'lib/new_relic/local_environment.rb', line 146

def delayed_worker=(worker)
  $stderr.puts "WARNING: DelayedJob instrumentation has been moved to the rpm_contrib gem."
end

#gather_environment_infoObject

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



70
71
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
# File 'lib/new_relic/local_environment.rb', line 70

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



121
122
123
124
125
126
# File 'lib/new_relic/local_environment.rb', line 121

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



290
291
292
293
294
295
296
# File 'lib/new_relic/local_environment.rb', line 290

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



136
137
138
139
140
141
142
# File 'lib/new_relic/local_environment.rb', line 136

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