diff --git a/packages/dd-trace/src/guardrails/index.js b/packages/dd-trace/src/guardrails/index.js index 25daea00527..aef0f36ab65 100644 --- a/packages/dd-trace/src/guardrails/index.js +++ b/packages/dd-trace/src/guardrails/index.js @@ -8,12 +8,15 @@ var isTrue = require('./util').isTrue var log = require('./log') var telemetry = require('./telemetry') var nodeVersion = require('../../../../version') +var fs = require('fs') +var BUNDLE_THRESHOLD = 1024 * 256 // arbitrary 256 KB filesize threshold var NODE_MAJOR = nodeVersion.NODE_MAJOR function guard (fn) { var initBailout = false var clobberBailout = false + var bundleBailout = false var forced = isTrue(process.env.DD_INJECT_FORCE) var engines = require('../../../../package.json').engines var minMajor = parseInt(engines.node.replace(/[^0-9]/g, '')) @@ -36,6 +39,20 @@ function guard (fn) { clobberBailout = true } } + + // If the application has been bundled then we probably cannot instrument it since the require() calls are gone + // If the app was bundled using our ESBuild plugin it should have a copy of the tracer insde of it anyway. + + var contents = fs.readFileSync(entrypoint, 'utf8') + + // this only checks for ESBuild bundles, but there are many other bundlers to consider + if ( + contents.length > BUNDLE_THRESHOLD && + contents.indexOf('__defProp') !== -1 && + contents.indexOf('Bundled license information') !== -1 + ) { + bundleBailout = true + } } // If the runtime doesn't match the engines field in package.json, then we @@ -53,6 +70,18 @@ function guard (fn) { } } + if (bundleBailout) { + initBailout = true + telemetry([ + { name: 'abort', tags: ['reason:incompatible_bundle'] }, // TODO: what reason code + { name: 'abort.runtime', tags: [] } + ]) + log.info('Aborting application instrumentation since application is bundled.') + if (forced) { + log.info('DD_INJECT_FORCE enabled, allowing unsupported bundling and continuing.') + } + } + if (!clobberBailout && (!initBailout || forced)) { // Ensure the instrumentation source is set for the current process and potential // child processes.