Module: NewRelic::Agent::Agent::InstanceMethods::Connect
- Included in:
- NewRelic::Agent::Agent::InstanceMethods
- Defined in:
- lib/new_relic/agent/agent.rb
Overview
This module is an artifact of a refactoring of the connect method - all of its methods are used in that context, so it can be refactored at will. It should be fully tested
Instance Attribute Summary collapse
-
#connect_attempts ⇒ Object
number of attempts we’ve made to contact the server.
Instance Method Summary collapse
- #add_rules_to_engine(rule_specifications, rules_engine) ⇒ Object
-
#apdex_f ⇒ Object
apdex_f is always 4 times the apdex_t.
-
#connect_retry_period ⇒ Object
Retry period is a minute for each failed attempt that we’ve made.
-
#connect_settings ⇒ Object
Initializes the hash of settings that we send to the server.
-
#connect_to_server ⇒ Object
Returns connect data passed back from the server.
- #connected? ⇒ Boolean
-
#disconnect ⇒ Object
Disconnect just sets connected to false, which prevents the agent from trying to connect again.
- #disconnected? ⇒ Boolean
-
#environment_for_connect ⇒ Object
Checks whether we should send environment info, and if so, returns the snapshot from the local environment.
-
#finish_setup(config_data) ⇒ Object
Takes a hash of configuration data returned from the server and uses it to set local variables and to initialize various parts of the agent that are configured separately.
- #generate_environment_report ⇒ Object
-
#handle_license_error(error) ⇒ Object
When the server sends us an error with the license key, we want to tell the user that something went wrong, and let them know where to go to get a valid license key.
- #handle_unrecoverable_agent_error(error) ⇒ Object
- #log_collector_messages(messages) ⇒ Object
-
#log_connection!(config_data) ⇒ Object
Logs when we connect to the server, for debugging purposes - makes sure we know if an agent has not connected.
-
#log_error(error) ⇒ Object
When we have a problem connecting to the server, we need to tell the user what happened, since this is not an error we can handle gracefully.
- #note_connect_failure ⇒ Object
-
#query_server_for_configuration ⇒ Object
Sets the collector host and connects to the server, then invokes the final configuration with the returned data.
-
#should_connect?(force = false) ⇒ Boolean
Don’t connect if we’re already connected, or if we tried to connect and were rejected with prejudice because of a license issue, unless we’re forced to by force_reconnect.
Instance Attribute Details
#connect_attempts ⇒ Object
number of attempts we’ve made to contact the server
695 696 697 |
# File 'lib/new_relic/agent/agent.rb', line 695 def connect_attempts @connect_attempts end |
Instance Method Details
#add_rules_to_engine(rule_specifications, rules_engine) ⇒ Object
833 834 835 836 837 838 |
# File 'lib/new_relic/agent/agent.rb', line 833 def add_rules_to_engine(rule_specifications, rules_engine) return unless rule_specifications && rule_specifications.any? rule_specifications.each do |rule_spec| rules_engine << NewRelic::Agent::RulesEngine::Rule.new(rule_spec) end end |
#apdex_f ⇒ Object
apdex_f is always 4 times the apdex_t
791 792 793 |
# File 'lib/new_relic/agent/agent.rb', line 791 def apdex_f (4 * Agent.config[:apdex_t]).to_f end |
#connect_retry_period ⇒ Object
Retry period is a minute for each failed attempt that we’ve made. This should probably do some sort of sane TCP backoff to prevent hammering the server, but a minute for each attempt seems to work reasonably well.
723 724 725 |
# File 'lib/new_relic/agent/agent.rb', line 723 def connect_retry_period [600, connect_attempts * 60].min end |
#connect_settings ⇒ Object
Initializes the hash of settings that we send to the server. Returns a literal hash containing the options
773 774 775 776 777 778 779 780 781 782 783 |
# File 'lib/new_relic/agent/agent.rb', line 773 def connect_settings { :pid => $$, :host => @local_host, :app_name => Agent.config.app_names, :language => 'ruby', :agent_version => NewRelic::VERSION::STRING, :environment => @environment_report, :settings => Agent.config.to_collector_hash, } end |
#connect_to_server ⇒ Object
Returns connect data passed back from the server
786 787 788 |
# File 'lib/new_relic/agent/agent.rb', line 786 def connect_to_server @service.connect(connect_settings) end |
#connected? ⇒ Boolean
704 705 706 |
# File 'lib/new_relic/agent/agent.rb', line 704 def connected? @connect_state == :connected end |
#disconnect ⇒ Object
Disconnect just sets connected to false, which prevents the agent from trying to connect again
699 700 701 702 |
# File 'lib/new_relic/agent/agent.rb', line 699 def disconnect @connect_state = :disconnected true end |
#disconnected? ⇒ Boolean
708 709 710 |
# File 'lib/new_relic/agent/agent.rb', line 708 def disconnected? @connect_state == :disconnected end |
#environment_for_connect ⇒ Object
Checks whether we should send environment info, and if so, returns the snapshot from the local environment. Generating the EnvironmentReport has the potential to trigger require calls in Rails environments, so this method should only be called synchronously from on the main thread.
767 768 769 |
# File 'lib/new_relic/agent/agent.rb', line 767 def environment_for_connect Agent.config[:send_environment_info] ? Array(EnvironmentReport.new) : [] end |
#finish_setup(config_data) ⇒ Object
Takes a hash of configuration data returned from the server and uses it to set local variables and to initialize various parts of the agent that are configured separately.
Can accommodate most arbitrary data - anything extra is ignored unless we say to do something with it here.
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'lib/new_relic/agent/agent.rb', line 808 def finish_setup(config_data) return if config_data == nil @service.agent_id = config_data['agent_run_id'] if @service if config_data['agent_config'] ::NewRelic::Agent.logger.debug "Using config from server" end ::NewRelic::Agent.logger.debug "Server provided config: #{config_data.inspect}" server_config = NewRelic::Agent::Configuration::ServerSource.new(config_data, Agent.config) Agent.config.apply_config(server_config, 1) log_connection!(config_data) if @service add_rules_to_engine(config_data['transaction_name_rules'], NewRelic::Agent.instance.transaction_rules) add_rules_to_engine(config_data['metric_name_rules'], NewRelic::Agent.instance.metric_rules) # If you're adding something else here to respond to the server-side config, # use Agent.instance.events.subscribe(:finished_configuring) callback instead! @beacon_configuration = BeaconConfiguration.new end |
#generate_environment_report ⇒ Object
758 759 760 |
# File 'lib/new_relic/agent/agent.rb', line 758 def generate_environment_report @environment_report = environment_for_connect end |
#handle_license_error(error) ⇒ Object
When the server sends us an error with the license key, we want to tell the user that something went wrong, and let them know where to go to get a valid license key
After this runs, it disconnects the agent so that it will no longer try to connect to the server, saving the application and the server load
745 746 747 748 749 750 |
# File 'lib/new_relic/agent/agent.rb', line 745 def handle_license_error(error) ::NewRelic::Agent.logger.error( \ error., \ "Visit NewRelic.com to obtain a valid license key, or to upgrade your account.") disconnect end |
#handle_unrecoverable_agent_error(error) ⇒ Object
752 753 754 755 756 |
# File 'lib/new_relic/agent/agent.rb', line 752 def handle_unrecoverable_agent_error(error) ::NewRelic::Agent.logger.error(error.) disconnect shutdown end |
#log_collector_messages(messages) ⇒ Object
851 852 853 854 855 |
# File 'lib/new_relic/agent/agent.rb', line 851 def () .each do || ::NewRelic::Agent.logger.send(['level'].downcase, ['message']) end end |
#log_connection!(config_data) ⇒ Object
Logs when we connect to the server, for debugging purposes
-
makes sure we know if an agent has not connected
842 843 844 845 846 847 848 849 |
# File 'lib/new_relic/agent/agent.rb', line 842 def log_connection!(config_data) ::NewRelic::Agent.logger.debug "Connected to NewRelic Service at #{@service.collector.name}" ::NewRelic::Agent.logger.debug "Agent Run = #{@service.agent_id}." ::NewRelic::Agent.logger.debug "Connection data = #{config_data.inspect}" if config_data['messages'] && config_data['messages'].any? (config_data['messages']) end end |
#log_error(error) ⇒ Object
When we have a problem connecting to the server, we need to tell the user what happened, since this is not an error we can handle gracefully.
734 735 736 |
# File 'lib/new_relic/agent/agent.rb', line 734 def log_error(error) ::NewRelic::Agent.logger.error "Error establishing connection with New Relic Service at #{control.server}:", error end |
#note_connect_failure ⇒ Object
727 728 729 |
# File 'lib/new_relic/agent/agent.rb', line 727 def note_connect_failure self.connect_attempts += 1 end |
#query_server_for_configuration ⇒ Object
Sets the collector host and connects to the server, then invokes the final configuration with the returned data
797 798 799 |
# File 'lib/new_relic/agent/agent.rb', line 797 def query_server_for_configuration finish_setup(connect_to_server) end |
#should_connect?(force = false) ⇒ Boolean
Don’t connect if we’re already connected, or if we tried to connect and were rejected with prejudice because of a license issue, unless we’re forced to by force_reconnect.
715 716 717 |
# File 'lib/new_relic/agent/agent.rb', line 715 def should_connect?(force=false) force || (!connected? && !disconnected?) end |