# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

require_relative 'instrumentation'

module NewRelic::Agent::Instrumentation
  module RdkafkaProducer
    module Prepend
      include NewRelic::Agent::Instrumentation::Rdkafka

      def produce(**kwargs)
        produce_with_new_relic(kwargs) do |headers|
          kwargs[:headers] = headers
          super
        end
      end
    end
  end

  module RdkafkaConsumer
    module Prepend
      include NewRelic::Agent::Instrumentation::Rdkafka

      def each
        super do |message|
          each_with_new_relic(message) do
            yield(message)
          end
        end
      end
    end
  end

  module RdkafkaConfig
    module Prepend
      include NewRelic::Agent::Instrumentation::RdkafkaConfig

      if (defined?(::Rdkafka) && NewRelic::Helper.version_satisfied?(::Rdkafka::VERSION, '>=', '0.16.0')) ||
          NewRelic::Helper.version_satisfied?(RUBY_VERSION, '>=', '2.7.0')
        def producer(**kwargs)
          super.tap do |producer|
            set_nr_config(producer)
          end
        end

        def consumer(**kwargs)
          super.tap do |consumer|
            set_nr_config(consumer)
          end
        end
      else # older versions
        def producer
          super.tap do |producer|
            set_nr_config(producer)
          end
        end

        def consumer
          super.tap do |consumer|
            set_nr_config(consumer)
          end
        end
      end
    end
  end
end
