Skip to content

Commit

Permalink
use AsyncLocalStorage instead of our home-grown solutions (#4201)
Browse files Browse the repository at this point in the history
* use AsyncLocalStorage instead of our home-grown solutions

The comment in the file that selected a storage implementation suggested
just using AsyncLocalStorage once it supports triggerAsyncResource().
That said, literally zero of our code uses triggerAsyncResource(), so
this is assumed to be historical and no longer relevant.

Switching to stock AsyncLocalStorage will enable the usage of
TracingChannel in the future.

* self-contain profiling's AsyncLocalStorage channel usage

* remove flag detection
  • Loading branch information
bengl authored Oct 7, 2024
1 parent bba5f3d commit 7f93d36
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 303 deletions.
4 changes: 2 additions & 2 deletions packages/datadog-core/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const LocalStorage = require('./src/storage')
const { AsyncLocalStorage } = require('async_hooks')

const storage = new LocalStorage()
const storage = new AsyncLocalStorage()

module.exports = { storage }
108 changes: 0 additions & 108 deletions packages/datadog-core/src/storage/async_resource.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/datadog-core/src/storage/index.js

This file was deleted.

8 changes: 0 additions & 8 deletions packages/datadog-core/test/setup.js

This file was deleted.

20 changes: 0 additions & 20 deletions packages/datadog-core/test/storage/async_resource.spec.js

This file was deleted.

160 changes: 0 additions & 160 deletions packages/datadog-core/test/storage/test.js

This file was deleted.

40 changes: 40 additions & 0 deletions packages/dd-trace/src/profiling/profilers/wall.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,44 @@ function getWebTags (startedSpans, i, span) {
return memoize(null)
}

let channelsActivated = false
function ensureChannelsActivated () {
if (channelsActivated) return

const { AsyncLocalStorage, createHook } = require('async_hooks')
const shimmer = require('../../../../datadog-shimmer')

createHook({ before: () => beforeCh.publish() }).enable()

let inRun = false
shimmer.wrap(AsyncLocalStorage.prototype, 'enterWith', function (original) {
return function (...args) {
const retVal = original.apply(this, args)
if (!inRun) enterCh.publish()
return retVal
}
})

shimmer.wrap(AsyncLocalStorage.prototype, 'run', function (original) {
return function (store, callback, ...args) {
const wrappedCb = shimmer.wrapFunction(callback, cb => function (...args) {
inRun = false
enterCh.publish()
const retVal = cb.apply(this, args)
inRun = true
return retVal
})
inRun = true
const retVal = original.call(this, store, wrappedCb, ...args)
enterCh.publish()
inRun = false
return retVal
}
})

channelsActivated = true
}

class NativeWallProfiler {
constructor (options = {}) {
this.type = 'wall'
Expand Down Expand Up @@ -121,6 +159,8 @@ class NativeWallProfiler {
start ({ mapper } = {}) {
if (this._started) return

ensureChannelsActivated()

this._mapper = mapper
this._pprof = require('@datadog/pprof')
kSampleCount = this._pprof.time.constants.kSampleCount
Expand Down

0 comments on commit 7f93d36

Please sign in to comment.