# encoding: utf-8 module OneApm module Agent class Agent module Connect attr_accessor :connect_attempts def disconnect @connect_state = :disconnected true end def connected? @connect_state == :connected end def disconnected? @connect_state == :disconnected end def should_connect?(force = false) force || (!connected? && !disconnected?) end def connect_retry_period [600, connect_attempts * 60].min end def note_connect_failure self.connect_attempts += 1 end def log_error(error) OneApm::Manager.logger.error "Error establishing connection with OneApm Service at #{@service.server}:", error end def handle_license_error(error) OneApm::Manager.logger.error( \ error.message, \ "Visit oneapm.com to obtain a valid license key, or to upgrade your account.") disconnect end def handle_unrecoverable_agent_error(error) OneApm::Manager.logger.error(error.message) disconnect shutdown end def generate_environment_report @environment_report = environment_for_connect end def environment_for_connect OneApm::Manager.config[:send_environment_info] ? Array(EnvironmentReport.new) : [] end def sanitize_environment_report if !@service.valid_to_marshal?(@environment_report) @environment_report = [] end end def connect_settings sanitize_environment_report { :pid => $$, :host => local_host, :app_name => OneApm::Manager.config.app_names, :language => 'ruby', :labels => OneApm::Manager.config.parsed_labels, :agent_version => OneApm::VERSION::STRING, :environment => @environment_report, :settings => OneApm::Manager.config.to_notified_hash, :high_security => OneApm::Manager.config[:high_security], :identifier => "ruby:#{local_host}" } end def apdex_f (4 * OneApm::Manager.config[:apdex_t]).to_f end def query_server_for_configuration finish_setup(connect_to_server) end def connect_to_server @service.connect(connect_settings) end def finish_setup(config_data) return if config_data == nil @service.agent_id = config_data['agent_run_id'] if @service OneApm::Manager.logger.debug "Server provided config: #{config_data.inspect}" server_config = OneApm::Configuration::ServerSource.new(config_data, OneApm::Manager.config) OneApm::Manager.config.update_config(server_config) log_connection!(config_data) if @service @transaction_rules = OneApm::Support::RulesEngine.create_transaction_rules(config_data) @stats_engine.metric_rules = OneApm::Support::RulesEngine.create_metric_rules(config_data) end def local_host @local_host ||= OneApm::Agent::Hostname.get end def log_connection!(config_data) OneApm::Manager.logger.debug "Connected to OneApm Service at #{@service.collector.name}" OneApm::Manager.logger.debug "Agent Run = #{@service.agent_id}." OneApm::Manager.logger.debug "Connection data = #{config_data.inspect}" if config_data['messages'] && config_data['messages'].any? log_collector_messages(config_data['messages']) end end def log_collector_messages(messages) messages.each do |message| OneApm::Manager.logger.send(message['level'].downcase, message['message']) end end end end end end