Skip to content

Commit fcc9e4a

Browse files
committed
fix: skip thread stream
1 parent 026a25b commit fcc9e4a

File tree

3 files changed

+40
-48
lines changed

3 files changed

+40
-48
lines changed

packages/collector/src/index.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@ if (isNodeJsTooOld()) {
3333
// @ts-ignore TS1108 (return can only be used within a function body)
3434
return;
3535
}
36-
const skipPinoWorker = require('./util/skipPinoWorker');
37-
// Skip Instana instrumentation in Pino thread-stream workers.
38-
// Pino uses internal worker threads for its "thread-stream" transport.
39-
// These threads are not part of the main application logic and should
40-
// not be instrumented.
41-
//
42-
// This prevents errors when the collector is loaded via NODE_OPTIONS in worker threads.
43-
if (skipPinoWorker()) {
44-
// eslint-disable-next-line no-console
45-
console.warn('[Instana] Skipping instrumentation in Pino thread-stream worker.');
46-
module.exports = function noOp() {};
47-
module.exports.default = function noOp() {};
48-
// @ts-ignore
49-
return;
50-
}
51-
5236
let isMainThread = true;
5337
try {
5438
isMainThread = require('worker_threads').isMainThread;

packages/collector/src/util/skipPinoWorker.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

packages/core/src/util/excludedFromInstrumentation.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,50 @@
1313
// no value and also creates confusing log output, so we exclude them here explicitly.
1414
const excludePattern = /^.*\/(?:npm(?:\.js)?|npm-cli(?:\.js)?|yarn(?:\.js)?|yarn\/lib\/cli(?:\.js)?)$/i;
1515

16+
/**
17+
* Check if the current process is a Pino thread-stream worker thread
18+
* @returns {boolean}
19+
*/
20+
function isPinoThreadStreamWorker() {
21+
try {
22+
const { isMainThread, workerData } = require('worker_threads');
23+
if (isMainThread) return false;
24+
25+
if (workerData && typeof workerData === 'object') {
26+
const nested = workerData.workerData;
27+
const isPinoWorker =
28+
typeof workerData.filename === 'string' &&
29+
nested &&
30+
nested.$context &&
31+
typeof nested.$context.threadStreamVersion === 'string';
32+
33+
if (isPinoWorker) {
34+
return true;
35+
}
36+
}
37+
return false;
38+
} catch {
39+
return false;
40+
}
41+
}
42+
1643
/**
1744
* @type {Function}
1845
* @returns {boolean}
1946
*/
2047
module.exports = exports = function isExcludedFromInstrumentation() {
2148
const mainModule = process.argv[1];
22-
const excludedFromInstrumentation = typeof mainModule === 'string' && excludePattern.test(mainModule);
49+
let excludedFromInstrumentation = typeof mainModule === 'string' && excludePattern.test(mainModule);
50+
let exclusionReason = 'npm or yarn';
51+
52+
// Skip Instana instrumentation in Pino thread-stream workers.
53+
// Pino uses internal worker threads for its "thread-stream" transport.
54+
// These threads are not part of the main application logic and should
55+
// not be instrumented.
56+
if (!excludedFromInstrumentation && isPinoThreadStreamWorker()) {
57+
excludedFromInstrumentation = true;
58+
exclusionReason = 'Pino thread-stream worker';
59+
}
2360

2461
if (excludedFromInstrumentation) {
2562
const logLevelIsDebugOrInfo =
@@ -32,7 +69,8 @@ module.exports = exports = function isExcludedFromInstrumentation() {
3269
// eslint-disable-next-line no-console
3370
console.log(
3471
`[Instana] INFO: Not instrumenting process ${process.pid}: ${process.argv[0]} ${mainModule}` +
35-
' - this Node.js process seems to be npm or yarn. A child process started via "npm start" or "yarn start" ' +
72+
// eslint-disable-next-line max-len
73+
` - this Node.js process seems to be ${exclusionReason}. A child process started via "npm start" or "yarn start" ` +
3674
'_will_ be instrumented, but not npm or yarn itself.'
3775
);
3876
}

0 commit comments

Comments
 (0)