Skip to content

Commit 6d90033

Browse files
ErvalhouSpicandocodigo
authored andcommitted
Adds transaction outcomes to Racecar transactions
1 parent 03646cd commit 6d90033

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

lib/elastic_apm/spies/racecar.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,24 @@ class ConsumerSubscriber < ActiveSupport::Subscriber
3333
def start_process_message(event)
3434
start_process_transaction(event: event, kind: 'process_message')
3535
end
36-
def process_message(_event)
36+
def process_message(event)
37+
transaction = ElasticAPM.current_transaction
38+
error_present = !event.payload[:unrecoverable_delivery_error].nil?
39+
transaction.outcome = error_present ? Transaction::Outcome::FAILURE : Transaction::Outcome::SUCCESS
40+
transaction.done(error_present ? :error : :success)
41+
3742
ElasticAPM.end_transaction
3843
end
3944

4045
def start_process_batch(event)
4146
start_process_transaction(event: event, kind: 'process_batch')
4247
end
43-
def process_batch(_event)
48+
def process_batch(event)
49+
transaction = ElasticAPM.current_transaction
50+
error_present = !event.payload[:unrecoverable_delivery_error].nil?
51+
transaction.outcome = error_present ? Transaction::Outcome::FAILURE : Transaction::Outcome::SUCCESS
52+
transaction.done(error_present ? :error : :success)
53+
4454
ElasticAPM.end_transaction
4555
end
4656

spec/elastic_apm/spies/racecar_spec.rb

+45-10
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@
2424
require 'racecar'
2525
module ElasticAPM
2626
RSpec.describe 'Spy: Racecar', :intercept do
27+
let(:instrumentation_payload) {
28+
{ consumer_class: 'SpecConsumer',
29+
topic: 'spec_topic',
30+
partition: '0',
31+
offset: '1',
32+
create_time: Time.now,
33+
key: '1',
34+
value: {key: 'value'},
35+
headers: {key: 'value'} }
36+
}
2737
it 'captures the instrumentation' do
2838
with_agent do
29-
instrumentation_payload = {
30-
consumer_class: 'SpecConsumer',
31-
topic: 'spec_topic',
32-
partition: '0',
33-
offset: '1',
34-
create_time: Time.now,
35-
key: '1',
36-
value: {key: 'value'},
37-
headers: {key: 'value'}
38-
}
3939
ActiveSupport::Notifications.instrument('start_process_message.racecar', instrumentation_payload)
4040
ActiveSupport::Notifications.instrument('process_message.racecar', instrumentation_payload) do
4141
# this is the body of the racecar consumer #process method
@@ -47,6 +47,41 @@ module ElasticAPM
4747
expect(first_transaction.context.service.framework.name).to eq('racecar')
4848
end
4949
end
50+
51+
describe 'transaction outcome' do
52+
it 'records success when no delivery error happen' do
53+
with_agent do
54+
ActiveSupport::Notifications.instrument('start_process_message.racecar', instrumentation_payload)
55+
ActiveSupport::Notifications.instrument('process_message.racecar', instrumentation_payload) do
56+
# this is the body of the racecar consumer #process method
57+
end
58+
first_transaction = @intercepted.transactions.first
59+
expect(first_transaction.outcome).to eq(Transaction::Outcome::SUCCESS)
60+
end
61+
end
62+
it 'records failure when with a unrecoverable delivery error' do
63+
instrumentation_payload[:unrecoverable_delivery_error] = true
64+
with_agent do
65+
ActiveSupport::Notifications.instrument('start_process_message.racecar', instrumentation_payload)
66+
ActiveSupport::Notifications.instrument('process_message.racecar', instrumentation_payload) do
67+
# this is the body of the racecar consumer #process method
68+
end
69+
first_transaction = @intercepted.transactions.first
70+
expect(first_transaction.outcome).to eq(Transaction::Outcome::FAILURE)
71+
end
72+
end
73+
it 'records failure when with a recoverable delivery error' do
74+
instrumentation_payload[:unrecoverable_delivery_error] = false
75+
with_agent do
76+
ActiveSupport::Notifications.instrument('start_process_message.racecar', instrumentation_payload)
77+
ActiveSupport::Notifications.instrument('process_message.racecar', instrumentation_payload) do
78+
# this is the body of the racecar consumer #process method
79+
end
80+
first_transaction = @intercepted.transactions.first
81+
expect(first_transaction.outcome).to eq(Transaction::Outcome::FAILURE)
82+
end
83+
end
84+
end
5085
end
5186
end
5287

0 commit comments

Comments
 (0)