Module: NewRelic::Control::Instrumentation

Included in:
NewRelic::Control
Defined in:
lib/new_relic/control/instrumentation.rb

Instance Method Summary collapse

Instance Method Details

#_delayed_instrumentationObject



36
37
38
39
40
41
42
# File 'lib/new_relic/control/instrumentation.rb', line 36

def _delayed_instrumentation
  Rails.configuration.after_initialize do
    _install_instrumentation
  end
rescue
  _install_instrumentation
end

#add_instrumentation(pattern) ⇒ Object

Add instrumentation. Don’t call this directly. Use NewRelic::Agent#add_instrumentation. This will load the file synchronously if we’ve already loaded the default instrumentation.



28
29
30
31
32
33
34
# File 'lib/new_relic/control/instrumentation.rb', line 28

def add_instrumentation pattern
  if @instrumented
    load_instrumentation_files pattern
  else
    @instrumentation_files << pattern
  end
end

#install_instrumentationObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/new_relic/control/instrumentation.rb', line 44

def install_instrumentation
  if defined?(Rails) && !Rails.initialized?
    _delayed_instrumentation
  else
    _install_instrumentation
  end
rescue NameError
  # needed in the rails 3 case, where Rails.initialized? raises
  # an error if rails has not been initialised. which is totally sane.
  _delayed_instrumentation
end

#install_shimObject

Install stubs to the proper location so the app code will not fail if the agent is not running.



18
19
20
21
22
# File 'lib/new_relic/control/instrumentation.rb', line 18

def install_shim
  # Once we install instrumentation, you can't undo that by installing the shim.
  raise "Cannot install the Agent shim after instrumentation has already been installed!" if @instrumented
  NewRelic::Agent.agent = NewRelic::Agent::ShimAgent.instance
end

#load_instrumentation_files(pattern) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/new_relic/control/instrumentation.rb', line 4

def load_instrumentation_files pattern
  Dir.glob(pattern) do |file|
    begin
      log.debug "Processing instrumentation file '#{file}'"
      require file
    rescue => e
      log.error "Error loading instrumentation file '#{file}': #{e}"
      log.debug e.backtrace.join("\n")
    end
  end
end

#load_samplersObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/new_relic/control/instrumentation.rb', line 56

def load_samplers
  agent = NewRelic::Agent.instance
  NewRelic::Agent::Sampler.sampler_classes.each do | subclass |
    begin
      log.debug "#{subclass.name} not supported on this platform." and next if not subclass.supported_on_this_platform?
      sampler = subclass.new
      if subclass.use_harvest_sampler?
        agent.stats_engine.add_harvest_sampler sampler
        log.debug "Registered #{subclass.name} for harvest time sampling"
      else
        agent.stats_engine.add_sampler sampler
        log.debug "Registered #{subclass.name} for periodic sampling"
      end
    rescue NewRelic::Agent::Sampler::Unsupported => e
      log.info "#{subclass} sampler not available: #{e}"
    rescue => e
      log.error "Error registering sampler: #{e}, #{e.backtrace.join("\n")}"
    end
  end
end