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

Wrong import for "import-in-the-middle" with ESM #4717

Closed
okko opened this issue May 17, 2024 · 2 comments · Fixed by #4745
Closed

Wrong import for "import-in-the-middle" with ESM #4717

okko opened this issue May 17, 2024 · 2 comments · Fixed by #4745
Labels
bug Something isn't working triage

Comments

@okko
Copy link

okko commented May 17, 2024

What happened?

Steps to Reproduce

Import from "@opentelemetry/instrumentation" and bundle using esbuild with format: 'esm'

Expected Result

The build should proceed without warning and not crash during runtime.

Actual Result

When AWS CDK builds with esbuild, the following warning is emitted:

▲ [WARNING] Constructing "ImportInTheMiddle" will crash at run-time because it's an import namespace object, not a constructor [call-import-namespace]

    node_modules/@opentelemetry/instrumentation/build/esm/platform/node/instrumentation.js:286:30:
      286 │             var esmHook = new ImportInTheMiddle([module_2.name], { internals: false }, hookFn);
          ╵                               ~~~~~~~~~~~~~~~~~

  Consider changing "ImportInTheMiddle" to a default import instead:

    node_modules/@opentelemetry/instrumentation/build/esm/platform/node/instrumentation.js:48:7:
      48 │ import * as ImportInTheMiddle from 'import-in-the-middle';
         │        ~~~~~~~~~~~~~~~~~~~~~~
         ╵        ImportInTheMiddle

1 warning

Runtime, the application crashes with

2024-05-17T10:20:37.898Z	undefined	INFO	[Dynatrace] Failed to import runtime script(Node.js: v20.12.0, Agent: 1.287.2.20240223-044013, Script: /tmp/dtIndex.mjs):
TypeError: ImportInTheMiddle is not a constructor
    at _loop_1 (file:///var/task/index.mjs:x:x)
    at InstrumentationBase4.enable (file:///var/task/index.mjs:x:x)
    at new InstrumentationBase4 (file:///var/task/index.mjs:x:x)
    at new _AwsInstrumentation (file:///var/task/index.mjs:x:x)
    at file:///var/task/index.mjs:xx
    at ModuleJob.run (node:internal/modules/esm/module_job:x:x)
    at async ModuleLoader.import (node:internal/modules/esm/loader:x:x)
    at async _tryAwaitImport (file:///tmp/dtIndex.mjs:x:x)
    at async _tryRequire (file:///tmp/dtIndex.mjs:x:x)
    at async _loadUserApp (file:///tmp/dtIndex.mjs:x:x)

Additional Details

OpenTelemetry Setup Code

No response

package.json

"dependencies": {
    "@opentelemetry/api": "^1.8.0",
    "@opentelemetry/exporter-metrics-otlp-proto": "^0.51.1",
    "@opentelemetry/exporter-trace-otlp-proto": "^0.51.1",
    "@opentelemetry/instrumentation": "^0.51.1",
    "@opentelemetry/instrumentation-aws-sdk": "^0.41.0",
    "@opentelemetry/instrumentation-pg": "^0.41.0",
    "@opentelemetry/instrumentation-pino": "^0.39.0",
    "@opentelemetry/resources": "^1.24.1",
    "@opentelemetry/sdk-trace-base": "^1.24.1",
    "@opentelemetry/sdk-trace-node": "^1.24.1",
    "@opentelemetry/semantic-conventions": "^1.24.1"
  }

Relevant log output

No response

@okko okko added bug Something isn't working triage labels May 17, 2024
@okko
Copy link
Author

okko commented May 17, 2024

This issue is similar to issue #3954 but not the same.

@okko
Copy link
Author

okko commented May 17, 2024

Reporting this issue as requested in #4553 (comment)

bengl pushed a commit to nodejs/import-in-the-middle that referenced this issue May 31, 2024
`import-in-the-middle` has some ESM interop issues.

From
[here](open-telemetry/opentelemetry-js#4546 (comment)):

> Specifically, namespace imports are required by the ESM spec to be
objects. IITM's top-level export is not an object, but a callable
function

This causes
[issues](open-telemetry/opentelemetry-js#4717)
with some bundlers. Obviously bundled imports will not go through
`import-in-the-middle` but using it should not inhibit bundlers as iitm
can still hook build-in modules.

This adds a `Hook` named export and changes one cjs and esm test to
use that new export.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant