Skip to content

Commit 587a5f5

Browse files
authored
fix(grpc): patch original client methods (#631)
* fix(grpc): patch original client methods * fix: review comments and build * fix: add test
1 parent bc583b8 commit 587a5f5

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

packages/opentelemetry-plugin-grpc/src/grpc.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,17 +320,17 @@ export class GrpcPlugin extends BasePlugin<grpc> {
320320
const plugin = this;
321321
return (original: typeof grpcTypes.makeGenericClientConstructor): never => {
322322
plugin._logger.debug('patching client');
323-
return function makeClientConstructor<ImplementationType>(
323+
return function makeClientConstructor(
324324
this: typeof grpcTypes.Client,
325-
methods: grpcTypes.ServiceDefinition<ImplementationType>,
325+
methods: { [key: string]: { originalName?: string } },
326326
serviceName: string,
327327
options: grpcTypes.GenericClientOptions
328328
) {
329329
// tslint:disable-next-line:no-any
330330
const client = original.apply(this, arguments as any);
331331
shimmer.massWrap(
332332
client.prototype as never,
333-
Object.keys(methods) as never[],
333+
plugin._getMethodsToWrap(client, methods) as never[],
334334
// tslint:disable-next-line:no-any
335335
plugin._getPatchedClientMethods() as any
336336
);
@@ -339,6 +339,22 @@ export class GrpcPlugin extends BasePlugin<grpc> {
339339
};
340340
}
341341

342+
private _getMethodsToWrap(
343+
client: typeof grpcTypes.Client,
344+
methods: { [key: string]: { originalName?: string } }
345+
): string[] {
346+
const methodsToWrap = [
347+
...Object.keys(methods),
348+
...(Object.keys(methods)
349+
.map(methodName => methods[methodName].originalName)
350+
.filter(
351+
originalName =>
352+
!!originalName && client.prototype.hasOwnProperty(originalName)
353+
) as string[]),
354+
];
355+
return methodsToWrap;
356+
}
357+
342358
private _getPatchedClientMethods() {
343359
const plugin = this;
344360
return (original: GrpcClientFunc) => {

packages/opentelemetry-plugin-grpc/test/grpc.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type TestGrpcClient = grpc.Client & {
4545
// tslint:disable-next-line:no-any
4646
unaryMethod: any;
4747
// tslint:disable-next-line:no-any
48+
UnaryMethod: any;
49+
// tslint:disable-next-line:no-any
4850
clientStreamMethod: any;
4951
// tslint:disable-next-line:no-any
5052
serverStreamMethod: any;
@@ -93,6 +95,24 @@ const grpcClient = {
9395
});
9496
},
9597

98+
UnaryMethod: (
99+
client: TestGrpcClient,
100+
request: TestRequestResponse
101+
): Promise<TestRequestResponse> => {
102+
return new Promise((resolve, reject) => {
103+
return client.UnaryMethod(
104+
request,
105+
(err: grpc.ServiceError, response: TestRequestResponse) => {
106+
if (err) {
107+
reject(err);
108+
} else {
109+
resolve(response);
110+
}
111+
}
112+
);
113+
});
114+
},
115+
96116
clientStreamMethod: (
97117
client: TestGrpcClient,
98118
request: TestRequestResponse[]
@@ -318,6 +338,13 @@ describe('GrpcPlugin', () => {
318338
request: requestList[0],
319339
result: requestList[0],
320340
},
341+
{
342+
description: 'Unary call',
343+
methodName: 'UnaryMethod',
344+
method: grpcClient.UnaryMethod,
345+
request: requestList[0],
346+
result: requestList[0],
347+
},
321348
{
322349
description: 'clientStream call',
323350
methodName: 'ClientStreamMethod',

0 commit comments

Comments
 (0)