Skip to content

guardrails: bail out if esbuild bundle #5988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/dd-trace/src/guardrails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, ''))
Expand All @@ -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
Expand All @@ -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.
Expand Down
Loading