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:

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



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

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_dispatcher
end

Instance Attribute Details

#dispatcher_instance_idObject

An instance id pulled from either @dispatcher_instance_id or by splitting out the first part of the running file



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

def dispatcher_instance_id
  if @dispatcher_instance_id.nil?
    if @discovered_dispatcher.nil?
      @dispatcher_instance_id = File.basename($0).split(".").first
    end
  end
  @dispatcher_instance_id
end

Instance Method Details

#discovered_dispatcherObject

mongrel, thin, webrick, or possibly nil



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

def discovered_dispatcher
  discover_dispatcher unless @discovered_dispatcher
  @discovered_dispatcher
end

#find_class_in_object_space(klass) ⇒ Object

Runs through all the objects in ObjectSpace to find the first one that match the provided class



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

def find_class_in_object_space(klass)
  if NewRelic::LanguageSupport.object_space_enabled?
    ObjectSpace.each_object(klass) do |x|
      return x
    end
  end
  return nil
end

#mongrelObject

Sets the @mongrel instance variable if we can find a Mongrel::HttpServer



64
65
66
67
68
69
70
71
# File 'lib/new_relic/local_environment.rb', line 64

def mongrel
  return @mongrel if @looked_for_mongrel
  @looked_for_mongrel = true
  if defined?(::Mongrel) && defined?(::Mongrel::HttpServer)
    @mongrel = find_class_in_object_space(::Mongrel::HttpServer)
  end
  @mongrel
end

#to_sObject

outputs a human-readable description



226
227
228
229
230
231
# File 'lib/new_relic/local_environment.rb', line 226

def to_s
  s = "LocalEnvironment["
  s << ";dispatcher=#{@discovered_dispatcher}" if @discovered_dispatcher
  s << ";instance=#{@dispatcher_instance_id}" if @dispatcher_instance_id
  s << "]"
end