Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new span attributes for bunny instrumentation #2738

Merged
merged 6 commits into from
Jul 10, 2024
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
14 changes: 14 additions & 0 deletions lib/new_relic/agent/instrumentation/bunny/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def publish_with_tracing(payload, opts = {})
correlation_id: opts[:correlation_id],
exchange_type: type
)
if segment
segment.add_agent_attribute('server.address', channel&.connection&.hostname)
segment.add_agent_attribute('server.port', channel&.connection&.port)
segment.add_agent_attribute('messaging.destination.name', destination) # for produce, this is exchange name
segment.add_agent_attribute('messaging.rabbitmq.destination.routing_key', opts[:routing_key])
end
rescue => e
NewRelic::Agent.logger.error('Error starting message broker segment in Bunny::Exchange#publish', e)
yield
Expand Down Expand Up @@ -94,6 +100,14 @@ def pop_with_tracing
queue_name: name,
start_time: t0
)
if segment
segment.add_agent_attribute('server.address', channel&.connection&.hostname)
segment.add_agent_attribute('server.port', channel&.connection&.port)
segment.add_agent_attribute('messaging.destination.name', name) # for consume, this is queue name
segment.add_agent_attribute('messaging.destination_publish.name', exch_name)
segment.add_agent_attribute('message.queueName', name)
segment.add_agent_attribute('messaging.rabbitmq.destination.routing_key', delivery_info&.routing_key)
end
rescue => e
NewRelic::Agent.logger.error('Error starting message broker segment in Bunny::Queue#pop', e)
else
Expand Down
37 changes: 37 additions & 0 deletions test/multiverse/suites/bunny/bunny_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class BunnyTest < Minitest::Test
end

def teardown
harvest_span_events!
mocha_teardown
NewRelic::Agent.instance.stats_engine.clear_stats
@conn.close
end

Expand Down Expand Up @@ -333,6 +336,40 @@ def test_pop_returning_no_message_doesnt_error
end
end

def test_pop_adds_attributes_to_span
with_queue do |queue|
queue.publish('test_msg', routing_key: queue.name)
in_transaction('test_txn') do |txn|
txn.stubs(:sampled?).returns(true)

queue.pop
end
spans = harvest_span_events!

assert_equal @conn.hostname, spans[1][0][2]['server.address']
assert_equal 5672, spans[1][0][2]['server.port']
assert_equal 'Default', spans[1][0][2]['messaging.destination_publish.name']
assert_equal queue.name, spans[1][0][2]['messaging.destination.name']
assert_equal queue.name, spans[1][0][2]['messaging.rabbitmq.destination.routing_key']
assert_equal queue.name, spans[1][0][2]['message.queueName']
end
end

def test_publish_adds_attributes_to_span
with_queue do |queue|
in_transaction('test_txn') do |txn|
txn.stubs(:sampled?).returns(true)
queue.publish('test_msg', routing_key: queue.name)
end
spans = harvest_span_events!

assert_equal @conn.hostname, spans[1][0][2]['server.address']
assert_equal 5672, spans[1][0][2]['server.port']
assert_equal 'Default', spans[1][0][2]['messaging.destination.name']
assert_equal queue.name, spans[1][0][2]['messaging.rabbitmq.destination.routing_key']
end
end

def test_pop_returning_a_good_message_send_to_an_exchange_we_havent_accessed_doesnt_error
NewRelic::Agent.stubs(:logger).returns(NewRelic::Agent::MemoryLogger.new)

Expand Down
Loading