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.
This commit is contained in:
Sam Lord 2022-10-12 16:52:51 +01:00
parent 168fa64395
commit 0c18060f61

View File

@ -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