Module: NewRelic::Control::Instrumentation

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

Overview

Contains methods that relate to adding and executing files that contain instrumentation for the Ruby Agent

Instance Method Summary collapse

Instance Method Details

#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, otherwise instrumentation files specified here will be deferred until all instrumentation is run

This happens after the agent has loaded and all dependencies are ready to be instrumented



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

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

#install_instrumentationObject

Signals the agent that it’s time to actually load the instrumentation files. May be overridden by subclasses



47
48
49
# File 'lib/new_relic/control/instrumentation.rb', line 47

def install_instrumentation
  _install_instrumentation
end

#install_shimObject

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



24
25
26
27
28
# File 'lib/new_relic/control/instrumentation.rb', line 24

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

Adds a list of files in Dir.glob format (e.g. ‘/app/foo/*/_instrumentation.rb’) This requires the files within a rescue block, so that any errors within instrumentation files do not affect the overall agent or application in which it runs.



12
13
14
15
16
17
18
19
20
# File 'lib/new_relic/control/instrumentation.rb', line 12

def load_instrumentation_files pattern
  Dir.glob(pattern) do |file|
    begin
      require file.to_s
    rescue => e
      ::NewRelic::Agent.logger.warn "Error loading instrumentation file '#{file}':", e
    end
  end
end

#load_samplersObject

adds samplers to the stats engine so that they run every minute. This is dynamically recognized by any class that subclasses NewRelic::Agent::Sampler



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

def load_samplers
  agent = NewRelic::Agent.instance
  NewRelic::Agent::Sampler.sampler_classes.each do | subclass |
    begin
      ::NewRelic::Agent.logger.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
        ::NewRelic::Agent.logger.debug "Registered #{subclass.name} for harvest time sampling"
      else
        agent.stats_engine.add_sampler sampler
        ::NewRelic::Agent.logger.debug "Registered #{subclass.name} for periodic sampling"
      end
    rescue NewRelic::Agent::Sampler::Unsupported => e
      ::NewRelic::Agent.logger.info "#{subclass} sampler not available: #{e}"
    rescue => e
      ::NewRelic::Agent.logger.error "Error registering sampler:", e
    end
  end
end