-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: new "run context" management to fix numerous span hierarchy issu…
…es (#2181) This makes a number of changes to the `Instrumentation` API that is used by the instrumentations under "lib/instrumentation/modules/". Tracking the "current transaction" and "current span" is now entirely encapsulated in tracking the current "run context" (see `RunContext.js`) via a "run context manager" singleton (`agent._instrumentation._runCtxMgr`). This is the thing that uses `async_hooks` (`AsyncHooksRunContextManager`) or not (`BasicRunContextManager`) to track the run context for the currently running JS. They use an interface (and some implementation) very similar to OTel's ContextManager. The primary reason our context management can't be closer to OTel's is because our `apm.startTransaction(...)`, `apm.startSpan(...)`, `span.end()`, et al APIs **change the current run context**. # Instrumentation API used for run context tracking - **Part** of run context tracking is handled by an async-hook tracking new async tasks. The rest of run context tracking is in explicit calls to "bind" a function call to a particular run context. ins.bindEmitter(ee) // (unchanged) bind added event handlers to the curr run context ins.bindFunction(fn) // (unchanged) bind fn to the curr run context ins.bindFunctionToRunContext(rc, fn) // bind fn to a specific run context ins.bindFunctionToEmptyRunContext(fn) // an odd ball used to explicitly break run context ins.withRunContext(rc, fn, thisArg, ...args) // Equivalent to binding `fn` then calling it, but with less overhead. - Creating and ending transactions and spans: ins.startTransaction(...) -> trans // (unchanged) ins.startSpan(...) -> span // (unchanged) ins.createSpan(...) -> span // Create span, but don't change the current run context. ins.addEndedTransaction(trans) // (unchanged) ins.addEndedSpan(trans) // (unchanged) - Getting and working with run contexts: ins.currRunContext() -> rc // Get the current run context. // The following are mostly used internally by above methods. ins.supersedeWithTransRunContext(trans) ins.supersedeWithSpanRunContext(span) ins.supersedeWithEmptyRunContext() # Behavior changes This makes *no* changes to the public API. There are, however, the following changes in behavior. 1. If user code creates span A, then creates span B *in the same async task*: B will be a child of A. // BEFORE AFTER apm.startTransaction('t0') // transaction 't0' transaction 't0' apm.startSpan('s1') // |- span 's1' `- span 's1' apm.startSpan('s2') // `- span 's2' `- span 's2' 2. Before this PR, an ended transaction would linger as `apm.currentTransaction`. Not any more. Fixes: #1889 Fixes: #1239
- Loading branch information
Showing
96 changed files
with
1,942 additions
and
869 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
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.