Class: NewRelic::LocalEnvironment

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

Overview

This class is responsible for determining the ‘dispatcher’ in use by the current process. The 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.

Dispatcher detection is best-effort, and serves two purposes:

  1. For some dispatchers, we need to apply specific workarounds in order for the agent to work correctly.

  2. When reading logs, since multiple processes on a given host might write into the same log, it’s useful to be able to identify what kind of process a given PID mapped to.

Overriding the dispatcher is possible via the NEW_RELIC_DISPATCHER environment variable, but this should not generally be necessary unless you’re on a dispatcher that falls into category 1 above, and our detection logic isn’t working correctly.

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

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

Instance Method Summary collapse

Constructor Details

#initializeLocalEnvironment

Returns a new instance of LocalEnvironment.



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

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

  @discovered_dispatcher = nil
  discover_dispatcher
end

Instance Method Details

#discovered_dispatcherObject



31
32
33
34
# File 'lib/new_relic/local_environment.rb', line 31

def discovered_dispatcher
  discover_dispatcher unless @discovered_dispatcher
  @discovered_dispatcher
end

#executableObject



212
213
214
# File 'lib/new_relic/local_environment.rb', line 212

def executable
  File.basename($0)
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



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

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

#to_sObject

outputs a human-readable description



208
209
210
# File 'lib/new_relic/local_environment.rb', line 208

def to_s
  %Q(LocalEnvironment[#{";dispatcher=#{@discovered_dispatcher}" if @discovered_dispatcher}])
end