-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't clobber manually-installed tracer with SSI
- Loading branch information
Showing
3 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |