Bug 1615989 - Collect how many mails are sent. r=mkmelin
authorPing Chen <remotenonsense@gmail.com>
Tue, 26 May 2020 13:30:23 +0300
changeset 39255 5d54c44d54d3997fb44197ad5c21613c9cc2a2f4
parent 39254 cc4f616b9c78ed4dd893fc97ea3a63acc74800e8
child 39256 92b5978bce31c5a8b8ddd9c866d8df1ca198c67a
push id402
push userclokep@gmail.com
push dateMon, 29 Jun 2020 20:48:04 +0000
reviewersmkmelin
bugs1615989
Bug 1615989 - Collect how many mails are sent. r=mkmelin
mail/components/telemetry/Scalars.yaml
mailnews/compose/src/nsSmtpService.cpp
mailnews/compose/test/unit/test_mailTelemetry.js
mailnews/compose/test/unit/xpcshell.ini
--- a/mail/components/telemetry/Scalars.yaml
+++ b/mail/components/telemetry/Scalars.yaml
@@ -74,16 +74,29 @@ tb.compose:
       - 'thunderbird'
     kind: uint
     notification_emails:
       - "telemetry-client-dev@thunderbird.net"
     record_in_processes:
       - 'main'
 
 tb.mails:
+  sent:
+    bug_numbers:
+      - 1615989
+    description: How many emails are sent.
+    expires: never
+    products:
+      - 'thunderbird'
+    kind: uint
+    notification_emails:
+      - "telemetry-client-dev@thunderbird.net"
+    record_in_processes:
+      - 'main'
+
   read:
     bug_numbers:
       - 1615990
     description: How many emails are read.
     expires: never
     products:
       - 'thunderbird'
     kind: uint
--- a/mailnews/compose/src/nsSmtpService.cpp
+++ b/mailnews/compose/src/nsSmtpService.cpp
@@ -19,25 +19,28 @@
 #include "nsSmtpProtocol.h"
 #include "nsCOMPtr.h"
 #include "nsIMsgIdentity.h"
 #include "nsIPrompt.h"
 #include "nsIWindowWatcher.h"
 #include "nsComposeStrings.h"
 #include "nsIAsyncInputStream.h"
 #include "nsIPrincipal.h"
+#include "mozilla/Telemetry.h"
 
 #define SERVER_DELIMITER ','
 #define APPEND_SERVERS_VERSION_PREF_NAME "append_preconfig_smtpservers.version"
 #define MAIL_ROOT_PREF "mail."
 #define PREF_MAIL_SMTPSERVERS "mail.smtpservers"
 #define PREF_MAIL_SMTPSERVERS_APPEND_SERVERS \
   "mail.smtpservers.appendsmtpservers"
 #define PREF_MAIL_SMTP_DEFAULTSERVER "mail.smtp.defaultserver"
 
+using namespace mozilla;
+
 typedef struct _findServerByKeyEntry {
   const char *key;
   nsISmtpServer *server;
 } findServerByKeyEntry;
 
 typedef struct _findServerByHostnameEntry {
   nsCString hostname;
   nsCString username;
@@ -88,16 +91,20 @@ NS_IMETHODIMP nsSmtpService::SendMailMes
                             aNotificationCallbacks, &urlToRun, aRequestDSN);
     if (NS_SUCCEEDED(rv) && urlToRun)
       rv = NS_MsgLoadSmtpUrl(urlToRun, nullptr, aRequest);
 
     if (aURL)            // does the caller want a handle on the url?
       *aURL = urlToRun;  // transfer our ref count to the caller....
     else
       NS_IF_RELEASE(urlToRun);
+
+#ifndef MOZ_SUITE
+    Telemetry::ScalarAdd(Telemetry::ScalarID::TB_MAILS_SENT, 1);
+#endif
   }
 
   return rv;
 }
 
 // The following are two convenience functions I'm using to help expedite
 // building and running a mail to url...
 
new file mode 100644
--- /dev/null
+++ b/mailnews/compose/test/unit/test_mailTelemetry.js
@@ -0,0 +1,67 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * Test telemetry related to mails sent.
+ */
+
+let { TelemetryTestUtils } = ChromeUtils.import(
+  "resource://testing-common/TelemetryTestUtils.jsm"
+);
+
+let server;
+
+let kIdentityMail = "identity@foo.invalid";
+let kSender = "from@foo.invalid";
+let kTo = "to@foo.invalid";
+
+/**
+ * Check that we're counting mails sent.
+ */
+add_task(async function test_mails_sent() {
+  Services.telemetry.clearScalars();
+  const NUM_MAILS = 3;
+
+  server = setupServerDaemon();
+
+  // Test file
+  let testFile = do_get_file("data/message1.eml");
+
+  // Ensure we have at least one mail account
+  localAccountUtils.loadLocalMailAccount();
+
+  // Handle the server in a try/catch/finally loop so that we always will stop
+  // the server if something fails.
+  try {
+    // Start the fake SMTP server
+    server.start();
+    let smtpServer = getBasicSmtpServer(server.port);
+    let identity = getSmtpIdentity(kIdentityMail, smtpServer);
+
+    for (let i = 0; i < NUM_MAILS; i++) {
+      MailServices.smtp.sendMailMessage(
+        testFile,
+        kTo,
+        identity,
+        kSender,
+        null,
+        null,
+        null,
+        null,
+        false,
+        {},
+        {}
+      );
+    }
+  } catch (e) {
+    do_throw(e);
+  } finally {
+    server.stop();
+  }
+  let scalars = TelemetryTestUtils.getProcessScalars("parent");
+  Assert.equal(
+    scalars["tb.mails.sent"],
+    NUM_MAILS,
+    "Count of mails sent must be correct."
+  );
+});
--- a/mailnews/compose/test/unit/xpcshell.ini
+++ b/mailnews/compose/test/unit/xpcshell.ini
@@ -39,8 +39,9 @@ skip-if = os == 'mac'
 [test_smtpProtocols.js]
 [test_smtpProxy.js]
 [test_smtpURL.js]
 [test_splitRecipients.js]
 [test_staleTemporaryFileCleanup.js]
 [test_temporaryFilesRemoved.js]
 [test_longLines.js]
 [test_telemetry_compose.js]
+[test_mailTelemetry.js]