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 native esm support #1159

Merged
merged 19 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@
"socket.io-client": "^4.7.5",
"sqs-consumer": "^10.3.0",
"sqs-consumer-v5": "npm:sqs-consumer@^5.7.0",
"square-calc": "3.2.1",
"square-calc-v2": "npm:[email protected]",
"stealthy-require": "1.1.1",
"superagent": "^9.0.2",
"tedious": "^17.0.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/aws-fargate/esm-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@
*/

import './src/index.js';
// Here we export all named exports from '@instana/core/iitm-loader.mjs', enabling
// integration of import-in-the-middle (IITM) for Native ESM module support.
export * from '@instana/core/iitm-loader.mjs';
11 changes: 6 additions & 5 deletions packages/aws-fargate/esm-register.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Previously, loading the Instana collector within the loader and after the update ESM support
* no longer working with v18.19 and above. To address this, we've opted to load the Instana
* collector in the main thread using --import.
* Additionally, we aim to incorporate native ESM support by utilizing the node register method,
* Additionally, we incorporated native ESM support by utilizing the node register method,
* enabling customization of the ESM loader with 'import-in-the-middle'.
*
* Usage:
Expand All @@ -19,7 +19,8 @@
// Import the initialization module for aws-fargate collector; it self-initializes upon import
// and it should be executed in the main thread.
import './src/index.js';

// We plan to utilize this for adding native ESM support in the near future
// import { register } from 'node:module';
// register(./loader.mjs, import.meta.url);
import { register } from 'node:module';
// ESM module resolution and loading are facilitated by registering `@instana/core/iitm-loader.mjs`, which exports
// import-in-the-middle(IITM) hooks. This registration can be accomplished using the register method from node:module.
// see: https://nodejs.org/api/module.html#customization-hooks
register('@instana/core/iitm-loader.mjs', import.meta.url);
3 changes: 3 additions & 0 deletions packages/azure-container-services/esm-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@
*/

import './src/index.js';
// Here we export all named exports from '@instana/core/iitm-loader.mjs', enabling
// integration of import-in-the-middle (IITM) for Native ESM module support.
export * from '@instana/core/iitm-loader.mjs';
11 changes: 6 additions & 5 deletions packages/azure-container-services/esm-register.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* see https://github.com/nodejs/node/pull/44710.
* Previously, loading the Instana collector within the loader and after the update ESM support
* no longer working with v18.19 and above. To address this, we've opted to load the Instana
* collector in the main thread using --import. Additionally, we aim to incorporate native ESM
* collector in the main thread using --import. Additionally, we incorporated native ESM
* support by utilizing the node register method, enabling customization of the ESM loader
* with 'import-in-the-middle'.
*
Expand All @@ -19,7 +19,8 @@
// Import the initialization module for azure-container-services collector; it self-initializes upon import
// and it should be executed in the main thread.
import './src/index.js';

// We plan to utilize this for adding native ESM support in the near future
// import { register } from 'node:module';
// register(./loader.mjs, import.meta.url);
import { register } from 'node:module';
// ESM module resolution and loading are facilitated by registering `@instana/core/iitm-loader.mjs`, which exports
// import-in-the-middle(IITM) hooks. This registration can be accomplished using the register method from node:module.
// see: https://nodejs.org/api/module.html#customization-hooks
register('@instana/core/iitm-loader.mjs', import.meta.url);
3 changes: 3 additions & 0 deletions packages/collector/esm-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@

import instana from './src/index.js';
instana();
// Here we export all named exports from '@instana/core/iitm-loader.mjs', enabling
// integration of import-in-the-middle (IITM) for Native ESM module support.
export * from '@instana/core/iitm-loader.mjs';
11 changes: 6 additions & 5 deletions packages/collector/esm-register.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* see https://github.com/nodejs/node/pull/44710.
* Previously, loading the Instana collector within the loader and after the update ESM support
* no longer working with v18.19 and above. To address this, we've opted to load the Instana
* collector in the main thread using --import. Additionally, we aim to incorporate native ESM
* collector in the main thread using --import. Additionally, we incorporated native ESM
* support by utilizing the node register method, enabling customization of the ESM loader
* with 'import-in-the-middle'.
*
Expand All @@ -19,7 +19,8 @@
// Import the initialization module for Instana collector and it should be executed in the main thread.
import instana from './src/index.js';
instana();

