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): camelCase methods can be double patched #1289

Merged
merged 5 commits into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 13 additions & 11 deletions packages/opentelemetry-plugin-grpc/src/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,19 @@ export class GrpcPlugin extends BasePlugin<grpc> {
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 =>
// eslint-disable-next-line no-prototype-builtins
!!originalName && client.prototype.hasOwnProperty(originalName)
) as string[]),
];
return methodsToWrap;
const methodSet = new Set<string>(); // use set to remove duplicates caused by "camelCase"
dyladan marked this conversation as resolved.
Show resolved Hide resolved

// For a method defined in .proto as "UnaryMethod"
Object.entries(methods).forEach(([name, { originalName }]) => {
methodSet.add(name); // adds camel case method name: "unaryMethod"
// eslint-disable-next-line no-prototype-builtins
if (originalName && client.prototype.hasOwnProperty(originalName)) {
// adds original method name: "UnaryMethod",
methodSet.add(originalName);
}
});

return Array.from(methodSet);
}

private _getPatchedClientMethods() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package pkg_test;

service GrpcTester {
rpc UnaryMethod (TestRequest) returns (TestReply) {}
rpc camelCaseMethod (TestRequest) returns (TestReply) {}
rpc ClientStreamMethod (stream TestRequest) returns (TestReply) {}
rpc ServerStreamMethod (TestRequest) returns (stream TestReply) {}
rpc BidiStreamMethod (stream TestRequest) returns (stream TestReply) {}
Expand Down
36 changes: 36 additions & 0 deletions packages/opentelemetry-plugin-grpc/test/grpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ interface TestRequestResponse {
type TestGrpcClient = grpc.Client & {
unaryMethod: any;
UnaryMethod: any;
camelCaseMethod: any;
clientStreamMethod: any;
serverStreamMethod: any;
bidiStreamMethod: any;
Expand Down Expand Up @@ -93,6 +94,24 @@ const grpcClient = {
});
},

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

UnaryMethod: (
client: TestGrpcClient,
request: TestRequestResponse
Expand Down Expand Up @@ -220,6 +239,16 @@ function startServer(grpc: GrpcModule, proto: any) {
: callback(null, { num: call.request.num });
},

// This method returns the request
camelCaseMethod(
call: grpc.ServerUnaryCall<any>,
callback: SendUnaryDataCallback
) {
call.request.num <= MAX_ERROR_STATUS
? callback(getError('Unary Method Error', call.request.num))
: callback(null, { num: call.request.num });
},

// This method sum the requests
clientStreamMethod(
call: grpc.ServerReadableStream<any>,
Expand Down Expand Up @@ -345,6 +374,13 @@ describe('GrpcPlugin', () => {
request: requestList[0],
result: requestList[0],
},
{
description: 'camelCase unary call',
methodName: 'camelCaseMethod',
method: grpcClient.camelCaseMethod,
request: requestList[0],
result: requestList[0],
},
{
description: 'Unary call',
methodName: 'UnaryMethod',
Expand Down