lib/elastic_apm/agent.rb in elastic-apm-0.4.0 vs lib/elastic_apm/agent.rb in elastic-apm-0.4.1

- old
+ new

@@ -21,19 +21,30 @@ def self.instance # rubocop:disable Style/TrivialAccessors @instance end + # rubocop:disable Metrics/MethodLength def self.start(config) return @instance if @instance + config = Config.new(config) if config.is_a?(Hash) + + unless config.enabled_environments.include?(config.environment) + config.logger && config.logger.info( + format('Not tracking anything in "%s" env', config.environment) + ) + return + end + LOCK.synchronize do return @instance if @instance @instance = new(config.freeze).start end end + # rubocop:enable Metrics/MethodLength def self.stop LOCK.synchronize do return unless @instance @@ -44,14 +55,11 @@ def self.running? !!@instance end - # rubocop:disable Metrics/MethodLength def initialize(config) - config = Config.new(config) if config.is_a?(Hash) - @config = config @http = Http.new(config) @queue = Queue.new @@ -62,31 +70,26 @@ @serializers = Struct.new(:transactions, :errors).new( Serializers::Transactions.new(config), Serializers::Errors.new(config) ) end - # rubocop:enable Metrics/MethodLength attr_reader :config, :queue, :instrumenter, :context_builder, :http def start - debug 'Starting agent reporting to %s', config.server_url + debug '[%s] Starting agent, reporting to %s', VERSION, config.server_url @instrumenter.start - boot_worker - config.enabled_injectors.each do |lib| require "elastic_apm/injectors/#{lib}" end self end def stop - debug 'Stopping agent' - @instrumenter.stop kill_worker self @@ -95,16 +98,20 @@ at_exit do stop end def enqueue_transactions(transactions) + boot_worker unless worker_running? + data = @serializers.transactions.build_all(transactions) @queue << Worker::Request.new('/v1/transactions', data) transactions end def enqueue_errors(errors) + boot_worker unless worker_running? + data = @serializers.errors.build_all(errors) @queue << Worker::Request.new('/v1/errors', data) errors end @@ -127,10 +134,12 @@ end # errors def report(exception, handled: true) + return if config.filter_exception_types.include?(exception.class.to_s) + error = @error_builder.build_exception( exception, handled: handled ) enqueue_errors error @@ -168,26 +177,28 @@ end private def boot_worker - debug 'Booting worker in thread' + debug 'Booting worker' @worker_thread = Thread.new do Worker.new(@config, @queue, @http).run_forever end end def kill_worker @queue << Worker::StopMessage.new - unless @worker_thread.join(5) # 5 secs + if @worker_thread && !@worker_thread.join(5) # 5 secs raise 'Failed to wait for worker, not all messages sent' end @worker_thread = nil + end - debug 'Killed worker' + def worker_running? + @worker_thread && @worker_thread.alive? end end # rubocop:enable Metrics/ClassLength end