Skip to content

Commit

Permalink
debug warnings when init after instrumented modules
Browse files Browse the repository at this point in the history
  • Loading branch information
tlhunter committed May 15, 2024
1 parent 79f0d64 commit 5928bc6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 31 deletions.
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)
}
}
32 changes: 1 addition & 31 deletions packages/datadog-esbuild/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const instrumentations = require('../datadog-instrumentations/src/helpers/instrumentations.js')
const hooks = require('../datadog-instrumentations/src/helpers/hooks.js')
const extractPackageAndModulePath = require('../datadog-core/src/utils/src/extract-package-and-module-path')

for (const hook of Object.values(hooks)) {
hook()
Expand All @@ -21,7 +22,6 @@ for (const instrumentation of Object.values(instrumentations)) {
}
}

const NM = 'node_modules/'
const INSTRUMENTED = Object.keys(instrumentations)
const RAW_BUILTINS = require('module').builtinModules
const CHANNEL = 'dd-trace:bundler:load'
Expand Down Expand Up @@ -181,33 +181,3 @@ function dotFriendlyResolve (path, directory) {

return require.resolve(path, { paths: [directory] })
}

/**
* 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' }
*/
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)
}
}
39 changes: 39 additions & 0 deletions packages/dd-trace/src/check_require_cache.js
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)) {

Check failure on line 26 in packages/dd-trace/src/check_require_cache.js

View workflow job for this annotation

GitHub Actions / lint

'pathToModule' is never reassigned. Use 'const' instead
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

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 121. Maximum allowed is 120

Check failure on line 32 in packages/dd-trace/src/check_require_cache.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 129. Maximum allowed is 120

Check failure on line 38 in packages/dd-trace/src/check_require_cache.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 38 in packages/dd-trace/src/check_require_cache.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
}
4 changes: 4 additions & 0 deletions packages/dd-trace/src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const NoopDogStatsDClient = require('./noop/dogstatsd')
const spanleak = require('./spanleak')
const { SSITelemetry } = require('./profiling/ssi-telemetry')
const telemetryLog = require('dc-polyfill').channel('datadog:telemetry:log')
const checkRequireCache = require('./check_require_cache')

class LazyModule {
constructor (provider) {
Expand Down Expand Up @@ -55,8 +56,11 @@ class Tracer extends NoopProxy {

try {
const config = new Config(options) // TODO: support dynamic code config

telemetry.start(config, this._pluginManager)

if (config.debug) checkRequireCache()

if (config.dogstatsd) {
// Custom Metrics
this.dogstatsd = new dogstatsd.CustomMetrics(config)
Expand Down

0 comments on commit 5928bc6

Please sign in to comment.