Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ vi.mock('@/models/email-suppression-entry', () => ({
},
}))

vi.mock('@/helpers/metrics', () => ({
incrementMetric: vi.fn(),
}))

describe('send transactional email', () => {
let $: IGlobalVariable

Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/apps/postman/common/email-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import appConfig from '@/config/app'
import HttpError from '@/errors/http'
import { getLdFlagValue } from '@/helpers/launch-darkly'
import logger from '@/helpers/logger'
import { incrementMetric } from '@/helpers/metrics'
import { getSesClient, shouldUseSes } from '@/helpers/ses-email-helper'
import EmailSuppressionEntry from '@/models/email-suppression-entry'

Expand Down Expand Up @@ -158,6 +159,7 @@ async function sendViaSes(
}),
}),
)
incrementMetric('ses.email.sent')

// TODO: remove this log once the SES rollout is verified and stable.
logger.info('Email sent via SES', {
Expand Down
10 changes: 10 additions & 0 deletions packages/backend/src/helpers/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import tracer from './tracer'

export function incrementMetric(
name: string,
tags: Record<string, string> = {},
): void {
tracer.dogstatsd.increment(name, 1, {
...tags,
})
}
10 changes: 10 additions & 0 deletions packages/backend/src/helpers/process-ses-event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logger from '@/helpers/logger'
import { incrementMetric } from '@/helpers/metrics'
import { SesEvent, SesEventType } from '@/helpers/ses-event-parser'
import EmailSuppressionEntry from '@/models/email-suppression-entry'

Expand Down Expand Up @@ -28,6 +29,11 @@ export async function processSesEvent(data: SesEventInput): Promise<void> {
if (sesEvent.eventType === SesEventType.Bounce) {
const { bounceType, bounceSubType, bouncedRecipients } = sesEvent.bounce

incrementMetric('ses.email.bounce', {
bounce_type: bounceType,
bounce_sub_type: bounceSubType ?? 'unknown',
})

if (bounceType === 'Permanent') {
// TODO: add micro-optimisation for upsertSuppression to blacklist multiple recipient emails in phase 2
for (const recipient of bouncedRecipients) {
Expand Down Expand Up @@ -64,6 +70,10 @@ export async function processSesEvent(data: SesEventInput): Promise<void> {
if (sesEvent.eventType === SesEventType.Complaint) {
const { complainedRecipients, complaintFeedbackType } = sesEvent.complaint

incrementMetric('ses.email.complaint', {
complaint_feedback_type: complaintFeedbackType ?? 'other',
})

if (complaintFeedbackType === 'not-spam') {
// Auto-whitelist: recipient marked the email as not-spam
const emails = complainedRecipients.map((r) => r.emailAddress)
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/helpers/ses-email-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fromTemporaryCredentials } from '@aws-sdk/credential-providers'
import appConfig from '@/config/app'

import logger from './logger'
import { incrementMetric } from './metrics'

let sesClient: SESv2Client | null = null

Expand Down Expand Up @@ -82,6 +83,7 @@ export async function sendEmailViaSes({
}),
}),
)
incrementMetric('ses.email.sent')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: technically we can avoid multiple calls to this and just increment by total number of successfully sent emails inside the postman action itself.

But keeping it here is also ok, i dont think there is any material difference

} catch (e) {
logger.error('Error sending email via SES', {
error: e,
Expand Down
Loading