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

chore(grpc-instrumentation): fix grpc example #2160 #2179

Merged
merged 2 commits into from
May 3, 2021
Merged
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: 11 additions & 11 deletions packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
['1.*'],
(moduleExports, version) => {
diag.debug(`Applying patch for grpc@${version}`);
grpcClient = moduleExports;

if (isWrapped(moduleExports.Server.prototype.register)) {
this._unwrap(moduleExports.Server.prototype, 'register');
}
grpcClient = moduleExports;
this._wrap(
moduleExports.Server.prototype,
'register',
Expand All @@ -92,7 +93,7 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
this._wrap(
moduleExports,
'makeGenericClientConstructor',
this._patchClient(moduleExports)
this._patchClient()
);
return moduleExports;
},
Expand All @@ -116,11 +117,7 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
if (isWrapped(moduleExports.makeClientConstructor)) {
this._unwrap(moduleExports, 'makeClientConstructor');
}
this._wrap(
moduleExports,
'makeClientConstructor',
this._patchClient(grpcClient)
);
this._wrap(moduleExports, 'makeClientConstructor', this._patchClient());
return moduleExports;
};
const onUnPatch = (
Expand Down Expand Up @@ -243,7 +240,7 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
};
}

private _patchClient(grpcClient: typeof grpcTypes) {
private _patchClient() {
const instrumentation = this;
return (original: typeof grpcTypes.makeGenericClientConstructor): never => {
diag.debug('patching client');
Expand All @@ -257,7 +254,7 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
instrumentation._massWrap(
client.prototype as never,
instrumentation._getMethodsToWrap(client, methods) as never[],
instrumentation._getPatchedClientMethods(grpcClient) as any
instrumentation._getPatchedClientMethods() as any
);
return client;
} as never;
Expand Down Expand Up @@ -288,12 +285,15 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
return methodList;
}

private _getPatchedClientMethods(grpcClient: typeof grpcTypes) {
private _getPatchedClientMethods() {
const instrumentation = this;
return (original: GrpcClientFunc) => {
diag.debug('patch all client methods');
return function clientMethodTrace(this: grpcTypes.Client) {
const name = `grpc.${original.path.replace('/', '')}`;
const name = `grpc.${(original.path as string | undefined)?.replace(
'/',
''
)}`;
const args = Array.prototype.slice.call(arguments);
const metadata = getMetadata(grpcClient, original, args);
const span = instrumentation.tracer.startSpan(name, {
Expand Down