Sha256: ef58dc63269f5bebddd145ed2f573c2eda6c8f860ce609c7f679cf6cb4215e6b

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module ElasticAPM
  module Transport
    # @api private
    class Worker
      include Logging

      class << self
        def adapter
          @adapter ||= Connection
        end

        attr_writer :adapter
      end

      # @api private
      class StopMessage; end

      # @api private
      class FlushMessage; end

      def initialize(
        config,
        queue,
        serializers:,
        filters:
      )
        @config = config
        @queue = queue

        @serializers = serializers
        @filters = filters

        @connection = self.class.adapter.new(config)
      end

      attr_reader :queue, :filters, :name, :connection, :serializers
      def work_forever
        while (msg = queue.pop)
          case msg
          when StopMessage
            debug 'Stopping worker -- %s', self
            connection.flush(:halt)
            break
          else
            process msg
          end
        end
      rescue Exception => e
        warn 'Worker died with exception: %s', e.inspect
        debug e.backtrace.join("\n")
      end

      def process(resource)
        return unless (json = serialize_and_filter(resource))
        connection.write(json)
      end

      private

      def serialize_and_filter(resource)
        serialized = serializers.serialize(resource)

        # if a filter returns nil, it means skip the event
        return nil if @filters.apply!(serialized) == Filters::SKIP

        JSON.fast_generate(serialized)
      rescue Exception
        error format('Failed converting event to JSON: %s', resource.inspect)
        error serialized.inspect
        nil
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
elastic-apm-3.6.0 lib/elastic_apm/transport/worker.rb
elastic-apm-3.5.0 lib/elastic_apm/transport/worker.rb
elastic-apm-3.4.0 lib/elastic_apm/transport/worker.rb
elastic-apm-3.3.0 lib/elastic_apm/transport/worker.rb