From f36beedef47e402e223cf2cf9602da269a4e0501 Mon Sep 17 00:00:00 2001 From: KishenKumarrrrr Date: Fri, 22 Nov 2024 13:24:39 +0800 Subject: [PATCH] refactor: tracer wrap --- .../middlewares/email-callback.middleware.ts | 40 ++++++++----------- .../src/email/routes/email-callback.routes.ts | 10 +++-- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/backend/src/email/middlewares/email-callback.middleware.ts b/backend/src/email/middlewares/email-callback.middleware.ts index f4c659066..a65722374 100644 --- a/backend/src/email/middlewares/email-callback.middleware.ts +++ b/backend/src/email/middlewares/email-callback.middleware.ts @@ -1,8 +1,6 @@ import { Request, Response, NextFunction } from 'express' import { EmailCallbackService } from '@email/services' import { loggerWithLabel } from '@core/logger' -import { tracer } from 'dd-trace' - const logger = loggerWithLabel(module) const isAuthenticated = ( @@ -10,24 +8,22 @@ const isAuthenticated = ( res: Response, next: NextFunction ): Response | void => { - tracer.wrap('isAuthenticated', () => { - const authHeader = req.get('authorization') - if (!authHeader) { - // SNS will send 2 request: - // - first one without the basic authorization first and require the callback - // server to respond with 401 WWW-Authenticate Basic realm="Email" - // - second one with the basic authorization - // The above mechanism is based on RFC-2671 https://www.rfc-editor.org/rfc/rfc2617.html#page-8 - // Of course, this middleare is to reject all requests without the - // Authorization header as well - res.set('WWW-Authenticate', 'Basic realm="Email"') - return res.sendStatus(401) - } - if (EmailCallbackService.isAuthenticated(authHeader)) { - return next() - } - return res.sendStatus(403) - }) + const authHeader = req.get('authorization') + if (!authHeader) { + // SNS will send 2 request: + // - first one without the basic authorization first and require the callback + // server to respond with 401 WWW-Authenticate Basic realm="Email" + // - second one with the basic authorization + // The above mechanism is based on RFC-2671 https://www.rfc-editor.org/rfc/rfc2617.html#page-8 + // Of course, this middleare is to reject all requests without the + // Authorization header as well + res.set('WWW-Authenticate', 'Basic realm="Email"') + return res.sendStatus(401) + } + if (EmailCallbackService.isAuthenticated(authHeader)) { + return next() + } + return res.sendStatus(403) } const parseEvent = async ( @@ -48,9 +44,6 @@ const printConfirmSubscription = ( res: Response, next: NextFunction ): Response | void => { - const printConfirmSubscriptionSpan = tracer.startSpan( - 'printConfirmSubscription' - ) const { Type: type, SubscribeURL: subscribeUrl } = JSON.parse(req.body) if (type === 'SubscriptionConfirmation') { const parsed = new URL(subscribeUrl) @@ -68,7 +61,6 @@ const printConfirmSubscription = ( return res.sendStatus(202) } } - printConfirmSubscriptionSpan.finish() return next() } export const EmailCallbackMiddleware = { diff --git a/backend/src/email/routes/email-callback.routes.ts b/backend/src/email/routes/email-callback.routes.ts index 212ecd6f0..939af206e 100644 --- a/backend/src/email/routes/email-callback.routes.ts +++ b/backend/src/email/routes/email-callback.routes.ts @@ -1,5 +1,6 @@ import { Router } from 'express' import { EmailCallbackMiddleware } from '@email/middlewares' +import tracer from 'dd-trace' const router = Router() /** * paths: @@ -16,9 +17,12 @@ const router = Router() */ router.post( '/', - EmailCallbackMiddleware.printConfirmSubscription, - EmailCallbackMiddleware.isAuthenticated, - EmailCallbackMiddleware.parseEvent + tracer.wrap( + 'printConfirmSubscription', + () => EmailCallbackMiddleware.printConfirmSubscription + ), + tracer.wrap('isAuthenticated', () => EmailCallbackMiddleware.isAuthenticated), + tracer.wrap('parseEvent', () => EmailCallbackMiddleware.parseEvent) ) export default router