Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Instrument the generation of Action Mailer messages

The processing of outbound mail is instrumented with the key
`process.action_mailer`. The payload includes the mailer name as well as
the mailer method.
  • Loading branch information...
commit 8f0c5e00ff61cc2f61acedfb35de756bd0e95437 1 parent cc8d148
@dasch dasch authored
View
5 actionmailer/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Instrument the generation of Action Mailer messages. The time it takes to
+ generate a message is written to the log.
+
+ *Daniel Schierbeck*
+
* invoke mailer defaults as procs only if they are procs, do not convert
with to_proc. That an object is convertible to a proc does not mean it's
meant to be always used as a proc. Fixes #11533
View
15 actionmailer/lib/action_mailer/base.rb
@@ -511,11 +511,18 @@ def initialize(method_name=nil, *args)
process(method_name, *args) if method_name
end
- def process(*args) #:nodoc:
- lookup_context.skip_default_locale!
+ def process(method_name, *args) #:nodoc:
+ payload = {
+ :mailer => self.class.name,
+ :action => method_name
+ }
- super
- @_message = NullMail.new unless @_mail_was_called
+ ActiveSupport::Notifications.instrument("process.action_mailer", payload) do
+ lookup_context.skip_default_locale!
+
+ super
+ @_message = NullMail.new unless @_mail_was_called
+ end
end
class NullMail #:nodoc:
View
7 actionmailer/lib/action_mailer/log_subscriber.rb
@@ -19,6 +19,13 @@ def receive(event)
debug(event.payload[:mail])
end
+ # An email was generated.
+ def process(event)
+ mailer = event.payload[:mailer]
+ action = event.payload[:action]
+ debug("\n#{mailer}##{action}: processed outbound mail in #{event.duration.round(1)}ms")
+ end
+
# Use the logger configured for ActionMailer::Base
def logger
ActionMailer::Base.logger
View
9 actionmailer/test/log_subscriber_test.rb
@@ -24,10 +24,13 @@ def set_logger(logger)
def test_deliver_is_notified
BaseMailer.welcome.deliver
wait
+
assert_equal(1, @logger.logged(:info).size)
assert_match(/Sent mail to system@test.lindsaar.net/, @logger.logged(:info).first)
- assert_equal(1, @logger.logged(:debug).size)
- assert_match(/Welcome/, @logger.logged(:debug).first)
+
+ assert_equal(2, @logger.logged(:debug).size)
+ assert_match(/BaseMailer#welcome: processed outbound mail in [\d.]+ms/, @logger.logged(:debug).first)
+ assert_match(/Welcome/, @logger.logged(:debug).second)
end
def test_receive_is_notified
@@ -39,4 +42,4 @@ def test_receive_is_notified
assert_equal(1, @logger.logged(:debug).size)
assert_match(/Jamis/, @logger.logged(:debug).first)
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.