Skip to content
This repository
Browse code

Merge pull request #12556 from dasch/dasch/nstrument-am-processing

Instrument the generation of Action Mailer messages
  • Loading branch information...
commit fdfc96750a396d385d05ea1bf2de58c4d777e72e 2 parents cc8d148 + 8f0c5e0
José Valim authored October 20, 2013
5  actionmailer/CHANGELOG.md
Source Rendered
... ...
@@ -1,3 +1,8 @@
  1
+* Instrument the generation of Action Mailer messages. The time it takes to
  2
+  generate a message is written to the log.
  3
+
  4
+  *Daniel Schierbeck*
  5
+
1 6
 * invoke mailer defaults as procs only if they are procs, do not convert
2 7
   with to_proc.  That an object is convertible to a proc does not mean it's
3 8
   meant to be always used as a proc.  Fixes #11533
15  actionmailer/lib/action_mailer/base.rb
@@ -511,11 +511,18 @@ def initialize(method_name=nil, *args)
511 511
       process(method_name, *args) if method_name
512 512
     end
513 513
 
514  
-    def process(*args) #:nodoc:
515  
-      lookup_context.skip_default_locale!
  514
+    def process(method_name, *args) #:nodoc:
  515
+      payload = {
  516
+        :mailer => self.class.name,
  517
+        :action => method_name
  518
+      }
516 519
 
517  
-      super
518  
-      @_message = NullMail.new unless @_mail_was_called
  520
+      ActiveSupport::Notifications.instrument("process.action_mailer", payload) do
  521
+        lookup_context.skip_default_locale!
  522
+
  523
+        super
  524
+        @_message = NullMail.new unless @_mail_was_called
  525
+      end
519 526
     end
520 527
 
521 528
     class NullMail #:nodoc:
7  actionmailer/lib/action_mailer/log_subscriber.rb
@@ -19,6 +19,13 @@ def receive(event)
19 19
       debug(event.payload[:mail])
20 20
     end
21 21
 
  22
+    # An email was generated.
  23
+    def process(event)
  24
+      mailer = event.payload[:mailer]
  25
+      action = event.payload[:action]
  26
+      debug("\n#{mailer}##{action}: processed outbound mail in #{event.duration.round(1)}ms")
  27
+    end
  28
+
22 29
     # Use the logger configured for ActionMailer::Base
23 30
     def logger
24 31
       ActionMailer::Base.logger
9  actionmailer/test/log_subscriber_test.rb
@@ -24,10 +24,13 @@ def set_logger(logger)
24 24
   def test_deliver_is_notified
25 25
     BaseMailer.welcome.deliver
26 26
     wait
  27
+
27 28
     assert_equal(1, @logger.logged(:info).size)
28 29
     assert_match(/Sent mail to system@test.lindsaar.net/, @logger.logged(:info).first)
29  
-    assert_equal(1, @logger.logged(:debug).size)
30  
-    assert_match(/Welcome/, @logger.logged(:debug).first)
  30
+
  31
+    assert_equal(2, @logger.logged(:debug).size)
  32
+    assert_match(/BaseMailer#welcome: processed outbound mail in [\d.]+ms/, @logger.logged(:debug).first)
  33
+    assert_match(/Welcome/, @logger.logged(:debug).second)
31 34
   end
32 35
 
33 36
   def test_receive_is_notified
@@ -39,4 +42,4 @@ def test_receive_is_notified
39 42
     assert_equal(1, @logger.logged(:debug).size)
40 43
     assert_match(/Jamis/, @logger.logged(:debug).first)
41 44
   end
42  
-end
  45
+end

0 notes on commit fdfc967

Please sign in to comment.
Something went wrong with that request. Please try again.