Instrument Action Mailbox processing

Use ActiveSupport::Notifications to instrument Action Mailbox for
logging or performance monitoring.
This commit is contained in:
Lewis Buckley 2022-03-09 10:03:00 +00:00
parent 8516bb6804
commit 599e73bc00
No known key found for this signature in database
GPG Key ID: 4A9328D3A51196CB
3 changed files with 56 additions and 6 deletions

View File

@ -43,6 +43,14 @@ module ActionMailbox
def processed?
delivered? || failed? || bounced?
end
def instrumentation_payload # :nodoc:
{
id: id,
message_id: message_id,
status: status
}
end
end
end

View File

@ -78,14 +78,16 @@ module ActionMailbox
end
def perform_processing # :nodoc:
track_status_of_inbound_email do
run_callbacks :process do
process
ActiveSupport::Notifications.instrument "process.action_mailbox", instrumentation_payload do
track_status_of_inbound_email do
run_callbacks :process do
process
end
end
rescue => exception
# TODO: Include a reference to the inbound_email in the exception raised so error handling becomes easier
rescue_with_handler(exception) || raise
end
rescue => exception
# TODO: Include a reference to the inbound_email in the exception raised so error handling becomes easier
rescue_with_handler(exception) || raise
end
def process
@ -104,6 +106,13 @@ module ActionMailbox
end
private
def instrumentation_payload
{
mailbox: self.class.name,
inbound_email: inbound_email.instrumentation_payload
}
end
def track_status_of_inbound_email
inbound_email.processing!
yield

View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
require_relative "../../test_helper"
class RepliesMailbox < ActionMailbox::Base
end
class ActionMailbox::Base::NotificationsTest < ActiveSupport::TestCase
test "instruments processing" do
events = []
ActiveSupport::Notifications.subscribe("process.action_mailbox") do |*args|
events << ActiveSupport::Notifications::Event.new(*args)
end
RepliesMailbox.receive create_inbound_email_from_fixture("welcome.eml")
assert_equal 1, events.length
assert_equal "process.action_mailbox", events[0].name
assert_equal(
{
mailbox: "RepliesMailbox",
inbound_email: {
id: 1,
message_id: "0CB459E0-0336-41DA-BC88-E6E28C697DDB@37signals.com",
status: "processing"
}
},
events[0].payload
)
ensure
ActiveSupport::Notifications.unsubscribe("process.action_mailbox")
end
end