From a9d300dcfe4b4a165103dc26bf9948ca701f14e5 Mon Sep 17 00:00:00 2001 From: Valentin Marchaud Date: Mon, 3 May 2021 21:49:17 +0200 Subject: [PATCH] chore(grpc-instrumentation): fix grpc example #2160 (#2179) Co-authored-by: Daniel Dyla --- .../src/grpc/index.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts b/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts index 25bf00bb2c..bd0e1f117c 100644 --- a/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts +++ b/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts @@ -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', @@ -92,7 +93,7 @@ export class GrpcNativeInstrumentation extends InstrumentationBase< this._wrap( moduleExports, 'makeGenericClientConstructor', - this._patchClient(moduleExports) + this._patchClient() ); return moduleExports; }, @@ -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 = ( @@ -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'); @@ -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; @@ -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, {