-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
debug warnings when init after instrumented modules
- Loading branch information
Showing
4 changed files
with
77 additions
and
31 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/datadog-core/src/utils/src/extract-package-and-module-path.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
const NM = 'node_modules/' | ||
|
||
/** | ||
* For a given full path to a module, | ||
* return the package name it belongs to and the local path to the module | ||
* input: '/foo/node_modules/@co/stuff/foo/bar/baz.js' | ||
* output: { pkg: '@co/stuff', path: 'foo/bar/baz.js' } | ||
*/ | ||
module.exports = function extractPackageAndModulePath (fullPath) { | ||
const nm = fullPath.lastIndexOf(NM) | ||
if (nm < 0) { | ||
return { pkg: null, path: null } | ||
} | ||
|
||
const subPath = fullPath.substring(nm + NM.length) | ||
const firstSlash = subPath.indexOf('/') | ||
|
||
if (subPath[0] === '@') { | ||
const secondSlash = subPath.substring(firstSlash + 1).indexOf('/') | ||
|
||
return { | ||
pkg: subPath.substring(0, firstSlash + 1 + secondSlash), | ||
path: subPath.substring(firstSlash + 1 + secondSlash + 1) | ||
} | ||
} | ||
|
||
return { | ||
pkg: subPath.substring(0, firstSlash), | ||
path: subPath.substring(firstSlash + 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
const extractPackageAndModulePath = require('../../datadog-core/src/utils/src/extract-package-and-module-path') | ||
|
||
/** | ||
* The lowest hanging fruit to debug an app that isn't tracing | ||
* properly is to check that it is loaded before any modules | ||
* that need to be instrumented. This function checks the | ||
* `require.cache` to see if any instrumented modules have been | ||
* required already and prints a warning. | ||
* | ||
* Note that this only going to work for modules within npm | ||
* packages, like `express`, and not internal modules, like | ||
* `http`. | ||
* | ||
* The output isn't necessarily 100% perfect. For example if the | ||
* app loads a package we instrument but outside of an | ||
* unsupported version then a warning would still be displayed. | ||
* This is OK as the tracer should be loaded earlier regardless. | ||
*/ | ||
module.exports = function () { | ||
const packages = require('../../datadog-instrumentations/src/helpers/hooks') | ||
const naughties = new Set() | ||
let didWarn = false | ||
|
||
for (let pathToModule of Object.keys(require.cache)) { | ||
const { pkg } = extractPackageAndModulePath(pathToModule) | ||
|
||
if (naughties.has(pkg)) continue | ||
if (!(pkg in packages)) continue | ||
|
||
console.error(`Warning: The package '${pkg}' was loaded before dd-trace! This may cause errors with instrumentation`) | ||
Check failure on line 32 in packages/dd-trace/src/check_require_cache.js GitHub Actions / lint
|
||
|
||
naughties.add(pkg) | ||
didWarn = true | ||
} | ||
|
||
if (didWarn) console.error(`Warning: Please ensure dd-trace is loaded before any other modules to ensure proper functionality`) | ||
Check failure on line 38 in packages/dd-trace/src/check_require_cache.js GitHub Actions / lint
Check failure on line 38 in packages/dd-trace/src/check_require_cache.js GitHub Actions / lint
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters