Store sampling context in the AsyncContextFrame directly#255
Open
Store sampling context in the AsyncContextFrame directly#255
Conversation
Overall package sizeSelf size: 1.81 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | source-map | 0.7.6 | 185.63 kB | 185.63 kB | | pprof-format | 2.2.1 | 163.06 kB | 163.06 kB | | p-limit | 3.1.0 | 7.75 kB | 13.78 kB | | delay | 5.0.0 | 11.17 kB | 11.17 kB | | node-gyp-build | 3.9.0 | 8.81 kB | 8.81 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
bb7b1ea to
11f315e
Compare
067ef72 to
a425da6
Compare
30c38cb to
9f0aa90
Compare
…heir deregistration through StopImpl->Dispose. This was usually not a problem but test-get-value-from-map-profiler.ts creates a profiler without starting it.
This was usually not a problem but test-get-value-from-map-profiler.ts creates a profiler without starting it.
9f0aa90 to
053743d
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?:
AsyncContextFrame(ACF) object in the CPED with a proxy that has an internal field for our sample context.AsyncLocalStorageas the key will allow the sampling context object to be retrieved from JavaScript usingstorage.getStore()on that object. This opens the possibility of implementing custom contexts in dd-trace-js.createContextHoldermethod for direct use of custom contexts withAsyncLocalStorage.run()Motivation:
Simplification, performance, and additional capabilities.
Simplification: Storing the sampling context directly in the ACF reduces the runtime complexity by not having a proxy object. Arguably, there's the added complexity of having the raw memory map reading code, but there's only so much complexity you can reduce and the remaining inherent complexity you can only push around from one place to another.
Performance: We also no longer need to ensure that the proxy object is updated every time a new async context is created, thus dd-trace-js no longer needs to instrument
AsyncLocalStorage.enterWith, eliminating the need to do any JS work in any callback on context creation or switches. I think this is pretty significant. See https://github.com/DataDog/dd-trace-js/tree/szegedi/acf-simple for the dd-trace-js side of changes.Additional capabilities: with the addition of
createContextHolderit will be possible to implement an API likeprofiler.runWithCustomLabels(labels, fn)in dd-trace-js.Additional Notes:
While storing the sampling context is now simpler, retrieving it from the signal handler actually got more complex, but that's a tradeoff we should make. Namely, in the signal handler now we need to chase pointers from current isolate to the hash table backing the ACF storage, and then also implement the hash table lookup-by-key. None of these data structures are published by V8, but we're defining equivalent ones that hold for the currently used V8 versions. They would also need to be updated if V8 internally changes them, but we have enough tests to detect if they no longer work.
For review, let me note that the first 5 commits are reverts of all the commits for the previous approach, so they just establish a clean slate on which to build the new approach. Few of the commits are just tests, or fixes for small things, or adjustments to make the big ones easier.
The consequential changes are all in these 4 commits:
If needed, I could probably pull this apart into several smaller PRs, let me know. The last two could even be separated into their own PRs as they're additional functionality.
How to test the change?
All existing CPED tests pass, and I've added some additional tests in
test-get-value-from-map-profiler.ts. Funnily enough, for these tests I was creating profiler instances, but not starting them, which exposed (through ASAN, bless its name) some asymmetry in construction and destruction ofWallProfilerthat's not normally problematic (as normallytime-profiler.tsalways starts a created instance) but I fixed them anyway (specifically, registration of GC callbacks got moved from constructor toStartImpland detachment and destruction of live context pointers got moved fromDisposeto the actual~WallProfiler.