Skip to content

Commit

Permalink
fix(next, redis, mongodb): ensure module sub-path RITM hooks are appl…
Browse files Browse the repository at this point in the history
…ied on Windows (elastic#3905)

For some instrumentations we path module sub-paths (e.g. mongodb/lib/foo.js).
On Windows RITM returns modPath=mongodb\lib\foo.js. This change adds path
separator normalization so those values equate. Before this the module
patcher for that subpath was not applied.
  • Loading branch information
trentm authored and fpm-peter committed Aug 20, 2024
1 parent 4cd9255 commit 00f0ff8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ See the <<upgrade-to-v4>> guide.
included changes that resulted in the APM agent's instrumentation breaking it.
({pull}3897[#3897])
* Fix a path normalization issue that broke (or partially broke) instrumentation
of some modules on Windows: Next.js, redis v4+, mongodb. ({pull}3905[#3905])
[float]
===== Chores
Expand Down
8 changes: 8 additions & 0 deletions lib/instrumentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ function modNameFromModPath(modPath) {
}
}

function normPathSeps(s) {
return path.sep !== '/' ? s.split(path.sep).join('/') : s;
}

/**
* Holds the registered set of "patchers" (functions that monkey patch imported
* modules) for a module path (`modPath`).
Expand Down Expand Up @@ -558,6 +562,10 @@ Instrumentation.prototype._restartHooks = function () {
if (self._lambdaHandlerInfo?.modName === modPath) {
modPath = self._lambdaHandlerInfo.filePath;
version = process.env.AWS_LAMBDA_FUNCTION_VERSION || '';
} else {
// RITM returns `modPath` using native path separators. However,
// _patcherReg is keyed with '/' separators, so we need to normalize.
modPath = normPathSeps(modPath);
}

if (!self._patcherReg.has(modPath)) {
Expand Down
5 changes: 0 additions & 5 deletions test/instrumentation/modules/next/next.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

'use strict';

if (process.env.GITHUB_ACTIONS === 'true' && process.platform === 'win32') {
console.log('# SKIP: GH Actions do not support docker services on Windows');
process.exit(0);
}

// Test Next.js instrumentation.
//
// This test roughly does the following:
Expand Down

0 comments on commit 00f0ff8

Please sign in to comment.