From 3597a8a0ea8edfc91ca6dcdb2cffac75558d5219 Mon Sep 17 00:00:00 2001 From: alecps Date: Mon, 28 Aug 2023 19:01:21 -0400 Subject: [PATCH] fix response metrics --- .../signer/src/common/handler.ts | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/phone-number-privacy/signer/src/common/handler.ts b/packages/phone-number-privacy/signer/src/common/handler.ts index 88648ff774f..08d29e06107 100644 --- a/packages/phone-number-privacy/signer/src/common/handler.ts +++ b/packages/phone-number-privacy/signer/src/common/handler.ts @@ -44,9 +44,9 @@ export function catchErrorHandler( if (!res.headersSent) { if (err instanceof OdisError) { - sendFailure(err.code, err.status, res) + sendFailure(err.code, err.status, res, req.url) } else { - sendFailure(ErrorMessage.UNKNOWN_ERROR, 500, res) + sendFailure(ErrorMessage.UNKNOWN_ERROR, 500, res, req.url) } } else { // Getting to this error likely indicates that the `perform` process @@ -102,20 +102,20 @@ export function timeoutHandler( timeoutMs: number, handler: PromiseHandler ): PromiseHandler { - return async (request, response) => { + return async (req, res) => { const timeoutSignal = (AbortSignal as any).timeout(timeoutMs) timeoutSignal.addEventListener( 'abort', () => { - if (!response.headersSent) { + if (!res.headersSent) { Counters.timeouts.inc() - sendFailure(ErrorMessage.TIMEOUT_FROM_SIGNER, 500, response) + sendFailure(ErrorMessage.TIMEOUT_FROM_SIGNER, 500, res, req.url) } }, { once: true } ) - await handler(request, response) + await handler(req, res) } } @@ -127,22 +127,23 @@ export function withEnableHandler( if (enabled) { return handler(req, res) } else { - sendFailure(WarningMessage.API_UNAVAILABLE, 503, res) + sendFailure(WarningMessage.API_UNAVAILABLE, 503, res, req.url) } } } export async function disabledHandler( - _: Request<{}, {}, R>, + req: Request<{}, {}, R>, response: Response, Locals> ): Promise { - sendFailure(WarningMessage.API_UNAVAILABLE, 503, response) + sendFailure(WarningMessage.API_UNAVAILABLE, 503, response, req.url) } export function sendFailure( error: ErrorType, status: number, response: Response, + endpoint: string, body?: Record // TODO remove any ) { send( @@ -156,6 +157,7 @@ export function sendFailure( status, response.locals.logger ) + Counters.responses.labels(endpoint, status.toString()).inc() } export interface Result {