-
Notifications
You must be signed in to change notification settings - Fork 232
test: refactor to move knowledge for in-process Agent re-use into Agent methods #2305
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
Changes from all commits
9dbded4
43511d9
7b96ccc
a9c20bc
6beb6ac
b5d2181
cdc05b0
3219cfc
6810173
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,6 @@ if (packageName === 'elastic-apm-node') { | |
} | ||
var userAgent = `${packageName}/${version}` | ||
|
||
config.INTAKE_STRING_MAX_SIZE = 1024 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. REVIEWER NOTE: All the changes in this file were to export |
||
config.CAPTURE_ERROR_LOG_STACK_TRACES_NEVER = 'never' | ||
config.CAPTURE_ERROR_LOG_STACK_TRACES_MESSAGES = 'messages' | ||
config.CAPTURE_ERROR_LOG_STACK_TRACES_ALWAYS = 'always' | ||
|
||
module.exports = config | ||
|
||
let confFile = loadConfigFile() | ||
|
||
let serviceName, serviceVersion | ||
|
@@ -38,6 +31,11 @@ try { | |
serviceVersion = version | ||
} catch (err) {} | ||
|
||
const INTAKE_STRING_MAX_SIZE = 1024 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I generally prefer "export everything once at the bottom" approach, and I never liked relying on the |
||
const CAPTURE_ERROR_LOG_STACK_TRACES_NEVER = 'never' | ||
const CAPTURE_ERROR_LOG_STACK_TRACES_MESSAGES = 'messages' | ||
const CAPTURE_ERROR_LOG_STACK_TRACES_ALWAYS = 'always' | ||
|
||
var DEFAULTS = { | ||
abortedErrorThreshold: '25s', | ||
active: true, | ||
|
@@ -47,7 +45,7 @@ var DEFAULTS = { | |
asyncHooks: true, | ||
breakdownMetrics: true, | ||
captureBody: 'off', | ||
captureErrorLogStackTraces: config.CAPTURE_ERROR_LOG_STACK_TRACES_MESSAGES, | ||
captureErrorLogStackTraces: CAPTURE_ERROR_LOG_STACK_TRACES_MESSAGES, | ||
captureExceptions: true, | ||
captureHeaders: true, | ||
captureSpanStackTraces: true, | ||
|
@@ -562,8 +560,8 @@ function normalizeBools (opts, logger) { | |
} | ||
|
||
function truncateOptions (opts) { | ||
if (opts.serviceVersion) opts.serviceVersion = truncate(String(opts.serviceVersion), config.INTAKE_STRING_MAX_SIZE) | ||
if (opts.hostname) opts.hostname = truncate(String(opts.hostname), config.INTAKE_STRING_MAX_SIZE) | ||
if (opts.serviceVersion) opts.serviceVersion = truncate(String(opts.serviceVersion), INTAKE_STRING_MAX_SIZE) | ||
if (opts.hostname) opts.hostname = truncate(String(opts.hostname), INTAKE_STRING_MAX_SIZE) | ||
} | ||
|
||
function bytes (input) { | ||
|
@@ -685,7 +683,7 @@ function getBaseClientConfig (conf, agent) { | |
environment: conf.environment, | ||
|
||
// Sanitize conf | ||
truncateKeywordsAt: config.INTAKE_STRING_MAX_SIZE, | ||
truncateKeywordsAt: INTAKE_STRING_MAX_SIZE, | ||
truncateErrorMessagesAt: conf.errorMessageMaxLength, | ||
|
||
// HTTP conf | ||
|
@@ -720,3 +718,11 @@ function getBaseClientConfig (conf, agent) { | |
cloudMetadataFetcher: (new CloudMetadata(cloudProvider, conf.logger, conf.serviceName)) | ||
} | ||
} | ||
|
||
// Exports. | ||
module.exports = config | ||
module.exports.INTAKE_STRING_MAX_SIZE = INTAKE_STRING_MAX_SIZE | ||
module.exports.CAPTURE_ERROR_LOG_STACK_TRACES_NEVER = CAPTURE_ERROR_LOG_STACK_TRACES_NEVER | ||
module.exports.CAPTURE_ERROR_LOG_STACK_TRACES_MESSAGES = CAPTURE_ERROR_LOG_STACK_TRACES_MESSAGES | ||
module.exports.CAPTURE_ERROR_LOG_STACK_TRACES_ALWAYS = CAPTURE_ERROR_LOG_STACK_TRACES_ALWAYS | ||
module.exports.DEFAULTS = DEFAULTS |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,20 +3,12 @@ | |
const asyncHooks = require('async_hooks') | ||
const shimmer = require('./shimmer') | ||
|
||
// FOR INTERNAL TESTING PURPOSES ONLY! | ||
const resettable = process.env._ELASTIC_APM_ASYNC_HOOKS_RESETTABLE === 'true' | ||
let _asyncHook | ||
|
||
module.exports = function (ins) { | ||
const asyncHook = asyncHooks.createHook({ init, before, destroy }) | ||
const contexts = new WeakMap() | ||
|
||
if (resettable) { | ||
if (_asyncHook) _asyncHook.disable() | ||
_asyncHook = asyncHook | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
const activeSpans = new Map() | ||
const activeTransactions = new Map() | ||
let contexts = new WeakMap() | ||
|
||
Object.defineProperty(ins, 'currentTransaction', { | ||
get () { | ||
const asyncId = asyncHooks.executionAsyncId() | ||
|
@@ -32,7 +24,6 @@ module.exports = function (ins) { | |
} | ||
}) | ||
|
||
const activeSpans = new Map() | ||
Object.defineProperty(ins, 'activeSpan', { | ||
get () { | ||
const asyncId = asyncHooks.executionAsyncId() | ||
|
@@ -63,6 +54,18 @@ module.exports = function (ins) { | |
} | ||
}) | ||
|
||
shimmer.wrap(ins, 'stop', function (origStop) { | ||
return function wrappedStop () { | ||
asyncHook.disable() | ||
activeTransactions.clear() | ||
activeSpans.clear() | ||
contexts = new WeakMap() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
shimmer.unwrap(ins, 'addEndedTransaction') | ||
shimmer.unwrap(ins, 'stop') | ||
return origStop.call(this) | ||
} | ||
}) | ||
|
||
asyncHook.enable() | ||
|
||
function init (asyncId, type, triggerAsyncId, resource) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,25 @@ function Instrumentation (agent) { | |
} | ||
} | ||
|
||
// Stop active instrumentation and reset global state *as much as possible*. | ||
// | ||
// Limitations: Removing and re-applying 'require-in-the-middle'-based patches | ||
// has no way to update existing references to patched or unpatched exports from | ||
// those modules. | ||
Instrumentation.prototype.stop = function () { | ||
// Reset context tracking. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Appears to reset the appropriate properties. |
||
this.currentTransaction = null | ||
this.bindingSpan = null | ||
this.activeSpan = null | ||
|
||
// Reset patching. | ||
this._started = false | ||
if (this._hook) { | ||
this._hook.unhook() | ||
this._hook = null | ||
} | ||
} | ||
|
||
Object.defineProperty(Instrumentation.prototype, 'ids', { | ||
get () { | ||
const current = this.currentSpan || this.currentTransaction | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I did a quick scan of the work done in both the agent's constructor and agent.start() and the values reset here appear to be sufficient.