From 6ffda271a7ad9a975839c74c0c857565c2d60e0a Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 14 Mar 2024 11:55:02 -0400 Subject: [PATCH 1/5] fix: use require for import in the middle Fixes an issue where bundlers (esbuild) fail to build because of es module interop issues --- .../src/platform/node/instrumentation.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index c639bc8bd48..3d5ff7720f9 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -25,7 +25,7 @@ import { Hooked, } from './RequireInTheMiddleSingleton'; import type { HookFn } from 'import-in-the-middle'; -import * as ImportInTheMiddle from 'import-in-the-middle'; +const ImportInTheMiddle = require('import-in-the-middle'); import { InstrumentationModuleDefinition } from '../../types'; import { diag } from '@opentelemetry/api'; import type { OnRequireFn } from 'require-in-the-middle'; @@ -268,7 +268,7 @@ export abstract class InstrumentationBase this._hooks.push(hook); const esmHook = - new (ImportInTheMiddle as unknown as typeof ImportInTheMiddle.default)( + new ImportInTheMiddle( [module.name], { internals: false }, hookFn From 946ae12b55eb525972b036aaf4289a6fff083e75 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 13 May 2024 13:12:49 -0400 Subject: [PATCH 2/5] import require --- .../src/platform/node/instrumentation.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index 45c61aa585d..6cfb485e641 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -25,7 +25,7 @@ import { Hooked, } from './RequireInTheMiddleSingleton'; import type { HookFn } from 'import-in-the-middle'; -const ImportInTheMiddle = require('import-in-the-middle'); +import ImportInTheMiddle = require('import-in-the-middle'); import { InstrumentationConfig, InstrumentationModuleDefinition, @@ -309,12 +309,11 @@ export abstract class InstrumentationBase< : this._requireInTheMiddleSingleton.register(module.name, onRequire); this._hooks.push(hook); - const esmHook = - new ImportInTheMiddle( - [module.name], - { internals: false }, - hookFn - ); + const esmHook = new ImportInTheMiddle( + [module.name], + { internals: false }, + hookFn + ); this._hooks.push(esmHook); } } From 951b72601df0b192612956ddf11485273ff938d7 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 13 May 2024 13:15:31 -0400 Subject: [PATCH 3/5] const require with ignore --- .../src/platform/node/instrumentation.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index 6cfb485e641..e68e54ecd9d 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -25,7 +25,8 @@ import { Hooked, } from './RequireInTheMiddleSingleton'; import type { HookFn } from 'import-in-the-middle'; -import ImportInTheMiddle = require('import-in-the-middle'); +// eslint-ignore-next-line @typescript-eslint/no-var-requires +const ImportInTheMiddle = require('import-in-the-middle'); import { InstrumentationConfig, InstrumentationModuleDefinition, From 7f1ae6169cc33b89f4139f3c40bc578f6c210f52 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 13 May 2024 13:23:19 -0400 Subject: [PATCH 4/5] Fix eslint comment --- .../src/platform/node/instrumentation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index e68e54ecd9d..c3e17a69607 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -25,7 +25,7 @@ import { Hooked, } from './RequireInTheMiddleSingleton'; import type { HookFn } from 'import-in-the-middle'; -// eslint-ignore-next-line @typescript-eslint/no-var-requires +// eslint-disable-next-line @typescript-eslint/no-var-requires const ImportInTheMiddle = require('import-in-the-middle'); import { InstrumentationConfig, From 00ae462d2f2af199de6b591a5cfd03f34a820b6e Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 13 May 2024 13:24:34 -0400 Subject: [PATCH 5/5] Changelog --- experimental/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 9037238df14..662d77e2870 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -29,6 +29,8 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) +* fix: require import-in-the-middle in order to fix an issue with bundlers [#4546](https://github.com/open-telemetry/opentelemetry-js/pull/4546) @dyladan + ### :books: (Refine Doc) * docs(instrumentation): better docs for supportedVersions option [#4693](https://github.com/open-telemetry/opentelemetry-js/pull/4693) @blumamir