Class: NewRelic::Plugin::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/newrelic_plugin/run.rb

Overview

Run class. Provides entry points and polling initiation support.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRun

Returns a new instance of Run.



18
19
20
21
22
23
24
# File 'lib/newrelic_plugin/run.rb', line 18

def initialize
  @config = NewRelic::Plugin::Config.config
  @context = NewRelic::Binding::Context.new(
    @config.newrelic['license_key']
  )
  configuration_and_logging
end

Class Method Details

.setup_and_run(component_type_filter = nil) ⇒ Object

Primary Driver entry point



10
11
12
13
14
15
16
17
# File 'lib/newrelic_plugin/run.rb', line 10

def self.setup_and_run component_type_filter = nil
  run = new
  run.setup_from_config component_type_filter
  run.setup_no_config_agents
  run.agent_startup
  run.loop_forever
  run.agent_shutdown
end

Instance Method Details

#agent_setupObject



159
160
161
# File 'lib/newrelic_plugin/run.rb', line 159

def agent_setup
  @agent_setup ||= AgentSetup.new
end

#agent_shutdownObject



152
153
154
155
156
157
# File 'lib/newrelic_plugin/run.rb', line 152

def agent_shutdown
  configured_agents.each do |agent|
    agent.shutdown if agent.respond_to? :shutdown
  end
  PlatformLogger.info("Shutdown complete")
end

#agent_startupObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/newrelic_plugin/run.rb', line 99

def agent_startup
  if configured_agents.size == 0
    err_msg = "No agents configured!"
    err_msg += " Check the agents portion of your yml file." unless NewRelic::Plugin::Config.config.options.empty?
    PlatformLogger.error(err_msg)
    raise NoAgents, err_msg
  end
  installed_agents.each do |agent_id,installed_agent|
    version = installed_agent[:agent_class].version
    PlatformLogger.info("Agent #{installed_agent[:label]} is at version #{version}") if version
  end
  configured_agents.each do |agent|
    agent.startup if agent.respond_to? :startup
  end
end

#configuration_and_loggingObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/newrelic_plugin/run.rb', line 26

def configuration_and_logging
  if @config.newrelic["verbose"].to_i > 0
    NewRelic::PlatformLogger.log_level = ::Logger::DEBUG
  end
  PlatformLogger.info("Using Ruby SDK version: #{NewRelic::Plugin::VERSION}")

  if @config.newrelic['endpoint']
    NewRelic::Binding::Config.endpoint = @config.newrelic['endpoint']
    PlatformLogger.info("Using alternate endpoint: #{NewRelic::Binding::Config.endpoint}")
  end

  unless @config.newrelic['ssl_host_verification'].nil?
    NewRelic::Binding::Config.ssl_host_verification = !!@config.newrelic['ssl_host_verification']
    PlatformLogger.info("Disabling ssl host verification") unless NewRelic::Binding::Config.ssl_host_verification
  end

  if @config.newrelic['proxy']
    NewRelic::Binding::Config.proxy = @config.newrelic['proxy']
    PlatformLogger.info("Using a proxy: #{NewRelic::Binding::Config.proxy.inspect}")
  end

  @poll_cycle_period = (@config.newrelic["poll"] || 60).to_i
  NewRelic::Binding::Config.poll_cycle_period = @poll_cycle_period
  if @poll_cycle_period <= 0
    message = "A poll cycle period less than or equal to 0 is invalid"
    PlatformLogger.fatal(message)
    abort message
  end
  if @poll_cycle_period != 60
    PlatformLogger.warn("WARNING: Poll cycle period differs from 60 seconds (current is #{@poll_cycle_period})")
  end
end

#configured_agentsObject



67
68
69
# File 'lib/newrelic_plugin/run.rb', line 67

def configured_agents
  agent_setup.agents
end

#installed_agentsObject



59
60
61
62
63
64
65
# File 'lib/newrelic_plugin/run.rb', line 59

def installed_agents
  if Setup.installed_agents.size == 0
    PlatformLogger.error("No agents installed!")
    raise NoAgents, "No agents installed"
  end
  Setup.installed_agents
end

#loop_foreverObject

Call this method to loop forever. This will delay an appropriate amount until the next metric pull is needed, then it will loop thru all configured agents and call each one in turn so it can perform it's appropriate metric pull.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/newrelic_plugin/run.rb', line 121

def loop_forever
  @done = false
  begin
    while !@done
      @last_run_time = Time.now
      request = @context.get_request()
      run_agent_data_collection(request)
      request.deliver
      PlatformLogger.info("Gathered #{request.metric_count} statistics from #{request.component_count} components")

      seconds_to_delay = @poll_cycle_period - (Time.now - @last_run_time)
      sleep(seconds_to_delay) if seconds_to_delay > 0
    end
  rescue Interrupt => err
    PlatformLogger.info "Shutting down..."
  end
end

#run_agent_data_collection(request) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/newrelic_plugin/run.rb', line 139

def run_agent_data_collection(request)
  PlatformLogger.debug('Start collecting agent data for poll cycle')
  configured_agents.each do |agent|
    begin
      agent.run(request)
    rescue => err
      PlatformLogger.error("Error occurred in poll cycle: #{err}")
      PlatformLogger.debug("Stacktrace:\n#{err.backtrace.join("\n")}")
    end
  end
  PlatformLogger.debug('Finished collecting agent data for poll cycle')
end

#setup(&block) ⇒ Object



95
96
97
# File 'lib/newrelic_plugin/run.rb', line 95

def setup &block
  block.call(agent_setup)
end

#setup_from_config(component_type_filter = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/newrelic_plugin/run.rb', line 71

def setup_from_config(component_type_filter = nil)
  return unless NewRelic::Plugin::Config.config.agents
  installed_agents.each do |agent_id, installed_agent|
    next if component_type_filter and agent_id != component_type_filter
    config_list = NewRelic::Plugin::Config.config.agents[agent_id.to_s]
    next unless config_list
    [config_list].flatten.each do |config|
      next unless config
      # Convert keys to symbols...
      config.keys.each { |key|config[(key.to_sym rescue key) || key] = config.delete(key) }
      agent_setup.create_agent @context, agent_id, config
    end
  end
end

#setup_no_config_agentsObject

Add an entry for agents that require no configuration (and hence no instances)



88
89
90
91
92
93
94
# File 'lib/newrelic_plugin/run.rb', line 88

def setup_no_config_agents
  installed_agents.each do |agent_id,installed_agent|
    unless installed_agent[:agent_class].config_required?
      agent_setup.create_agent agent_id,installed_agent[:agent_class].label,{}
    end
  end
end