Skip to content

Commit 8224577

Browse files
committed
chore: reproduced + fixed
1 parent 54af1ea commit 8224577

File tree

7 files changed

+82
-8
lines changed

7 files changed

+82
-8
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2025
3+
*/
4+
5+
'use strict';
6+
7+
const path = require('path');
8+
const childProcess = require('child_process');
9+
const expect = require('chai').expect;
10+
const supportedVersion = require('@instana/core').tracing.supportedVersion;
11+
const config = require('../../../../../core/test/config');
12+
const mochaSuiteFn = supportedVersion(process.versions.node) ? describe : describe.skip;
13+
14+
mochaSuiteFn('tracing/invalidApp', function () {
15+
this.timeout(config.getTestTimeout() * 3);
16+
17+
it('when the collector is required in interactive shell', cb => {
18+
const child = childProcess.spawn(
19+
process.execPath,
20+
['--require', path.join(__dirname, '..', '..', '..', '..', 'src', 'immediate.js')],
21+
{
22+
stdio: 'inherit',
23+
cwd: process.cwd(),
24+
env: process.env
25+
}
26+
);
27+
28+
child.on('error', err => {
29+
cb(err);
30+
});
31+
32+
child.on('exit', (code, signal) => {
33+
expect(signal).to.equal('SIGTERM');
34+
expect(code).to.not.exist;
35+
cb();
36+
});
37+
38+
setTimeout(() => {
39+
child.kill('SIGTERM');
40+
}, 3 * 1000);
41+
});
42+
});

packages/core/src/util/applicationUnderMonitoring.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,17 @@ function getMainPackageJsonPathStartingAtDirectory(startDirectory, cb) {
187187
}
188188
}
189189

190-
// In case if require.main or process.argv[1] might be missing or invalid,
191-
// so mainModule.filename can be undefined.
190+
// CASE: node --require .../src/immediate.js
192191
if (!mainModule?.filename) {
193192
logger.warn('Application entrypoint could not be identified. Package.json resolution will be skipped.');
194-
return process.nextTick(cb);
193+
const err = new Error('Application entrypoint could not be identified. Search for package.json failed.');
194+
195+
// @ts-ignore
196+
err.code = 'INSTANA_NO_MAIN_MODULE';
197+
198+
return process.nextTick(cb, err, null);
195199
}
200+
196201
startDirectory = path.dirname(mainModule.filename);
197202
}
198203

packages/serverless-collector/src/metrics/name.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ module.exports = config => {
2121
return process.env.INSTANA_SERVICE_NAME;
2222
}
2323

24+
// TODO: all metrics call `getMainPackageJsonStartingAtMainModule` - if `getMainPackageJsonStartingAtMainModule` fails
25+
// for the 1st metrics, the other metrics will try again...we should initiate
26+
// `getMainPackageJsonStartingAtMainModule` only once in a central place!
2427
return new Promise(resolve => {
2528
instanaCore.util.applicationUnderMonitoring.getMainPackageJsonStartingAtMainModule((err, packageJson) => {
2629
if (err) {
27-
logger.debug(`Failed to determine main package.json. ${err?.message} ${err?.stack}`);
30+
logger.debug(`Failed to determine main package.json for "name" metric. ${err?.message} ${err?.stack}`);
2831
return resolve();
2932
}
3033

packages/shared-metrics/src/description.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
const { payloadPrefix } = require('./activeHandles');
9+
810
const { applicationUnderMonitoring } = require('@instana/core').util;
911

1012
/** @type {import('@instana/core/src/core').GenericLogger} */
@@ -28,9 +30,14 @@ let attempts = 0;
2830
exports.activate = function activate() {
2931
attempts++;
3032

33+
// TODO: all metrics call `getMainPackageJsonStartingAtMainModule` - if `getMainPackageJsonStartingAtMainModule` fails
34+
// for the 1st metrics, the other metrics will try again...we should initiate
35+
// `getMainPackageJsonStartingAtMainModule` only once in a central place!
3136
applicationUnderMonitoring.getMainPackageJsonStartingAtMainModule((err, packageJson) => {
3237
if (err) {
33-
return logger.warn(`Failed to determine main package json. Reason: ${err?.message} ${err?.stack}`);
38+
return logger.warn(
39+
`Failed to determine main package.json for "${payloadPrefix}" metric. Reason: ${err?.message} ${err?.stack}`
40+
);
3441
} else if (!packageJson && attempts < MAX_ATTEMPTS) {
3542
setTimeout(exports.activate, DELAY).unref();
3643
return;

packages/shared-metrics/src/keywords.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ let attempts = 0;
2929
exports.activate = function activate() {
3030
attempts++;
3131

32+
// TODO: all metrics call `getMainPackageJsonStartingAtMainModule` - if `getMainPackageJsonStartingAtMainModule` fails
33+
// for the 1st metrics, the other metrics will try again...we should initiate
34+
// `getMainPackageJsonStartingAtMainModule` only once in a central place!
3235
applicationUnderMonitoring.getMainPackageJsonStartingAtMainModule((err, packageJson) => {
3336
if (err) {
34-
return logger.warn(`Failed to determine main package json. Reason: ${err?.message} ${err?.stack}`);
37+
return logger.warn(
38+
// eslint-disable-next-line max-len
39+
`Failed to determine main package.json for "${exports.payloadPrefix}" metric. Reason: ${err?.message} ${err?.stack}`
40+
);
3541
} else if (!packageJson && attempts < MAX_ATTEMPTS) {
3642
setTimeout(exports.activate, DELAY).unref();
3743

packages/shared-metrics/src/name.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ let attempts = 0;
2828
exports.activate = function activate() {
2929
attempts++;
3030

31+
// TODO: all metrics call `getMainPackageJsonStartingAtMainModule` - if `getMainPackageJsonStartingAtMainModule` fails
32+
// for the 1st metrics, the other metrics will try again...we should initiate
33+
// `getMainPackageJsonStartingAtMainModule` only once in a central place!
3134
applicationUnderMonitoring.getMainPackageJsonStartingAtMainModule((err, packageJson) => {
3235
if (err) {
33-
return logger.warn(`Failed to determine main package json. Reason: ${err?.message} ${err?.stack}`);
36+
return logger.warn(
37+
`Failed to determine main package.json for "${exports.payloadPrefix}". Reason: ${err?.message} ${err?.stack}`
38+
);
3439
} else if (!packageJson && attempts < exports.MAX_ATTEMPTS) {
3540
logger.debug('Main package.json could not be found. Will try again later.');
3641
setTimeout(exports.activate, exports.DELAY).unref();

packages/shared-metrics/src/version.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ let attempts = 0;
2828
exports.activate = function activate() {
2929
attempts++;
3030

31+
// TODO: all metrics call `getMainPackageJsonStartingAtMainModule` - if `getMainPackageJsonStartingAtMainModule` fails
32+
// for the 1st metrics, the other metrics will try again...we should initiate
33+
// `getMainPackageJsonStartingAtMainModule` only once in a central place!
3134
applicationUnderMonitoring.getMainPackageJsonStartingAtMainModule((err, packageJson) => {
3235
if (err) {
33-
return logger.warn(`Failed to determine main package json. Reason: ${err?.message} ${err?.stack}`);
36+
return logger.warn(
37+
// eslint-disable-next-line max-len
38+
`Failed to determine main package.json for "${exports.payloadPrefix}" metric. Reason: ${err?.message} ${err?.stack}`
39+
);
3440
} else if (!packageJson && attempts < MAX_ATTEMPTS) {
3541
setTimeout(exports.activate, DELAY).unref();
3642

0 commit comments

Comments
 (0)