Skip to content

Commit

Permalink
don't clobber manually-installed tracer with SSI
Browse files Browse the repository at this point in the history
  • Loading branch information
bengl committed May 14, 2024
1 parent 4736996 commit 63f897c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
28 changes: 25 additions & 3 deletions init.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
'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) {
// 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]
try {
resolvedInApp = Module.createRequire(entrypoint).resolve('dd-trace')
} catch (e) {}
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()

0 comments on commit 63f897c

Please sign in to comment.