Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(grpc): patch original client methods #631

Merged
merged 3 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/opentelemetry-plugin-grpc/src/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,17 @@ export class GrpcPlugin extends BasePlugin<grpc> {
const plugin = this;
return (original: typeof grpcTypes.makeGenericClientConstructor): never => {
plugin._logger.debug('patching client');
return function makeClientConstructor<ImplementationType>(
return function makeClientConstructor(
this: typeof grpcTypes.Client,
methods: grpcTypes.ServiceDefinition<ImplementationType>,
methods: { [key: string]: { originalName?: string } },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ServiceDefintion type removed for better readability?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right.

serviceName: string,
options: grpcTypes.GenericClientOptions
) {
// tslint:disable-next-line:no-any
const client = original.apply(this, arguments as any);
shimmer.massWrap(
client.prototype as never,
Object.keys(methods) as never[],
plugin._getMethodsToWrap(client, methods) as never[],
// tslint:disable-next-line:no-any
plugin._getPatchedClientMethods() as any
);
Expand All @@ -339,6 +339,22 @@ export class GrpcPlugin extends BasePlugin<grpc> {
};
}

private _getMethodsToWrap(
client: typeof grpcTypes.Client,
methods: { [key: string]: { originalName?: string } }
): string[] {
const methodsToWrap = [
...Object.keys(methods),
...(Object.keys(methods)
.map(methodName => methods[methodName].originalName)
.filter(
originalName =>
!!originalName && client.prototype.hasOwnProperty(originalName)
) as string[]),
];
return methodsToWrap;
}

private _getPatchedClientMethods() {
const plugin = this;
return (original: GrpcClientFunc) => {
Expand Down
27 changes: 27 additions & 0 deletions packages/opentelemetry-plugin-grpc/test/grpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type TestGrpcClient = grpc.Client & {
// tslint:disable-next-line:no-any
unaryMethod: any;
// tslint:disable-next-line:no-any
UnaryMethod: any;
// tslint:disable-next-line:no-any
clientStreamMethod: any;
// tslint:disable-next-line:no-any
serverStreamMethod: any;
Expand Down Expand Up @@ -93,6 +95,24 @@ const grpcClient = {
});
},

UnaryMethod: (
client: TestGrpcClient,
request: TestRequestResponse
): Promise<TestRequestResponse> => {
return new Promise((resolve, reject) => {
return client.UnaryMethod(
request,
(err: grpc.ServiceError, response: TestRequestResponse) => {
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
});
},

clientStreamMethod: (
client: TestGrpcClient,
request: TestRequestResponse[]
Expand Down Expand Up @@ -318,6 +338,13 @@ describe('GrpcPlugin', () => {
request: requestList[0],
result: requestList[0],
},
{
description: 'Unary call',
methodName: 'UnaryMethod',
method: grpcClient.UnaryMethod,
request: requestList[0],
result: requestList[0],
},
{
description: 'clientStream call',
methodName: 'ClientStreamMethod',
Expand Down