Skip to content
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

don't clobber manually-installed tracer with SSI #4300

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 27 additions & 3 deletions init.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
'use strict'

const tracer = require('.')
const path = require('path')
const Module = require('module')

tracer.init()
let initBailout = false

module.exports = tracer
if (process.env.DD_INJECTION_ENABLED) {
rochdev marked this conversation as resolved.
Show resolved Hide resolved
// If we're running via single-step install, and we're not in the app's
// node_modules, then we should not initialize the tracer. This prevents
// single-step-installed tracer from clobbering the manually-installed tracer.
let resolvedInApp
const entrypoint = process.argv[1]
rochdev marked this conversation as resolved.
Show resolved Hide resolved
try {
resolvedInApp = Module.createRequire(entrypoint).resolve('dd-trace')
} catch (e) {
// Ignore. If we can't resolve the module, we assume it's not in the app.
}
if (resolvedInApp) {
const ourselves = path.join(__dirname, 'index.js')
if (ourselves !== resolvedInApp) {
initBailout = true
}
}
}

if (!initBailout) {
const tracer = require('.')
tracer.init()
module.exports = tracer
}
57 changes: 57 additions & 0 deletions integration-tests/init.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const {
createSandbox,
spawnProc
} = require('./helpers')
const { assert } = require('chai')
const path = require('path')

const DD_INJECTION_ENABLED = 'tracing'

describe('init.js', () => {
let cwd, proc, sandbox

async function runTest (cwd, env, expected) {
return new Promise((resolve, reject) => {
spawnProc(path.join(cwd, 'init/index.js'), { cwd, env }, data => {
try {
assert.strictEqual(data.toString(), expected)
resolve()
} catch (e) {
reject(e)
}
}).then(subproc => {
proc = subproc
})
})
}

before(async () => {
sandbox = await createSandbox()
cwd = sandbox.folder
})
afterEach(() => {
proc && proc.kill()
})
after(() => {
return sandbox.remove()
})

context('when dd-trace is not in the app dir', () => {
const NODE_OPTIONS = `--require ${path.join(__dirname, '..', 'init.js')}`
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS }, 'true\n')
})
it('should not initialize the tracer, if DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, DD_INJECTION_ENABLED }, 'false\n')
})
})
context('when dd-trace in the app dir', () => {
const NODE_OPTIONS = '--require dd-trace/init.js'
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS }, 'true\n')
})
it('should initialize the tracer, if DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, DD_INJECTION_ENABLED }, 'true\n')
})
})
})
3 changes: 3 additions & 0 deletions integration-tests/init/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// eslint-disable-next-line no-console
console.log(!!global._ddtrace)
process.exit()
Loading