Skip to content

Commit

Permalink
use weakmap to avoid references from node to datadog stores
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Nov 28, 2024
1 parent ac19207 commit 0b6ddf6
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions packages/datadog-core/src/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@

const { AsyncLocalStorage } = require('async_hooks')

class DatadogStorage {
constructor () {
this._storage = new AsyncLocalStorage()
}

enterWith (store) {
const handle = {}
stores.set(handle, store)
this._storage.enterWith(handle)
}

getStore () {
const handle = this._storage.getStore()
return stores.get(handle)
}
}

const storages = Object.create(null)
const legacyStorage = new AsyncLocalStorage()
const legacyStorage = new DatadogStorage()

const storage = function (namespace) {
if (!storages[namespace]) {
storages[namespace] = new AsyncLocalStorage()
storages[namespace] = new DatadogStorage()
}
return storages[namespace]
}

storage.disable = legacyStorage.disable.bind(legacyStorage)
storage.enterWith = legacyStorage.enterWith.bind(legacyStorage)
storage.exit = legacyStorage.exit.bind(legacyStorage)
storage.getStore = legacyStorage.getStore.bind(legacyStorage)
storage.run = legacyStorage.run.bind(legacyStorage)

const stores = new WeakMap()

module.exports = storage

0 comments on commit 0b6ddf6

Please sign in to comment.