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 the ‘dispatcher’ in use by the current process.

A dispatcher might be a recognized web server such as unicorn or passenger, a background job processor such as resque or sidekiq, or nil for unknown.

If the environment can’t be determined, it will be set to nil.

NewRelic::LocalEnvironment should be accessed through NewRelic::Control#env (via the NewRelic::Control singleton).

Instance Method Summary collapse

Constructor Details

#initializeLocalEnvironment

Returns a new instance of LocalEnvironment.



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

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 Method Details

#discovered_dispatcherObject



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

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



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

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



47
48
49
50
51
52
53
54
# File 'lib/new_relic/local_environment.rb', line 47

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

#mongrel=(m) ⇒ Object

Setter for testing



57
58
59
60
# File 'lib/new_relic/local_environment.rb', line 57

def mongrel=(m)
  @looked_for_mongrel = true
  @mongrel = m
end

#to_sObject

outputs a human-readable description



185
186
187
188
189
# File 'lib/new_relic/local_environment.rb', line 185

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