// We plan to utilize this for adding native ESM support in the near future
// import { register } from 'node:module';
// register(./loader.mjs, import.meta.url);
// ESM module resolution and loading are facilitated by registering `@instana/core/iitm-loader.mjs`, which exports
// import-in-the-middle(IITM) hooks. This registration can be accomplished using the register method from node:module.
// see: https://nodejs.org/api/module.html#customization-hooks
import { register } from 'node:module';
register('@instana/core/iitm-loader.mjs', import.meta.url);
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const cluster = require('cluster');

const requireHook = require('@instana/core').util.requireHook;
const hook = require('@instana/core').util.hook;
const selfPath = require('./selfPath');

/** @type {import('@instana/core/src/logger').GenericLogger} */
Expand All @@ -17,7 +17,7 @@ logger = require('../../../logger').getLogger('tracing/edgemicro', newLogger =>
});

exports.init = function () {
requireHook.onFileLoad(/\/edgemicro\/cli\/lib\/reload-cluster.js/, instrumentReloadCluster);
hook.onFileLoad(/\/edgemicro\/cli\/lib\/reload-cluster.js/, instrumentReloadCluster);
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'use strict';

const mock = require('mock-require');
const requireHook = require('../../../../../core/src/util/requireHook');
const hook = require('../../../../../core/src/util/hook');
const ELASTIC_VERSION = process.env.ELASTIC_VERSION;
const ELASTIC_REQUIRE =
process.env.ELASTIC_VERSION === 'latest' ? '@elastic/elasticsearch' : `@elastic/elasticsearch-v${ELASTIC_VERSION}`;
Expand All @@ -14,8 +14,8 @@ if (ELASTIC_REQUIRE !== '@elastic/elasticsearch') {
mock('@elastic/elasticsearch', ELASTIC_REQUIRE);
}

const originalFn = requireHook.onModuleLoad;
requireHook.onModuleLoad = function onModuleLoad() {
const originalFn = hook.onModuleLoad;
hook.onModuleLoad = function onModuleLoad() {
if (arguments[0] !== '@elastic/elasticsearch') {
return originalFn.apply(this, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'use strict';

const mock = require('mock-require');
const requireHook = require('../../../../../core/src/util/requireHook');
const hook = require('../../../../../core/src/util/hook');

const MONGODB_VERSION = process.env.MONGODB_VERSION;
const MONGODB_REQUIRE = process.env.MONGODB_VERSION === 'latest' ? 'mongodb' : `mongodb-${MONGODB_VERSION}`;
Expand All @@ -14,8 +14,8 @@ if (MONGODB_REQUIRE !== 'mongodb') {
mock('mongodb', MONGODB_REQUIRE);
}

const originalOnFileLoad = requireHook.onFileLoad;
requireHook.onFileLoad = function onFileLoad() {
const originalOnFileLoad = hook.onFileLoad;
hook.onFileLoad = function onFileLoad() {
if (
arguments[0].source === '\\/mongodb\\/lib\\/cmap\\/connection\\.js' ||
arguments[0].source === '\\/mongodb\\/lib\\/core\\/connection\\/pool\\.js'
Expand Down
17 changes: 15 additions & 2 deletions packages/collector/test/tracing/database/prisma/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,21 @@ describe('tracing/prisma', function () {

['latest', 'v4', 'v450'].forEach(version => {
providers.forEach(provider => {
let mochaSuiteFn = supportedVersion(process.versions.node) ? describe : describe.skip;
mochaSuiteFn = version === 'latest' && semver.lt(process.versions.node, '16.0.0') ? describe.skip : describe;
let mochaSuiteFn = describe;

if (supportedVersion(process.versions.node)) {
// Skip ESM tests for Node.js version 18.19.0 and above due to an issue with import-in-the-middle (IITM)
// package. See https://github.com/DataDog/import-in-the-middle/issues/97
if (semver.gte(process.versions.node, '18.19.0') && process.env.RUN_ESM) {
mochaSuiteFn = describe.skip;
}

if (version === 'latest' && semver.lt(process.versions.node, '16.0.0')) {
mochaSuiteFn = describe.skip;
}
} else {
mochaSuiteFn = describe.skip;
}

mochaSuiteFn(`[${version}] with provider ${provider}`, () => {
if (provider === 'postgresql' && !process.env.PRISMA_POSTGRES_URL) {
Expand Down
6 changes: 3 additions & 3 deletions packages/collector/test/tracing/logger/pino/mockVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'use strict';

const mock = require('mock-require');
const requireHook = require('../../../../../core/src/util/requireHook');
const hook = require('../../../../../core/src/util/hook');
const PINO_VERSION = process.env.PINO_VERSION || 'latest';
const PINO_REQUIRE = PINO_VERSION === 'latest' ? 'pino' : `pino-${PINO_VERSION}`;

Expand All @@ -22,8 +22,8 @@ if (PINO_REQUIRE !== 'pino') {
* If we test against `pino-v6`, we need to wait for the
* on file load event for `node_modules/pino-v6/....js`
*/
const originalOnFileLoad = requireHook.onFileLoad;
requireHook.onFileLoad = function onFileLoad() {
const originalOnFileLoad = hook.onFileLoad;
hook.onFileLoad = function onFileLoad() {
if (arguments[0].toString() !== '/\\/pino\\/lib\\/tools\\.js/') {
return originalOnFileLoad.apply(this, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const Module = require('module');
const mock = require('mock-require');
const requireHook = require('../../../../../core/src/util/requireHook');
const hook = require('../../../../../core/src/util/hook');

const AMQPLIB_REQUIRE = process.env.AMQPLIB_VERSION === 'latest' ? 'amqplib' : `amqplib-${process.env.AMQPLIB_VERSION}`;

Expand All @@ -23,8 +23,8 @@ if (AMQPLIB_REQUIRE !== 'amqplib') {
* If we test against `amqplib-v0.8.0`, we need to wait for the
* on file load event for `node_modules/amqplib-v0.8.0/lib/...
*/
const originalOnFileLoad = requireHook.onFileLoad;
requireHook.onFileLoad = function onFileLoad() {
const originalOnFileLoad = hook.onFileLoad;
hook.onFileLoad = function onFileLoad() {
if (
arguments[0].source === '\\/amqplib\\/lib\\/channel\\.js' ||
arguments[0].source === '\\/amqplib\\/lib\\/channel_model\\.js' ||
Expand Down
52 changes: 52 additions & 0 deletions packages/collector/test/tracing/native_esm/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* (c) Copyright IBM Corp. 2024
*/

'use strict';

// NOTE: c8 bug https://github.com/bcoe/c8/issues/166
process.on('SIGTERM', () => {
process.disconnect();
process.exit(0);
});
const mock = require('mock-require');
mock('square-calc', 'square-calc-v2');
require('../../..')();
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const getAppPort = require('../../test_util/app-port');
const calculateSquare = require('square-calc');

const port = getAppPort();

const app = express();
const logPrefix = `Native ESM App (${process.pid}):\t`;

if (process.env.WITH_STDOUT) {
app.use(morgan(`${logPrefix}:method :url :status`));
}

app.use(bodyParser.json());

app.get('/', async (req, res) => {
res.sendStatus(200);
});

app.get('/request', async (req, res) => {
const square = calculateSquare(5);
res.json({ square });
});

app.listen(port, () => {
log(`Listening on port: ${port}`);
});

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);
}

module.exports = app;
Loading