From 0c18060f615746c28935bc718d4eef02a08df863 Mon Sep 17 00:00:00 2001 From: Sam Lord Date: Wed, 12 Oct 2022 16:52:51 +0100 Subject: [PATCH] Added metric for sending transactional email with mailgun refs: https://github.com/TryGhost/Toolbox/issues/439 This case completes the monitoring of mailgun.js usage within Ghost. --- .../core/server/services/mail/GhostMailer.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ghost/core/core/server/services/mail/GhostMailer.js b/ghost/core/core/server/services/mail/GhostMailer.js index b0315ac45c..f98243328f 100644 --- a/ghost/core/core/server/services/mail/GhostMailer.js +++ b/ghost/core/core/server/services/mail/GhostMailer.js @@ -7,6 +7,7 @@ const errors = require('@tryghost/errors'); const tpl = require('@tryghost/tpl'); const settingsCache = require('../../../shared/settings-cache'); const urlUtils = require('../../../shared/url-utils'); +const metrics = require('@tryghost/metrics'); const messages = { title: 'Ghost at {domain}', checkEmailConfigInstructions: 'Please see {url} for instructions on configuring email.', @@ -83,7 +84,8 @@ module.exports = class GhostMailer { const options = config.get('mail') && _.clone(config.get('mail').options) || {}; this.state = { - usingDirect: transport === 'direct' + usingDirect: transport === 'direct', + usingMailgun: transport === 'mailgun' }; this.transport = nodemailer(transport, options); } @@ -121,10 +123,24 @@ module.exports = class GhostMailer { } async sendMail(message) { + const startTime = Date.now(); try { const response = await this.transport.sendMail(message); + if (this.state.usingMailgun) { + metrics.metric('mailgun-send-transactional-mail', { + value: Date.now() - startTime, + statusCode: 200 + }); + } + return response; } catch (err) { + if (this.state.usingMailgun) { + metrics.metric('mailgun-send-transactional-mail', { + value: Date.now() - startTime, + statusCode: err.status + }); + } throw createMailError({ message: tpl(messages.reason, {reason: err.message || err}), err