-
Notifications
You must be signed in to change notification settings - Fork 40
chore: verified require(esm) compatibility for ESM-only dependencies #2575
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
Open
aryamohanan
wants to merge
7
commits into
main
Choose a base branch
from
chore-require-esm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−17
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b1e1d46
chore: verified Node.js require(esm) compatibility for ESM-only depen…
aryamohanan 5bc7464
chore: verified Node.js require(esm) compatibility for ESM-only depen…
aryamohanan cdd1d25
chore: verified Node.js require(esm) compatibility for ESM-only depen…
aryamohanan 911140c
chore: require esm without default
aryamohanan 1ec259c
test: test
aryamohanan 541c70e
test: test
aryamohanan eec08a3
test: test
aryamohanan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * (c) Copyright IBM Corp. 2026 | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // NOTE: c8 bug https://github.com/bcoe/c8/issues/166 | ||
| process.on('SIGTERM', () => { | ||
| process.disconnect(); | ||
| process.exit(0); | ||
| }); | ||
|
|
||
| const agentPort = process.env.INSTANA_AGENT_PORT; | ||
|
|
||
| require('@instana/collector')(); | ||
|
|
||
| // Load ESM-only dependency using Node.js native require(esm) support | ||
| // 'got' is pure ESM from v14+. Since require(esm) returns the full Module Namespace Object | ||
| // rather than an unwrapped default function, we must explicitly destructure `.default`. | ||
| const { default: got } = require('got'); | ||
|
|
||
| const express = require('express'); | ||
| const port = require('@_local/collector/test/test_util/app-port')(); | ||
| const app = express(); | ||
| const logPrefix = `require(esm) test app (${process.pid}):\t`; | ||
|
|
||
| function log() { | ||
| const args = Array.prototype.slice.call(arguments); | ||
| args[0] = `${logPrefix}${args[0]}`; | ||
| // eslint-disable-next-line no-console | ||
| console.log.apply(console, args); | ||
| } | ||
|
|
||
| app.get('/', (req, res) => { | ||
| res.send('OK'); | ||
| }); | ||
|
|
||
| app.get('/make-request', async (req, res) => { | ||
| const targetUrl = `http://127.0.0.1:${agentPort}/`; | ||
|
|
||
| try { | ||
| const response = await got(targetUrl); | ||
|
|
||
| log(`Request successful, status: ${response.statusCode}`); | ||
| res.json({ | ||
| success: true, | ||
| statusCode: response.statusCode, | ||
| url: targetUrl, | ||
| bodyLength: response.body.length | ||
| }); | ||
| } catch (error) { | ||
| log(`Request failed: ${error.message}`); | ||
| res.status(500).json({ | ||
| success: false, | ||
| error: error.message, | ||
| url: targetUrl | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| app.listen(port, () => { | ||
| log(`Listening on port: ${port}`); | ||
| }); |
9 changes: 9 additions & 0 deletions
9
packages/collector/test/tracing/misc/require-esm/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "name": "require-esm-test", | ||
| "version": "1.0.0", | ||
| "description": "Test for Node.js require(esm) support", | ||
| "main": "app.js", | ||
| "dependencies": { | ||
| "got": "latest" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * (c) Copyright IBM Corp. 2026 | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const config = require('@instana/core/test/config'); | ||
|
|
||
| describe('tracing/misc/require-esm', function () { | ||
| this.timeout(config.getTestTimeout() * 2); | ||
|
|
||
| require('./test_base')(); | ||
| }); |
124 changes: 124 additions & 0 deletions
124
packages/collector/test/tracing/misc/require-esm/test_base.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * (c) Copyright IBM Corp. 2026 | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const semver = require('semver'); | ||
| const expect = require('chai').expect; | ||
| const { supportedVersion } = require('@instana/core').tracing; | ||
| const config = require('@instana/core/test/config'); | ||
| const { retry, expectExactlyOneMatching } = require('@instana/core/test/test_util'); | ||
| const ProcessControls = require('@instana/collector/test/test_util/ProcessControls'); | ||
| const globalAgent = require('@instana/collector/test/globalAgent'); | ||
|
|
||
| const supportsRequireESM = semver.gte(process.versions.node, '20.19.0'); | ||
| const mochaSuiteFn = supportedVersion(process.versions.node) && supportsRequireESM ? describe : describe.skip; | ||
|
|
||
| module.exports = function () { | ||
| mochaSuiteFn('tracing/require(esm)', function () { | ||
| this.timeout(config.getTestTimeout() * 2); | ||
|
|
||
| globalAgent.setUpCleanUpHooks(); | ||
| const agentControls = globalAgent.instance; | ||
|
|
||
| describe('tracing enabled, no suppression', function () { | ||
| let controls; | ||
|
|
||
| before(async () => { | ||
| controls = new ProcessControls({ | ||
| dirname: __dirname, | ||
| useGlobalAgent: true | ||
| }); | ||
|
|
||
| await controls.startAndWaitForAgentConnection(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| await agentControls.clearReceivedTraceData(); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await controls.stop(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await controls.clearIpcMessages(); | ||
| }); | ||
|
|
||
| it('must trace HTTP requests made with ESM-only package loaded via require()', async () => { | ||
| const response = await controls.sendRequest({ | ||
| method: 'GET', | ||
| path: '/make-request' | ||
| }); | ||
|
|
||
| expect(response.success).to.equal(true); | ||
| expect(response.statusCode).to.equal(200); | ||
|
|
||
| await retry(async () => { | ||
| const spans = await agentControls.getSpans(); | ||
|
|
||
| expect(spans).to.have.lengthOf(2); | ||
|
|
||
| const httpEntry = expectExactlyOneMatching(spans, [ | ||
| span => expect(span.n).to.equal('node.http.server'), | ||
| span => expect(span.k).to.equal(1), | ||
| span => expect(span.data.http.method).to.equal('GET'), | ||
| span => expect(span.data.http.url).to.match(/\/make-request/) | ||
| ]); | ||
|
|
||
| const httpExit = expectExactlyOneMatching(spans, [ | ||
| span => expect(span.n).to.equal('node.http.client'), | ||
| span => expect(span.k).to.equal(2), | ||
| span => expect(span.t).to.equal(httpEntry.t), | ||
| span => expect(span.p).to.equal(httpEntry.s), | ||
| span => expect(span.data.http.method).to.equal('GET'), | ||
| span => expect(span.data.http.url).to.include('127.0.0.1') | ||
| ]); | ||
|
|
||
| expect(httpExit.data.http.status).to.equal(200); | ||
| expect(httpExit.ec).to.equal(0); | ||
| expect(httpEntry.ec).to.equal(0); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('tracing suppressed', function () { | ||
| let controls; | ||
|
|
||
| before(async () => { | ||
| controls = new ProcessControls({ | ||
| dirname: __dirname, | ||
| useGlobalAgent: true | ||
| }); | ||
|
|
||
| await controls.startAndWaitForAgentConnection(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| await agentControls.clearReceivedTraceData(); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await controls.stop(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await controls.clearIpcMessages(); | ||
| }); | ||
|
|
||
| it('should not trace when suppressed', async () => { | ||
| await controls.sendRequest({ | ||
| method: 'GET', | ||
| path: '/make-request', | ||
| suppressTracing: true | ||
| }); | ||
|
|
||
| await retry(async () => { | ||
| const spans = await agentControls.getSpans(); | ||
| expect(spans).to.have.lengthOf(0); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really cool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The author of this package is really cool 😆