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

feat: added support for got v12, v13, v14 #1157

Merged
merged 5 commits into from
May 23, 2024
Merged

feat: added support for got v12, v13, v14 #1157

merged 5 commits into from
May 23, 2024

Conversation

aryamohanan
Copy link
Contributor

@aryamohanan aryamohanan commented May 21, 2024

Added Native ESM Support for got Module

In our instrumentation setup, while native ESM support is still pending and the requireHook isn't compatible with native ESM, the 'got' module transitions to a pure ESM module from version 12 onwards. This transition remains functional due to 'got' being instrumented alongside the 'http' module, which works without the requireHook. Notably, this differs from our other instrumentations, which require 'http' and 'https' at the top. Importantly, native ESM libraries importing core Node modules do not trigger Module._load and thus do not utilize the requireHook. However, our requireHook is triggered when an ESM library imports a CommonJS package.
We've extended our testing suite to cover this latest version, ensuring accurate capture of spans.

  • The ESM application will be tested against the latest version of got (v14).
  • The CJS application will be tested against the last version of got that supports CommonJS (v11).

@aryamohanan aryamohanan force-pushed the got-esm branch 2 times, most recently from 1645756 to c4e622b Compare May 22, 2024 04:04
@aryamohanan aryamohanan marked this pull request as ready for review May 22, 2024 08:23
@aryamohanan aryamohanan requested a review from a team as a code owner May 22, 2024 08:23
@kirrg001
Copy link
Contributor

feat: added native esm support for 'got' module #1157

Lets stick to our naming convention.

feat: added support for got v14 (or v12, v13, v14)

See convention or even better this.

@@ -35,8 +35,8 @@ app.get('/', (req, res) => {

app.get('/multipleRequireWithStealthyRequire', async (req, res) => {
// Wrap the require calls with stealthyRequire to avoid caching
const firstInstanceOfGot = stealthyRequire(require.cache, () => require('got'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: How about we choose a different library for this test case?

* })();
* NOTE: From v12 the got module is pure ESM.
* See https://github.com/sindresorhus/got/releases/tag/v12.0.0.
* In ESM test, we use the latest got module.
Copy link
Contributor

@kirrg001 kirrg001 May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I am missing a detailed explanation in the requireHook.js why got >= v12 is working.

@aryamohanan aryamohanan changed the title feat: added native esm support for 'got' module feat: added support forgot v12, v13, v14 May 22, 2024
@aryamohanan aryamohanan force-pushed the got-esm branch 2 times, most recently from f51495b to d80b866 Compare May 22, 2024 11:48
@aryamohanan aryamohanan changed the title feat: added support forgot v12, v13, v14 feat: added support for got v12, v13, v14 May 22, 2024
// We aim to extract the module name to apply our instrumentation.
// The native ESM support is pending, and the requireHook isn't compatible with native ESM.
// However, for the 'got' module, we persist with the require hook as its uses the core HTTP
// module under the hood. In our instrumentation, 'got' is automatically included alongside
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I think the description is not accurate and hard to follow.

with the require hook as its uses the core HTTP module under the hood

got is using http2wrapper, which is a CJS library, which requires http. Only thats why our requireHook works.

If got would only use import http from 'node:http' it would not work.
Its kinda a coincidence that got >= v12 is working.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the comment little bit, could you check again please?

@aryamohanan aryamohanan requested a review from kirrg001 May 22, 2024 13:21
// CASE: we ignore all file endings, which we are not interested in. Any module can load any file.
// CASE: The native ESM support is pending, and the requireHook isn't compatible with native ESM.
// However, 'got' is automatically instrumented along with the the http2wrapper,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// However, 'got' is automatically instrumented along with the the http2wrapper,
// However, 'got' is instrumented by coincidency, because it is requiring the [http2wrapper](https://github.com/search?q=repo%3Asindresorhus%2Fgot%20from%20%27http2-wrapper%27&type=code) CJS package, which is requiring http.

// CASE: we ignore all file endings, which we are not interested in. Any module can load any file.
// CASE: The native ESM support is pending, and the requireHook isn't compatible with native ESM.
// However, 'got' is automatically instrumented along with the the http2wrapper,
// which is a CommonJS library requiring http. Despite 'got' transitioning to a pure ESM module
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// which is a CommonJS library requiring http. Despite 'got' transitioning to a pure ESM module
// Despite 'got' transitioning to a pure ESM module

// However, 'got' is instrumented by coincidency, because it is requiring the http2wrapper
// https://github.com/search?q=repo%3Asindresorhus%2Fgot%20from%20%27http2-wrapper%27&type=code
// CJS package, which is requiring http. Despite 'got' transitioning to a pure ESM module
// from v12 onwards, it continues to work with the existing requireHook.
Copy link
Contributor

@kirrg001 kirrg001 May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that the explanation is not the underlying reason:

because it is requiring the http2wrapper

got and node-fetch work because we are instrumenting http and https without even using the requireHook. That is different to every other instrumentation we have. We even require http and https on top. Could you please rewrite the comment? Please also add the link.

But We still should keep the explanation that ESM libraries do not trigger requireHook when importing core modules and as soon as an ESM library imports a CJS package, our requireHook will get triggered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've up[dated the comment

@kirrg001 kirrg001 merged commit 1333a3c into main May 23, 2024
1 check was pending
@kirrg001 kirrg001 deleted the got-esm branch May 23, 2024 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants