From a869e12b7f1f800b6049566ef771eae9f4dd8121 Mon Sep 17 00:00:00 2001 From: Amir Blum Date: Fri, 10 May 2024 08:12:26 +0300 Subject: [PATCH 1/2] feat(instrumentation): apply unwrap before wrap in base class --- .../src/instrumentation.ts | 28 ------------------- .../src/http.ts | 19 ------------- .../src/platform/node/instrumentation.ts | 4 +++ 3 files changed, 4 insertions(+), 47 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts index ae0ac8f7a4..c035b256ee 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts @@ -53,7 +53,6 @@ import { } from '@opentelemetry/api'; import { InstrumentationNodeModuleDefinition, - isWrapped, InstrumentationBase, } from '@opentelemetry/instrumentation'; import { @@ -102,9 +101,6 @@ export class GrpcInstrumentation extends InstrumentationBase { - if (isWrapped(moduleExports.Server.prototype.register)) { - this._unwrap(moduleExports.Server.prototype, 'register'); - } // Patch Server methods this._wrap( moduleExports.Server.prototype, @@ -112,45 +108,21 @@ export class GrpcInstrumentation extends InstrumentationBase { - if (isWrapped(moduleExports.request)) { - this._unwrap(moduleExports, 'request'); - } this._wrap( moduleExports, 'request', this._getPatchOutgoingRequestFunction('http') ); - if (isWrapped(moduleExports.get)) { - this._unwrap(moduleExports, 'get'); - } this._wrap( moduleExports, 'get', this._getPatchOutgoingGetFunction(moduleExports.request) ); - if (isWrapped(moduleExports.Server.prototype.emit)) { - this._unwrap(moduleExports.Server.prototype, 'emit'); - } this._wrap( moduleExports.Server.prototype, 'emit', @@ -152,25 +142,16 @@ export class HttpInstrumentation extends InstrumentationBase { - if (isWrapped(moduleExports.request)) { - this._unwrap(moduleExports, 'request'); - } this._wrap( moduleExports, 'request', this._getPatchHttpsOutgoingRequestFunction('https') ); - if (isWrapped(moduleExports.get)) { - this._unwrap(moduleExports, 'get'); - } this._wrap( moduleExports, 'get', this._getPatchHttpsOutgoingGetFunction(moduleExports.request) ); - if (isWrapped(moduleExports.Server.prototype.emit)) { - this._unwrap(moduleExports.Server.prototype, 'emit'); - } this._wrap( moduleExports.Server.prototype, 'emit', diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index 674b1b963f..24426c05a6 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -34,6 +34,7 @@ import { diag } from '@opentelemetry/api'; import type { OnRequireFn } from 'require-in-the-middle'; import { Hook } from 'require-in-the-middle'; import { readFileSync } from 'fs'; +import { isWrapped } from '../../utils'; /** * Base abstract class for instrumenting node plugins @@ -79,6 +80,9 @@ export abstract class InstrumentationBase< } protected override _wrap: typeof wrap = (moduleExports, name, wrapper) => { + if (isWrapped(moduleExports[name])) { + this._unwrap(moduleExports, name); + } if (!utilTypes.isProxy(moduleExports)) { return wrap(moduleExports, name, wrapper); } else { From 073d1fe654f597e9e53b366b90b64821a54119ba Mon Sep 17 00:00:00 2001 From: Amir Blum Date: Fri, 10 May 2024 08:52:46 +0300 Subject: [PATCH 2/2] chore: CHANGELOG --- experimental/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 20e3fbfdab..aaac3b52f8 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -20,6 +20,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* feat(instrumentation): apply unwrap before wrap in base class [#4692](https://github.com/open-telemetry/opentelemetry-js/pull/4692) * feat(instrumentation): add util to execute span customization hook in base class [#4663](https://github.com/open-telemetry/opentelemetry-js/pull/4663) @blumamir * feat(instrumentation): generic config type in instrumentation base [#4659](https://github.com/open-telemetry/opentelemetry-js/pull/4659) @blumamir * feat: support node 22 [#4666](https://github.com/open-telemetry/opentelemetry-js/pull/4666) @dyladan