From 3bfb600a7598fb519863951eee8af87e4c59b98c Mon Sep 17 00:00:00 2001 From: zi-yang-uni Date: Thu, 5 Sep 2024 13:19:26 -0700 Subject: [PATCH 1/6] fix: Move latency metric to api-handler --- lib/handlers/base/api-handler.ts | 43 +++++++++++++++++------------- lib/handlers/hard-quote/handler.ts | 1 - lib/handlers/quote/handler.ts | 1 - 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/handlers/base/api-handler.ts b/lib/handlers/base/api-handler.ts index e778d6c1..81752c99 100644 --- a/lib/handlers/base/api-handler.ts +++ b/lib/handlers/base/api-handler.ts @@ -88,22 +88,29 @@ export abstract class APIGLambdaHandler< } get handler(): APIGatewayProxyHandler { - return async (event: APIGatewayProxyEvent, context: Context): Promise => { - const handler = this.buildHandler(); + return metricScope( + (metric: MetricsLogger) => + async (event: APIGatewayProxyEvent, context: Context): Promise => { + const requestStart = new Date().getTime(); + const handler = this.buildHandler(); + const response = await handler(event, context); + const requestEnd = new Date().getTime(); - const response = await handler(event, context); + // Track latency + metric.putMetric(Metric.QUOTE_LATENCY, requestEnd - requestStart, MetricLoggerUnit.Milliseconds); - return { - ...response, - headers: { - ...response.headers, - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token', - 'Access-Control-Allow-Credentials': true, - 'Content-Type': 'application/json', - }, - }; - }; + return { + ...response, + headers: { + ...response.headers, + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token', + 'Access-Control-Allow-Credentials': true, + 'Content-Type': 'application/json', + }, + }; + } + ); } protected buildHandler(): APIGatewayProxyHandler { @@ -236,10 +243,10 @@ export abstract class APIGLambdaHandler< log: Logger ): Promise< | { - state: 'valid'; - requestBody: ReqBody; - requestQueryParams: ReqQueryParams; - } + state: 'valid'; + requestBody: ReqBody; + requestQueryParams: ReqQueryParams; + } | { state: 'invalid'; errorResponse: APIGatewayProxyResult } > { /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ diff --git a/lib/handlers/hard-quote/handler.ts b/lib/handlers/hard-quote/handler.ts index b6bbbe95..dc3a4504 100644 --- a/lib/handlers/hard-quote/handler.ts +++ b/lib/handlers/hard-quote/handler.ts @@ -112,7 +112,6 @@ export class QuoteHandler extends APIGLambdaHandler< }); if (response.statusCode == 200 || response.statusCode == 201) { metric.putMetric(Metric.QUOTE_200, 1, MetricLoggerUnit.Count); - metric.putMetric(Metric.QUOTE_LATENCY, Date.now() - start, MetricLoggerUnit.Milliseconds); const hardResponse = new HardQuoteResponse(request, cosignedOrder); if (!bestQuote) { // The RFQ responses are logged in getBestQuote() diff --git a/lib/handlers/quote/handler.ts b/lib/handlers/quote/handler.ts index 0d14b346..6d622120 100644 --- a/lib/handlers/quote/handler.ts +++ b/lib/handlers/quote/handler.ts @@ -61,7 +61,6 @@ export class QuoteHandler extends APIGLambdaHandler< log.info({ bestQuote: bestQuote }, 'bestQuote'); metric.putMetric(Metric.QUOTE_200, 1, MetricLoggerUnit.Count); - metric.putMetric(Metric.QUOTE_LATENCY, Date.now() - start, MetricLoggerUnit.Milliseconds); return { statusCode: 200, body: bestQuote.toResponseJSON(), From 4d845c523102ff8a192865b2e7c2f880da0611cc Mon Sep 17 00:00:00 2001 From: zi-yang-uni Date: Thu, 5 Sep 2024 13:42:35 -0700 Subject: [PATCH 2/6] create new metric handler duration --- lib/entities/aws-metrics-logger.ts | 5 +++++ lib/handlers/base/api-handler.ts | 7 ++++--- lib/handlers/hard-quote/handler.ts | 1 + lib/handlers/quote/handler.ts | 1 + 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/entities/aws-metrics-logger.ts b/lib/entities/aws-metrics-logger.ts index 7f40caa7..9528ec9b 100644 --- a/lib/entities/aws-metrics-logger.ts +++ b/lib/entities/aws-metrics-logger.ts @@ -37,6 +37,10 @@ export class AWSMetricsLogger implements IMetric { } } +export enum MetricDimension { + METHOD = 'method', +} + export enum Metric { QUOTE_200 = 'QUOTE_200', QUOTE_400 = 'QUOTE_400', @@ -46,6 +50,7 @@ export enum Metric { QUOTE_REQUESTED = 'QUOTE_REQUESTED', QUOTE_LATENCY = 'QUOTE_LATENCY', QUOTE_RESPONSE_COUNT = 'QUOTE_RESPONSE_COUNT', + HANDLER_DURATION = 'HANDLER_DURATION', QUOTE_POST_ERROR = 'QUOTE_POST_ERROR', QUOTE_POST_ATTEMPT = 'QUOTE_POST_ATTEMPT', diff --git a/lib/handlers/base/api-handler.ts b/lib/handlers/base/api-handler.ts index 81752c99..34b42bdb 100644 --- a/lib/handlers/base/api-handler.ts +++ b/lib/handlers/base/api-handler.ts @@ -9,7 +9,7 @@ import { import { default as bunyan, default as Logger } from 'bunyan'; import Joi from 'joi'; -import { Metric } from '../../entities'; +import { Metric, MetricDimension } from '../../entities'; import { CustomError, ErrorCode } from '../../util/errors'; import { BaseHandleRequestParams, BaseInjector, BaseLambdaHandler, BaseRInj } from './base'; @@ -96,8 +96,9 @@ export abstract class APIGLambdaHandler< const response = await handler(event, context); const requestEnd = new Date().getTime(); - // Track latency - metric.putMetric(Metric.QUOTE_LATENCY, requestEnd - requestStart, MetricLoggerUnit.Milliseconds); + // Track handler duration + metric.putDimensions({ [MetricDimension.METHOD]: this.handlerName }); + metric.putMetric(Metric.HANDLER_DURATION, requestEnd - requestStart, MetricLoggerUnit.Milliseconds); return { ...response, diff --git a/lib/handlers/hard-quote/handler.ts b/lib/handlers/hard-quote/handler.ts index dc3a4504..b6bbbe95 100644 --- a/lib/handlers/hard-quote/handler.ts +++ b/lib/handlers/hard-quote/handler.ts @@ -112,6 +112,7 @@ export class QuoteHandler extends APIGLambdaHandler< }); if (response.statusCode == 200 || response.statusCode == 201) { metric.putMetric(Metric.QUOTE_200, 1, MetricLoggerUnit.Count); + metric.putMetric(Metric.QUOTE_LATENCY, Date.now() - start, MetricLoggerUnit.Milliseconds); const hardResponse = new HardQuoteResponse(request, cosignedOrder); if (!bestQuote) { // The RFQ responses are logged in getBestQuote() diff --git a/lib/handlers/quote/handler.ts b/lib/handlers/quote/handler.ts index 6d622120..0d14b346 100644 --- a/lib/handlers/quote/handler.ts +++ b/lib/handlers/quote/handler.ts @@ -61,6 +61,7 @@ export class QuoteHandler extends APIGLambdaHandler< log.info({ bestQuote: bestQuote }, 'bestQuote'); metric.putMetric(Metric.QUOTE_200, 1, MetricLoggerUnit.Count); + metric.putMetric(Metric.QUOTE_LATENCY, Date.now() - start, MetricLoggerUnit.Milliseconds); return { statusCode: 200, body: bestQuote.toResponseJSON(), From 26a4ea63f5c0e21c073a5dcbb07d9bce52817d1d Mon Sep 17 00:00:00 2001 From: zi-yang-uni Date: Mon, 7 Oct 2024 12:39:22 -0700 Subject: [PATCH 3/6] update redshift-alpha version --- cdk.context.json | 5 +++++ package.json | 2 +- yarn.lock | 8 ++++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cdk.context.json b/cdk.context.json index df9b9011..82b5147c 100644 --- a/cdk.context.json +++ b/cdk.context.json @@ -54,5 +54,10 @@ "us-east-2a", "us-east-2b", "us-east-2c" + ], + "availability-zones:account=637423166136:region=us-east-2": [ + "us-east-2a", + "us-east-2b", + "us-east-2c" ] } diff --git a/package.json b/package.json index ac7d4173..0ca3d6a5 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "typescript": "^4.8.4" }, "dependencies": { - "@aws-cdk/aws-redshift-alpha": "^2.54.0-alpha.0", + "@aws-cdk/aws-redshift-alpha": "^2.85.0-alpha.0", "@aws-sdk/client-dynamodb": "^3.386.0", "@aws-sdk/client-firehose": "^3.490.0", "@aws-sdk/client-redshift-data": "^3.382.0", diff --git a/yarn.lock b/yarn.lock index b26ef8d5..4c19f172 100644 --- a/yarn.lock +++ b/yarn.lock @@ -30,10 +30,10 @@ resolved "https://registry.npmjs.org/@aws-cdk/asset-node-proxy-agent-v5/-/asset-node-proxy-agent-v5-2.0.166.tgz#467507db141cd829ff8aa9d6ea5519310a4276b8" integrity sha512-j0xnccpUQHXJKPgCwQcGGNu4lRiC1PptYfdxBIH1L4dRK91iBxtSQHESRQX+yB47oGLaF/WfNN/aF3WXwlhikg== -"@aws-cdk/aws-redshift-alpha@^2.54.0-alpha.0": - version "2.54.0-alpha.0" - resolved "https://registry.yarnpkg.com/@aws-cdk/aws-redshift-alpha/-/aws-redshift-alpha-2.54.0-alpha.0.tgz#eac9251cdb6939b2848191b42cc33f326a29917d" - integrity sha512-7/bxH6KMLArBfgJxiylNaz8NLV1EHcxa/kLctUK9xRBsn40DobZ/mIZxXc0keCK/siTKwv9i5qiGQ7l0YvJHOA== +"@aws-cdk/aws-redshift-alpha@^2.85.0-alpha.0": + version "2.85.0-alpha.0" + resolved "https://registry.yarnpkg.com/@aws-cdk/aws-redshift-alpha/-/aws-redshift-alpha-2.85.0-alpha.0.tgz#1231c32eb288516c5e5901e81009f5fe1d185839" + integrity sha512-oBXABcEWtVn8yPYR4wOoufdZe8J+nyvy3j0u2l8Gr5GXGQsRgSKWicqFjDid8vwfsX3xh5m26QclXsPWXsm6mw== "@aws-crypto/crc32@3.0.0": version "3.0.0" From 3956a29830afec58d553d26fffdb5db2be9ba3ac Mon Sep 17 00:00:00 2001 From: zi-yang-uni Date: Wed, 9 Oct 2024 09:15:07 -0700 Subject: [PATCH 4/6] log error with toJson for CustomError --- lib/handlers/base/api-handler.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/handlers/base/api-handler.ts b/lib/handlers/base/api-handler.ts index 34b42bdb..70fa499c 100644 --- a/lib/handlers/base/api-handler.ts +++ b/lib/handlers/base/api-handler.ts @@ -195,10 +195,12 @@ export abstract class APIGLambdaHandler< ({ body, statusCode } = handleRequestResult); } } catch (err) { - log.error({ err }, 'Unexpected error in handler'); if (err instanceof CustomError) { - return err.toJSON(id); + const errorJson = err.toJSON(id); + log.error({ errorJson }, 'Unexpected error in handler'); + return errorJson; } + log.error({ err }, 'Unexpected error in handler'); metric.putMetric(Metric.QUOTE_500, 1, MetricLoggerUnit.Count); return INTERNAL_ERROR(id); } From b4a655601684c5f1c2b4496e67f05d67f7f5ad56 Mon Sep 17 00:00:00 2001 From: zi-yang-uni Date: Wed, 9 Oct 2024 12:41:17 -0700 Subject: [PATCH 5/6] remove local cdk changes --- cdk.context.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cdk.context.json b/cdk.context.json index 82b5147c..b58fe552 100644 --- a/cdk.context.json +++ b/cdk.context.json @@ -50,11 +50,6 @@ "us-east-2b", "us-east-2c" ], - "availability-zones:account=032111050613:region=us-east-2": [ - "us-east-2a", - "us-east-2b", - "us-east-2c" - ], "availability-zones:account=637423166136:region=us-east-2": [ "us-east-2a", "us-east-2b", From 28e6d894cc615bc4f593c82439c112bf85a9d890 Mon Sep 17 00:00:00 2001 From: zi-yang-uni Date: Wed, 9 Oct 2024 12:43:08 -0700 Subject: [PATCH 6/6] Revert --- cdk.context.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdk.context.json b/cdk.context.json index b58fe552..df9b9011 100644 --- a/cdk.context.json +++ b/cdk.context.json @@ -50,7 +50,7 @@ "us-east-2b", "us-east-2c" ], - "availability-zones:account=637423166136:region=us-east-2": [ + "availability-zones:account=032111050613:region=us-east-2": [ "us-east-2a", "us-east-2b", "us-east-2c"