-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update guardrails to report telemetry in old node versions (#4949)
- Loading branch information
Showing
9 changed files
with
195 additions
and
171 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
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
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
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
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,67 @@ | ||
'use strict' | ||
|
||
/* eslint-disable no-var */ | ||
|
||
var path = require('path') | ||
var Module = require('module') | ||
var isTrue = require('./util').isTrue | ||
var log = require('./log') | ||
var telemetry = require('./telemetry') | ||
var nodeVersion = require('../../../../version') | ||
|
||
var NODE_MAJOR = nodeVersion.NODE_MAJOR | ||
|
||
// TODO: Test telemetry for Node <12. For now only bailout is tested for those. | ||
function guard (fn) { | ||
var initBailout = false | ||
var clobberBailout = false | ||
var forced = isTrue(process.env.DD_INJECT_FORCE) | ||
|
||
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. | ||
var resolvedInApp | ||
var entrypoint = process.argv[1] | ||
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) { | ||
var ourselves = path.normalize(path.join(__dirname, '..', '..', '..', '..', 'index.js')) | ||
if (ourselves !== resolvedInApp) { | ||
clobberBailout = true | ||
} | ||
} | ||
|
||
// If we're running via single-step install, and the runtime doesn't match | ||
// the engines field in package.json, then we should not initialize the tracer. | ||
if (!clobberBailout) { | ||
var engines = require('../../../../package.json').engines | ||
var minMajor = parseInt(engines.node.replace(/[^0-9]/g, '')) | ||
var version = process.versions.node | ||
if (NODE_MAJOR < minMajor) { | ||
initBailout = true | ||
telemetry([ | ||
{ name: 'abort', tags: ['reason:incompatible_runtime'] }, | ||
{ name: 'abort.runtime', tags: [] } | ||
]) | ||
log.info('Aborting application instrumentation due to incompatible_runtime.') | ||
log.info('Found incompatible runtime nodejs ' + version + ', Supported runtimes: nodejs ' + engines.node + '.') | ||
if (forced) { | ||
log.info('DD_INJECT_FORCE enabled, allowing unsupported runtimes and continuing.') | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!clobberBailout && (!initBailout || forced)) { | ||
var result = fn() | ||
telemetry('complete', ['injection_forced:' + (forced && initBailout ? 'true' : 'false')]) | ||
log.info('Application instrumentation bootstrapping complete') | ||
return result | ||
} | ||
} | ||
|
||
module.exports = guard |
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,32 @@ | ||
'use strict' | ||
|
||
/* eslint-disable no-var */ | ||
/* eslint-disable no-console */ | ||
|
||
var isTrue = require('./util').isTrue | ||
|
||
var DD_TRACE_DEBUG = process.env.DD_TRACE_DEBUG | ||
var DD_TRACE_LOG_LEVEL = process.env.DD_TRACE_LOG_LEVEL | ||
|
||
var logLevels = { | ||
trace: 20, | ||
debug: 20, | ||
info: 30, | ||
warn: 40, | ||
error: 50, | ||
critical: 50, | ||
off: 100 | ||
} | ||
|
||
var logLevel = isTrue(DD_TRACE_DEBUG) | ||
? Number(DD_TRACE_LOG_LEVEL) || logLevels.debug | ||
: logLevels.off | ||
|
||
var log = { | ||
debug: logLevel <= 20 ? console.debug.bind(console) : function () {}, | ||
info: logLevel <= 30 ? console.info.bind(console) : function () {}, | ||
warn: logLevel <= 40 ? console.warn.bind(console) : function () {}, | ||
error: logLevel <= 50 ? console.error.bind(console) : function () {} | ||
} | ||
|
||
module.exports = log |
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,78 @@ | ||
'use strict' | ||
|
||
/* eslint-disable no-var */ | ||
/* eslint-disable object-shorthand */ | ||
|
||
var fs = require('fs') | ||
var spawn = require('child_process').spawn | ||
var tracerVersion = require('../../../../package.json').version | ||
var log = require('./log') | ||
|
||
module.exports = sendTelemetry | ||
|
||
if (!process.env.DD_INJECTION_ENABLED) { | ||
module.exports = function () {} | ||
} | ||
|
||
if (!process.env.DD_TELEMETRY_FORWARDER_PATH) { | ||
module.exports = function () {} | ||
} | ||
|
||
if (!fs.existsSync(process.env.DD_TELEMETRY_FORWARDER_PATH)) { | ||
module.exports = function () {} | ||
} | ||
|
||
var metadata = { | ||
language_name: 'nodejs', | ||
language_version: process.versions.node, | ||
runtime_name: 'nodejs', | ||
runtime_version: process.versions.node, | ||
tracer_version: tracerVersion, | ||
pid: process.pid | ||
} | ||
|
||
var seen = [] | ||
function hasSeen (point) { | ||
if (point.name === 'abort') { | ||
// This one can only be sent once, regardless of tags | ||
return seen.includes('abort') | ||
} | ||
if (point.name === 'abort.integration') { | ||
// For now, this is the only other one we want to dedupe | ||
var compiledPoint = point.name + point.tags.join('') | ||
return seen.includes(compiledPoint) | ||
} | ||
return false | ||
} | ||
|
||
function sendTelemetry (name, tags) { | ||
var points = name | ||
if (typeof name === 'string') { | ||
points = [{ name: name, tags: tags || [] }] | ||
} | ||
if (['1', 'true', 'True'].indexOf(process.env.DD_INJECT_FORCE) !== -1) { | ||
points = points.filter(function (p) { return ['error', 'complete'].includes(p.name) }) | ||
} | ||
points = points.filter(function (p) { return !hasSeen(p) }) | ||
for (var i = 0; i < points.length; i++) { | ||
points[i].name = 'library_entrypoint.' + points[i].name | ||
} | ||
if (points.length === 0) { | ||
return | ||
} | ||
var proc = spawn(process.env.DD_TELEMETRY_FORWARDER_PATH, ['library_entrypoint'], { | ||
stdio: 'pipe' | ||
}) | ||
proc.on('error', function () { | ||
log.error('Failed to spawn telemetry forwarder') | ||
}) | ||
proc.on('exit', function (code) { | ||
if (code !== 0) { | ||
log.error('Telemetry forwarder exited with code ' + code) | ||
} | ||
}) | ||
proc.stdin.on('error', function () { | ||
log.error('Failed to write telemetry data to telemetry forwarder') | ||
}) | ||
proc.stdin.end(JSON.stringify({ metadata: metadata, points: points })) | ||
} |
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,10 @@ | ||
'use strict' | ||
|
||
/* eslint-disable object-shorthand */ | ||
|
||
function isTrue (str) { | ||
str = String(str).toLowerCase() | ||
return str === 'true' || str === '1' | ||
} | ||
|
||
module.exports = { isTrue: isTrue } |
Oops, something went wrong.