From d1459668b3cdf0719ea13a4776b4c29cfac27d91 Mon Sep 17 00:00:00 2001 From: anitarua Date: Mon, 30 Oct 2023 15:20:36 -0700 Subject: [PATCH 1/5] fix: experimental metrics middleware should use the grpc message constructor name in emitted metrics instead of MiddlewareMessage --- .../config/middleware/impl/experimental-metrics-middleware.ts | 2 +- packages/client-sdk-nodejs/src/index.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/client-sdk-nodejs/src/config/middleware/impl/experimental-metrics-middleware.ts b/packages/client-sdk-nodejs/src/config/middleware/impl/experimental-metrics-middleware.ts index 2163528ca..2123a88cb 100644 --- a/packages/client-sdk-nodejs/src/config/middleware/impl/experimental-metrics-middleware.ts +++ b/packages/client-sdk-nodejs/src/config/middleware/impl/experimental-metrics-middleware.ts @@ -108,7 +108,7 @@ export abstract class ExperimentalMetricsMiddlewareRequestHandler onRequestBody(request: MiddlewareMessage): Promise { this.requestSize = request.messageLength(); - this.requestType = request.constructor.name; + this.requestType = request._grpcMessage.constructor.name; this.requestBodyTime = new Date().getTime(); return Promise.resolve(request); } diff --git a/packages/client-sdk-nodejs/src/index.ts b/packages/client-sdk-nodejs/src/index.ts index 2023460d5..6635552ff 100644 --- a/packages/client-sdk-nodejs/src/index.ts +++ b/packages/client-sdk-nodejs/src/index.ts @@ -200,6 +200,7 @@ export { export {ExperimentalRequestLoggingMiddleware} from './config/middleware/experimental-request-logging-middleware'; export {ExperimentalMetricsCsvMiddleware} from './config/middleware/experimental-metrics-csv-middleware'; +export {ExperimentalMetricsLoggingMiddleware} from './config/middleware/experimental-metrics-logging-middleware'; export {ExampleAsyncMiddleware} from './config/middleware/example-async-middleware'; export { From af7a769b1e51086c69fd29efafcaae079c805ac2 Mon Sep 17 00:00:00 2001 From: anitarua Date: Tue, 31 Oct 2023 11:59:29 -0700 Subject: [PATCH 2/5] add null and undefined check --- .../internal/grpc/middlewares-interceptor.ts | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts b/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts index 43e4ac073..ed7464925 100644 --- a/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts +++ b/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts @@ -61,15 +61,17 @@ export function middlewaresInterceptor( // eslint-disable-next-line @typescript-eslint/no-explicit-any next: (message: any) => void ): void { - applyMiddlewareHandlers( - 'onResponseBody', - reversedMiddlewareRequestHandlers, - (h: MiddlewareRequestHandler) => - (request: MiddlewareMessage | null) => - h.onResponseBody(request), - new MiddlewareMessage(message as Message), - (msg: MiddlewareMessage | null) => next(msg?._grpcMessage) - ); + if (message != undefined && message != null) { + applyMiddlewareHandlers( + 'onResponseBody', + reversedMiddlewareRequestHandlers, + (h: MiddlewareRequestHandler) => + (request: MiddlewareMessage | null) => + h.onResponseBody(request), + new MiddlewareMessage(message as Message), + (msg: MiddlewareMessage | null) => next(msg?._grpcMessage) + ); + } }, onReceiveStatus: function ( status: StatusObject, @@ -98,14 +100,16 @@ export function middlewaresInterceptor( // unfortunately grpc uses `any` in their type defs for these // eslint-disable-next-line @typescript-eslint/no-explicit-any sendMessage: function (message: any, next: (message: any) => void): void { - applyMiddlewareHandlers( - 'onRequestBody', - middlewareRequestHandlers, - (h: MiddlewareRequestHandler) => (request: MiddlewareMessage) => - h.onRequestBody(request), - new MiddlewareMessage(message as Message), - (m: MiddlewareMessage) => next(m._grpcMessage) - ); + if (message != undefined && message != null) { + applyMiddlewareHandlers( + 'onRequestBody', + middlewareRequestHandlers, + (h: MiddlewareRequestHandler) => (request: MiddlewareMessage) => + h.onRequestBody(request), + new MiddlewareMessage(message as Message), + (m: MiddlewareMessage) => next(m._grpcMessage) + ); + } }, }; return new InterceptingCall(nextCall(options), requester); From d5cee45c10d22bb74e4307af8b0c07348d8d20d5 Mon Sep 17 00:00:00 2001 From: anitarua Date: Tue, 31 Oct 2023 12:12:36 -0700 Subject: [PATCH 3/5] fix lint errors --- .../src/internal/grpc/middlewares-interceptor.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts b/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts index ed7464925..b541ab075 100644 --- a/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts +++ b/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts @@ -61,7 +61,7 @@ export function middlewaresInterceptor( // eslint-disable-next-line @typescript-eslint/no-explicit-any next: (message: any) => void ): void { - if (message != undefined && message != null) { + if (message !== undefined && message !== null) { applyMiddlewareHandlers( 'onResponseBody', reversedMiddlewareRequestHandlers, @@ -100,7 +100,7 @@ export function middlewaresInterceptor( // unfortunately grpc uses `any` in their type defs for these // eslint-disable-next-line @typescript-eslint/no-explicit-any sendMessage: function (message: any, next: (message: any) => void): void { - if (message != undefined && message != null) { + if (message !== undefined && message !== null) { applyMiddlewareHandlers( 'onRequestBody', middlewareRequestHandlers, From f37c9044a2c755fc211f432ce71de1e8768c63fb Mon Sep 17 00:00:00 2001 From: anitarua Date: Tue, 31 Oct 2023 13:36:21 -0700 Subject: [PATCH 4/5] move null check to middleware message instead --- .../client-sdk-nodejs/src/config/middleware/middleware.ts | 5 ++++- .../src/internal/grpc/middlewares-interceptor.ts | 5 +---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/client-sdk-nodejs/src/config/middleware/middleware.ts b/packages/client-sdk-nodejs/src/config/middleware/middleware.ts index e8d1291d4..63e80dbb8 100644 --- a/packages/client-sdk-nodejs/src/config/middleware/middleware.ts +++ b/packages/client-sdk-nodejs/src/config/middleware/middleware.ts @@ -29,7 +29,10 @@ export class MiddlewareMessage { } messageLength(): number { - return this._grpcMessage.serializeBinary().length; + if (this._grpcMessage !== null && this._grpcMessage !== undefined) { + return this._grpcMessage.serializeBinary().length; + } + return 0; } } diff --git a/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts b/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts index b541ab075..0b282beb6 100644 --- a/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts +++ b/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts @@ -61,7 +61,6 @@ export function middlewaresInterceptor( // eslint-disable-next-line @typescript-eslint/no-explicit-any next: (message: any) => void ): void { - if (message !== undefined && message !== null) { applyMiddlewareHandlers( 'onResponseBody', reversedMiddlewareRequestHandlers, @@ -71,7 +70,6 @@ export function middlewaresInterceptor( new MiddlewareMessage(message as Message), (msg: MiddlewareMessage | null) => next(msg?._grpcMessage) ); - } }, onReceiveStatus: function ( status: StatusObject, @@ -100,7 +98,6 @@ export function middlewaresInterceptor( // unfortunately grpc uses `any` in their type defs for these // eslint-disable-next-line @typescript-eslint/no-explicit-any sendMessage: function (message: any, next: (message: any) => void): void { - if (message !== undefined && message !== null) { applyMiddlewareHandlers( 'onRequestBody', middlewareRequestHandlers, @@ -109,9 +106,9 @@ export function middlewaresInterceptor( new MiddlewareMessage(message as Message), (m: MiddlewareMessage) => next(m._grpcMessage) ); - } }, }; + console.log("\nCompleted creating the requester"); return new InterceptingCall(nextCall(options), requester); }; } From f99b5598a8c02e1396e18d8f65fbfdea558805d6 Mon Sep 17 00:00:00 2001 From: anitarua Date: Tue, 31 Oct 2023 15:00:27 -0700 Subject: [PATCH 5/5] remove console log and run linter --- .../internal/grpc/middlewares-interceptor.ts | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts b/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts index 0b282beb6..43e4ac073 100644 --- a/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts +++ b/packages/client-sdk-nodejs/src/internal/grpc/middlewares-interceptor.ts @@ -61,15 +61,15 @@ export function middlewaresInterceptor( // eslint-disable-next-line @typescript-eslint/no-explicit-any next: (message: any) => void ): void { - applyMiddlewareHandlers( - 'onResponseBody', - reversedMiddlewareRequestHandlers, - (h: MiddlewareRequestHandler) => - (request: MiddlewareMessage | null) => - h.onResponseBody(request), - new MiddlewareMessage(message as Message), - (msg: MiddlewareMessage | null) => next(msg?._grpcMessage) - ); + applyMiddlewareHandlers( + 'onResponseBody', + reversedMiddlewareRequestHandlers, + (h: MiddlewareRequestHandler) => + (request: MiddlewareMessage | null) => + h.onResponseBody(request), + new MiddlewareMessage(message as Message), + (msg: MiddlewareMessage | null) => next(msg?._grpcMessage) + ); }, onReceiveStatus: function ( status: StatusObject, @@ -98,17 +98,16 @@ export function middlewaresInterceptor( // unfortunately grpc uses `any` in their type defs for these // eslint-disable-next-line @typescript-eslint/no-explicit-any sendMessage: function (message: any, next: (message: any) => void): void { - applyMiddlewareHandlers( - 'onRequestBody', - middlewareRequestHandlers, - (h: MiddlewareRequestHandler) => (request: MiddlewareMessage) => - h.onRequestBody(request), - new MiddlewareMessage(message as Message), - (m: MiddlewareMessage) => next(m._grpcMessage) - ); + applyMiddlewareHandlers( + 'onRequestBody', + middlewareRequestHandlers, + (h: MiddlewareRequestHandler) => (request: MiddlewareMessage) => + h.onRequestBody(request), + new MiddlewareMessage(message as Message), + (m: MiddlewareMessage) => next(m._grpcMessage) + ); }, }; - console.log("\nCompleted creating the requester"); return new InterceptingCall(nextCall(options), requester); }; }