Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion instrumentation/rdkafka/Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# SPDX-License-Identifier: Apache-2.0

%w[0.12.0 0.13.0 0.14.0 0.15.0 0.16.0 0.17.0 0.18.0 0.19.0].each do |version|
%w[0.18.0 0.19.0 0.20.0 0.21.0].each do |version|
appraise "rdkafka-#{version}" do
gem 'rdkafka', "~> #{version}"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Rdkafka
class Instrumentation < OpenTelemetry::Instrumentation::Base
compatible do
gem_version = Gem::Version.new(::Rdkafka::VERSION)
Gem::Requirement.new('>= 0.10.0', '< 0.20.0').satisfied_by?(gem_version)
Gem::Requirement.new('>= 0.18.0').satisfied_by?(gem_version)
end

install do |_config|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ module Rdkafka
module Patches
# The Consumer module contains the instrumentation patch for the Consumer class
module Consumer
GETTER = if Gem::Version.new(::Rdkafka::VERSION) >= Gem::Version.new('0.13.0')
Context::Propagation.text_map_getter
else
OpenTelemetry::Common::Propagation.symbol_key_getter
end
GETTER = Context::Propagation.text_map_getter
private_constant :GETTER

def each
Expand Down Expand Up @@ -42,26 +38,30 @@ def each
end
end

def each_batch(max_items: 100, bytes_threshold: Float::INFINITY, timeout_ms: 250, yield_on_error: false, &block)
super do |messages, error|
if messages.empty?
yield messages, error
else
attributes = {
'messaging.system' => 'kafka',
'messaging.destination_kind' => 'topic',
'messaging.kafka.message_count' => messages.size
}
# each_batch method is deleted in rdkafka-ruby-0.20.0
# But, rdkafka-ruby-0.19.x and 0.18.x are still maintained
if Gem::Version.new(::Rdkafka::VERSION) < Gem::Version.new('0.20.0')
def each_batch(max_items: 100, bytes_threshold: Float::INFINITY, timeout_ms: 250, yield_on_error: false, &block)
super do |messages, error|
if messages.empty?
yield messages, error
else
attributes = {
'messaging.system' => 'kafka',
'messaging.destination_kind' => 'topic',
'messaging.kafka.message_count' => messages.size
}

links = messages.map do |message|
trace_context = OpenTelemetry.propagation.extract(message.headers, getter: GETTER)
span_context = OpenTelemetry::Trace.current_span(trace_context).context
OpenTelemetry::Trace::Link.new(span_context) if span_context.valid?
end
links.compact!
links = messages.map do |message|
trace_context = OpenTelemetry.propagation.extract(message.headers, getter: GETTER)
span_context = OpenTelemetry::Trace.current_span(trace_context).context
OpenTelemetry::Trace::Link.new(span_context) if span_context.valid?
end
links.compact!

tracer.in_span('batch process', attributes: attributes, links: links, kind: :consumer) do
yield messages, error
tracer.in_span('batch process', attributes: attributes, links: links, kind: :consumer) do
yield messages, error
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,68 +166,71 @@
end
end

describe '#each_batch' do
it 'traces each_batch call' do
skip "#{Rdkafka::VERSION} is not supported" unless instrumentation.compatible?

rand_hash = SecureRandom.hex(10)
topic_name = "consumer-patch-batch-trace-#{rand_hash}"
config = { 'bootstrap.servers': "#{host}:#{port}" }

producer = Rdkafka::Config.new(config).producer
delivery_handles = []

delivery_handles << producer.produce(
topic: topic_name,
payload: 'wow',
key: 'Key 1'
)

delivery_handles << producer.produce(
topic: topic_name,
payload: 'super',
key: 'Key 2'
)

delivery_handles.each(&:wait)

consumer_config = config.merge(
'group.id': 'me',
'auto.offset.reset': 'smallest' # https://stackoverflow.com/a/51081649
)
consumer = Rdkafka::Config.new(config.merge(consumer_config)).consumer
consumer.subscribe(topic_name)

begin
consumer.each_batch(max_items: 2) do |messages|
raise 'oops' unless messages.empty?
# each_batch method is deleted in rdkafka 0.20.0
if Gem::Version.new(Rdkafka::VERSION) < Gem::Version.new('0.20.0')
describe '#each_batch' do
it 'traces each_batch call' do
skip "#{Rdkafka::VERSION} is not supported" unless instrumentation.compatible?

rand_hash = SecureRandom.hex(10)
topic_name = "consumer-patch-batch-trace-#{rand_hash}"
config = { 'bootstrap.servers': "#{host}:#{port}" }

producer = Rdkafka::Config.new(config).producer
delivery_handles = []

delivery_handles << producer.produce(
topic: topic_name,
payload: 'wow',
key: 'Key 1'
)

delivery_handles << producer.produce(
topic: topic_name,
payload: 'super',
key: 'Key 2'
)

delivery_handles.each(&:wait)

consumer_config = config.merge(
'group.id': 'me',
'auto.offset.reset': 'smallest' # https://stackoverflow.com/a/51081649
)
consumer = Rdkafka::Config.new(config.merge(consumer_config)).consumer
consumer.subscribe(topic_name)

begin
consumer.each_batch(max_items: 2) do |messages|
raise 'oops' unless messages.empty?
end
rescue StandardError
end
rescue StandardError
end

span = spans.find { |s| s.name == 'batch process' }
_(span.kind).must_equal(:consumer)
_(span.attributes['messaging.kafka.message_count']).must_equal(2)

event = span.events.first
_(event.name).must_equal('exception')
_(event.attributes['exception.type']).must_equal('RuntimeError')
_(event.attributes['exception.message']).must_equal('oops')

first_link = span.links[0]
linked_span_context = first_link.span_context
_(linked_span_context.trace_id).must_equal(spans[0].trace_id)
_(linked_span_context.span_id).must_equal(spans[0].span_id)

second_link = span.links[1]
linked_span_context = second_link.span_context
_(linked_span_context.trace_id).must_equal(spans[1].trace_id)
_(linked_span_context.span_id).must_equal(spans[1].span_id)

_(spans.size).must_equal(3)
ensure
begin; producer&.close; rescue StandardError; end
begin; consumer&.close; rescue StandardError; end
span = spans.find { |s| s.name == 'batch process' }
_(span.kind).must_equal(:consumer)
_(span.attributes['messaging.kafka.message_count']).must_equal(2)

event = span.events.first
_(event.name).must_equal('exception')
_(event.attributes['exception.type']).must_equal('RuntimeError')
_(event.attributes['exception.message']).must_equal('oops')

first_link = span.links[0]
linked_span_context = first_link.span_context
_(linked_span_context.trace_id).must_equal(spans[0].trace_id)
_(linked_span_context.span_id).must_equal(spans[0].span_id)

second_link = span.links[1]
linked_span_context = second_link.span_context
_(linked_span_context.trace_id).must_equal(spans[1].trace_id)
_(linked_span_context.span_id).must_equal(spans[1].span_id)

_(spans.size).must_equal(3)
ensure
begin; producer&.close; rescue StandardError; end
begin; consumer&.close; rescue StandardError; end
end
end
end
end
Expand Down
Loading