Skip to content

Commit 83c7c81

Browse files
committed
chore: fixes
1 parent b3d029e commit 83c7c81

5 files changed

Lines changed: 434 additions & 17 deletions

File tree

packages/collector/src/announceCycle/agentready.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,18 @@ function enter(_ctx) {
152152
// TODO: Add an EventEmitter functionality for the current process
153153
// such as `instana.on('instana.collector.initialized')`.
154154
// eslint-disable-next-line no-unused-expressions
155-
// process?.send?.('instana.collector.initialized');
155+
if (process.env.INSTANA_IPC_ENABLED === 'true') {
156+
process?.send?.('instana.collector.initialized');
156157

157-
/*
158-
if (!isMainThread) {
159-
const { parentPort } = require('worker_threads');
158+
if (!isMainThread) {
159+
const { parentPort } = require('worker_threads');
160160

161-
if (parentPort) {
162-
// CASE: This is for the worker thread if available.
163-
parentPort.postMessage('instana.collector.initialized');
161+
if (parentPort) {
162+
// CASE: This is for the worker thread if available.
163+
parentPort.postMessage('instana.collector.initialized');
164+
}
164165
}
165166
}
166-
*/
167167
}
168168

169169
function leave() {

packages/collector/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ function init(userConfig = {}) {
100100
}
101101

102102
if (collectorIndexCacheKey) {
103-
// process?.send?.('instana.collector.initialized');
103+
if (process.env.INSTANA_IPC_ENABLED === 'true') {
104+
process?.send?.('instana.collector.initialized');
105+
}
104106

105107
return require.cache[collectorIndexCacheKey].exports;
106108
} else {

packages/collector/test/test_util/ProcessControls.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ class ProcessControls {
151151
INSTANA_FIRE_MONITORING_EVENT_DURATION_IN_MS: 500,
152152
INSTANA_RETRY_AGENT_CONNECTION_IN_MS: 500,
153153
APP_USES_HTTPS: this.appUsesHttps ? 'true' : 'false',
154-
INSTANA_DISABLE_USE_OPENTELEMETRY: !this.enableOtelIntegration
154+
INSTANA_DISABLE_USE_OPENTELEMETRY: !this.enableOtelIntegration,
155+
INSTANA_IPC_ENABLED: 'true'
155156
},
156157
opts.env
157158
);

packages/core/src/util/requireHook.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,20 @@ function patchedModuleLoad(moduleName) {
6666
// However, when an ESM library imports a CommonJS package, our requireHook is triggered.
6767
// For native ESM libraries the iitmHook is triggered.
6868
if (path.isAbsolute(moduleName) && ['.node', '.json', '.ts'].indexOf(path.extname(moduleName)) === -1) {
69+
// Normalize Windows paths (backslashes) to forward slashes for regex matching
70+
const normalizedModuleName = moduleName.replace(/\\/g, '/');
6971
// EDGE CASE for ESM: mysql2/promise.js
70-
if (moduleName.indexOf('node_modules/mysql2/promise.js') !== -1) {
72+
if (normalizedModuleName.indexOf('node_modules/mysql2/promise.js') !== -1) {
7173
moduleName = 'mysql2/promise';
7274
} else {
7375
// e.g. path is node_modules/@elastic/elasicsearch/index.js
74-
let match = moduleName.match(/node_modules\/(@.*?(?=\/)\/.*?(?=\/))/);
76+
let match = normalizedModuleName.match(/node_modules\/(@.*?(?=\/)\/.*?(?=\/))/);
7577

7678
if (match && match.length > 1) {
7779
moduleName = match[1];
7880
} else {
7981
// e.g. path is node_modules/mysql/lib/index.js
80-
match = moduleName.match(/node_modules\/(.*?(?=\/))/);
82+
match = normalizedModuleName.match(/node_modules\/(.*?(?=\/))/);
8183

8284
if (match && match.length > 1) {
8385
moduleName = match[1];
@@ -145,8 +147,11 @@ function patchedModuleLoad(moduleName) {
145147
}
146148

147149
if (!cacheEntry.byFileNamePatternTransformersApplied) {
150+
// Normalize Windows paths (backslashes) to forward slashes for regex pattern matching
151+
// This ensures patterns with forward slashes (like /\/mongodb-core\/lib\/connection\/pool\.js/) work on Windows
152+
const normalizedFilename = filename.replace(/\\/g, '/');
148153
for (let i = 0; i < byFileNamePatternTransformers.length; i++) {
149-
if (byFileNamePatternTransformers[i].pattern.test(filename)) {
154+
if (byFileNamePatternTransformers[i].pattern.test(normalizedFilename)) {
150155
cacheEntry.moduleExports =
151156
byFileNamePatternTransformers[i].fn(cacheEntry.moduleExports, filename) || cacheEntry.moduleExports;
152157
}
@@ -191,15 +196,27 @@ exports.buildFileNamePattern = function buildFileNamePattern(arr) {
191196
return new RegExp(`${arr.reduce(buildFileNamePatternReducer, '')}$`);
192197
};
193198

199+
/**
200+
* Escapes special regex characters in a string
201+
* @param {string} str
202+
* @returns {string}
203+
*/
204+
function escapeRegex(str) {
205+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
206+
}
207+
194208
/**
195209
* @param {string} agg
196210
* @param {string} pathSegment
197211
* @returns {string}
198212
*/
199213
function buildFileNamePatternReducer(agg, pathSegment) {
200214
if (agg.length > 0) {
201-
agg += `\\${path.sep}`;
215+
// Always use forward slashes in patterns since we normalize filenames to forward slashes
216+
// This ensures patterns work consistently on both Windows and Unix systems
217+
agg += '\\/';
202218
}
203-
agg += pathSegment;
219+
// Escape special regex characters in path segments (e.g., '.' in 'express.js' should be '\.')
220+
agg += escapeRegex(pathSegment);
204221
return agg;
205222
}

0 commit comments

Comments
 (0)