From e47a8835462b6a5f64d037cb0f99988760ed9f15 Mon Sep 17 00:00:00 2001 From: KishenKumarrrrr Date: Mon, 25 Nov 2024 11:18:57 +0800 Subject: [PATCH] fix: link spans correctly --- .../src/email/utils/callback/parsers/ses.ts | 91 ++++++++++--------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/backend/src/email/utils/callback/parsers/ses.ts b/backend/src/email/utils/callback/parsers/ses.ts index 79e2660bc..6602018e7 100644 --- a/backend/src/email/utils/callback/parsers/ses.ts +++ b/backend/src/email/utils/callback/parsers/ses.ts @@ -138,47 +138,52 @@ const parseNotificationAndEvent = async ( message: any, metadata: Metadata ): Promise => { - tracer.wrap('parseNotificationAndEvent', async () => { - if (!isNotificationAndEventForMainRecipient(message, type)) { - logger.info({ - message: 'SES notification or event is not for the main recipient', - action: 'filterNotification', - body: message, + const parseNotificationAndEventSpan = tracer.startSpan( + 'parseNotificationAndEvent', + { + childOf: tracer.scope().active() || undefined, + } + ) + if (!isNotificationAndEventForMainRecipient(message, type)) { + logger.info({ + message: 'SES notification or event is not for the main recipient', + action: 'filterNotification', + body: message, + }) + return + } + switch (type) { + case SesEventType.Delivery: + await updateDeliveredStatus(metadata) + break + case SesEventType.Bounce: + await updateBouncedStatus({ + ...metadata, + bounceType: message?.bounce?.bounceType, + bounceSubType: message?.bounce?.bounceSubType, + to: message?.mail?.commonHeaders?.to, + }) + break + case SesEventType.Complaint: + await updateComplaintStatus({ + ...metadata, + complaintType: message?.complaint?.complaintFeedbackType, + complaintSubType: message?.complaint?.complaintSubType, + to: message?.mail?.commonHeaders?.to, + }) + break + case SesEventType.Open: + await updateReadStatus(metadata) + break + default: + logger.warn({ + message: 'Unable to handle messages with this type', + action: 'parseNotification', + type, }) return - } - switch (type) { - case SesEventType.Delivery: - await updateDeliveredStatus(metadata) - break - case SesEventType.Bounce: - await updateBouncedStatus({ - ...metadata, - bounceType: message?.bounce?.bounceType, - bounceSubType: message?.bounce?.bounceSubType, - to: message?.mail?.commonHeaders?.to, - }) - break - case SesEventType.Complaint: - await updateComplaintStatus({ - ...metadata, - complaintType: message?.complaint?.complaintFeedbackType, - complaintSubType: message?.complaint?.complaintSubType, - to: message?.mail?.commonHeaders?.to, - }) - break - case SesEventType.Open: - await updateReadStatus(metadata) - break - default: - logger.warn({ - message: 'Unable to handle messages with this type', - action: 'parseNotification', - type, - }) - return - } - }) + } + parseNotificationAndEventSpan.finish() } // Validate SES record hash, returns message ID if valid, otherwise throw errors @@ -232,19 +237,21 @@ const parseRecord = async (record: SesRecord): Promise => { logger.info({ message: 'Parsing SES callback record', }) - const parseRecordJson = tracer.startSpan('parseRecordJson') + const parseRecordJson = tracer.startSpan('parseRecordJson', { + childOf: parseRecordSpan, + }) const message = JSON.parse(record.Message) parseRecordJson.finish() const smtpApiHeader = getSmtpApiHeader(message) const validateRecordSpan = tracer.startSpan('validateRecord', { - childOf: tracer.scope().active() || undefined, + childOf: parseRecordSpan, }) await validateRecord(record, smtpApiHeader) validateRecordSpan.finish() // Transactional emails don't have message IDs, so blacklist // relevant email addresses before everything else const blacklistIfNeededSpan = tracer.startSpan('blacklistIfNeeded', { - childOf: tracer.scope().active() || undefined, + childOf: parseRecordSpan, }) await blacklistIfNeeded(message) blacklistIfNeededSpan.finish()