Sha256: 23d4d3083457d489920925882f07db5d7e5e980caf6baffc602a8b559de17a3b

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module WaterDrop
  module Instrumentation
    # Default listener that hooks up to our instrumentation and uses its events for logging
    # It can be removed/replaced or anything without any harm to the Waterdrop flow
    # @note It is a module as we can use it then as a part of the Karafka framework listener
    #   as well as we can use it standalone
    module Listener
      # Log levels that we use in this particular listener
      USED_LOG_LEVELS = %i[
        info
        error
      ].freeze

      %i[
        sync_producer
        async_producer
      ].each do |producer_type|
        error_name = :"on_#{producer_type}_call_error"
        retry_name = :"on_#{producer_type}_call_retry"

        define_method error_name do |event|
          options = event[:options]
          error = event[:error]
          error "Delivery failure to: #{options} because of #{error}"
        end

        define_method retry_name do |event|
          attempts_count = event[:attempts_count]
          options = event[:options]
          error = event[:error]

          info "Attempt #{attempts_count} of delivery to: #{options} because of #{error}"
        end

        module_function error_name
        module_function retry_name
        public error_name
        public retry_name
      end

      USED_LOG_LEVELS.each do |log_level|
        define_method log_level do |*args|
          WaterDrop.logger.send(log_level, *args)
        end

        module_function log_level
        private_class_method log_level
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
waterdrop-1.2.5 lib/water_drop/instrumentation/listener.rb
waterdrop-1.2.4 lib/water_drop/instrumentation/listener.rb
waterdrop-1.2.3 lib/water_drop/instrumentation/listener.rb
waterdrop-1.2.2 lib/water_drop/instrumentation/listener.rb
waterdrop-1.2.1 lib/water_drop/instrumentation/listener.rb
waterdrop-1.2.0 lib/water_drop/instrumentation/listener.rb
waterdrop-1.2.0.beta1 lib/water_drop/instrumentation/listener.rb