Skip to content

Commit

Permalink
fix(shared-metrics): fixed package.json not being found when app is E…
Browse files Browse the repository at this point in the history
…SM (#749)

- there was another case for esm apps
- you can either pass --experimental-loader={VAL} or --experimental-loader {VAL}
  • Loading branch information
kirrg001 authored Apr 6, 2023
1 parent b05bc23 commit c15569c
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 17 deletions.
6 changes: 4 additions & 2 deletions packages/core/src/util/applicationUnderMonitoring.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ function getMainPackageJsonPathStartingAtDirectory(startDirectory, cb) {
(process._preload_modules && process._preload_modules.length > 0) ||
(process.execArgv &&
process.execArgv.length > 0 &&
process.execArgv[0].indexOf('--experimental-loader') !== -1 &&
process.execArgv[0].indexOf('esm-loader.mjs') !== -1)
((process.execArgv[0].indexOf('--experimental-loader') !== -1 &&
process.execArgv[0].indexOf('esm-loader.mjs')) !== -1 ||
(process.execArgv[0].indexOf('--experimental-loader') !== -1 &&
process.execArgv[1].indexOf('esm-loader.mjs') !== -1)))
) {
// @ts-ignore
mainModule = {
Expand Down
3 changes: 3 additions & 0 deletions packages/shared-metrics/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
!test/util/dependencies_test_dir_02/node_modules/**
!test/util/dependencies_test_dir_03/node_modules
!test/util/dependencies_test_dir_03/node_modules/**
!test/esm-loader/module/node_modules
!test/esm-loader/module-2/node_modules
!test/esm-loader/module-3/node_modules
Empty file.
6 changes: 6 additions & 0 deletions packages/shared-metrics/test/esm-loader/module-2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "esm-loader-2",
"version": "1.0.0",
"description": "",
"dependencies": {}
}
26 changes: 26 additions & 0 deletions packages/shared-metrics/test/esm-loader/module-2/src/app.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* (c) Copyright IBM Corp. 2023
*/

'use strict';

import express from 'express';

const port = process.env.APP_PORT || 44004;
const app = express();
const logPrefix = `EJS preload collector: (${process.pid}):\t`;

app.get('/', (req, res) => {
res.send('OK');
});

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

function log() {
const args = Array.prototype.slice.call(arguments);
args[0] = `${logPrefix} (${process.pid}):\t${args[0]}`;
// eslint-disable-next-line no-console
console.log.apply(console, args);
}

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

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

6 changes: 6 additions & 0 deletions packages/shared-metrics/test/esm-loader/module-3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "esm-loader-3",
"version": "1.0.0",
"description": "",
"dependencies": {}
}
Empty file.
2 changes: 1 addition & 1 deletion packages/shared-metrics/test/esm-loader/module/src/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corp. 2022
* (c) Copyright IBM Corp. 2023
*/

'use strict';
Expand Down
74 changes: 60 additions & 14 deletions packages/shared-metrics/test/esm-loader/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,70 @@ const ProcessControls = require('../../../collector/test/test_util/ProcessContro
const mochaSuiteFn = semver.gte(process.versions.node, '12.0.0') ? describe : describe.skip;

mochaSuiteFn('ESM loader', function () {
this.timeout(config.getTestTimeout());
describe('case 1', function () {
this.timeout(config.getTestTimeout());

const controls = new ProcessControls({
useGlobalAgent: true,
cwd: path.join(__dirname, 'module'),
appPath: path.join(__dirname, 'module', 'src', 'app'),
execArgv: ['--experimental-loader=../../../../collector/esm-loader.mjs']
const controls = new ProcessControls({
useGlobalAgent: true,
cwd: path.join(__dirname, 'module'),
appPath: path.join(__dirname, 'module', 'src', 'app'),
execArgv: ['--experimental-loader=../../../../collector/esm-loader.mjs']
});

ProcessControls.setUpHooks(controls);

it('should be able to find package.json', async () => {
await testUtils.retry(() =>
controls.agentControls.getAllMetrics(controls.getPid()).then(metrics => {
const name = findMetric(metrics, ['name']);
expect(name).to.equal('esm-loader');
})
);
});
});

ProcessControls.setUpHooks(controls);
describe('case 2', function () {
this.timeout(config.getTestTimeout());

const controls = new ProcessControls({
useGlobalAgent: true,
cwd: path.join(__dirname, 'module'),
appPath: path.join(__dirname, 'module-2', 'src', 'app.mjs'),
execArgv: ['--experimental-loader', './../../../../collector/esm-loader.mjs']
});

ProcessControls.setUpHooks(controls);

it('should be able to find package.json', async () => {
await testUtils.retry(() =>
controls.agentControls.getAllMetrics(controls.getPid()).then(metrics => {
const name = findMetric(metrics, ['name']);
expect(name).to.equal('esm-loader-2');
})
);
});
});

describe('case 3', function () {
this.timeout(config.getTestTimeout());

const controls = new ProcessControls({
useGlobalAgent: true,
cwd: path.join(__dirname, 'module'),
appPath: path.join(__dirname, 'module-3', 'node_modules', 'my-app', 'server.mjs'),
execArgv: ['--experimental-loader', './../../../../collector/esm-loader.mjs']
});

ProcessControls.setUpHooks(controls);

it('should be able to find package.json', async () => {
await testUtils.retry(() =>
controls.agentControls.getAllMetrics(controls.getPid()).then(metrics => {
const name = findMetric(metrics, ['name']);
expect(name).to.equal('@instana/shared-metrics');
})
);
it('should be able to find package.json', async () => {
await testUtils.retry(() =>
controls.agentControls.getAllMetrics(controls.getPid()).then(metrics => {
const name = findMetric(metrics, ['name']);
expect(name).to.equal('my-app');
})
);
});
});
});

Expand Down

0 comments on commit c15569c

Please sign in to comment.