Skip to content

Commit

Permalink
chore(grpc-instrumentation): fix grpc example #2160 (#2179)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
vmarchaud and dyladan committed May 3, 2021
1 parent cd54a45 commit a9d300d
Showing 1 changed file with 11 additions and 11 deletions.
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

0 comments on commit a9d300d

Please sign in to comment.