Skip to content

Commit

Permalink
feat: enregistrement des perfs envoi d'email sur sentry (#1233)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlr authored May 21, 2024
1 parent 03387f2 commit d9f6dfa
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions server/src/services/mailer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Address } from "nodemailer/lib/mailer"
import SMTPTransport from "nodemailer/lib/smtp-transport"
import nodemailerHtmlToText from "nodemailer-html-to-text"

import { startSentryPerfRecording } from "@/common/utils/sentryUtils"

import config from "../config"

const htmlToText = nodemailerHtmlToText.htmlToText
Expand Down Expand Up @@ -41,10 +43,15 @@ const createTransporter = (): Transporter => {
const createMailer = () => {
const transporter = createTransporter()
const renderEmail = async (template: string, data: Data = {}): Promise<string> => {
const buffer = await renderFile(template, { data })
const { html } = mjml(buffer.toString(), { minify: true })
const onFinally = startSentryPerfRecording("mailer", "renderEmail")
try {
const buffer = await renderFile(template, { data })
const { html } = mjml(buffer.toString(), { minify: true })

return html
return html
} finally {
onFinally()
}
}

return {
Expand All @@ -69,15 +76,20 @@ const createMailer = () => {
cc?: string
attachments?: object[]
}): Promise<{ messageId: string; accepted?: string[] }> => {
return transporter.sendMail({
from,
to,
cc,
subject,
html: await renderEmail(template, data),
list: {},
attachments,
})
const html = await renderEmail(template, data)
const onFinally = startSentryPerfRecording("mailer", "sendEmail")

return transporter
.sendMail({
from,
to,
cc,
subject,
html,
list: {},
attachments,
})
.finally(onFinally)
},
}
}
Expand Down

0 comments on commit d9f6dfa

Please sign in to comment.