Skip to content

Commit

Permalink
Merge pull request #2738 from newrelic/update_bunny_instrumentation
Browse files Browse the repository at this point in the history
Add new span attributes for bunny instrumentation
  • Loading branch information
tannalynn authored Jul 10, 2024
2 parents e4417ef + 1827a7a commit 81c7233
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
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

0 comments on commit 81c7233

Please sign in to comment.