-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Isolate observation context per agent (#903)
- Loading branch information
1 parent
7f5cd4b
commit 85887c8
Showing
10 changed files
with
110 additions
and
31 deletions.
There are no files selected for viewing
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { gosNREUM } from '../window/nreum' | ||
import { bundleId } from '../ids/bundle-id' | ||
import { EventContext } from './event-context' | ||
|
||
export class ObservationContextManager { | ||
// These IDs are provided for backwards compatibility until the agent is fully updated | ||
// use the observation context class. | ||
|
||
static contextId = `nr@context:${bundleId}` | ||
static contextOriginalId = `nr@original:${bundleId}` | ||
static contextWrappedId = `nr@wrapped:${ObservationContextManager.contextId}` | ||
|
||
static getObservationContextByAgentIdentifier (agentIdentifier) { | ||
const nr = gosNREUM() | ||
return Object.keys(nr?.initializedAgents || {}).indexOf(agentIdentifier) > -1 | ||
? nr.initializedAgents[agentIdentifier].observationContext | ||
: undefined | ||
} | ||
|
||
/** | ||
* @type {WeakMap<WeakKey, {[key: string]: unknown}>} | ||
*/ | ||
#observationContext = new WeakMap() | ||
|
||
/** | ||
* Returns the observation context tied to the supplied construct. If there has been | ||
* no observation construct created, an empty object is created and stored as the current | ||
* context. | ||
* @param key {unknown} The construct being observed such as an XHR instance | ||
* @return {EventContext} An object of key:value pairs to track as | ||
* part of the observation | ||
*/ | ||
getCreateContext (key) { | ||
if (!this.#observationContext.has(key)) { | ||
this.#observationContext.set(key, new EventContext()) | ||
} | ||
|
||
return this.#observationContext.get(key) | ||
} | ||
|
||
/** | ||
* Set the observation context for an observed construct. If values of the context | ||
* need to be updated, they should be done so directly on the context. This function | ||
* is only for the setting of a whole context. | ||
* @param key {unknown} The construct being observed such as an XHR instance | ||
* @param value {EventContext} An object of key:value pairs to track as | ||
* part of the observation | ||
* @return {EventContext} The updated observation context | ||
*/ | ||
setContext (key, value) { | ||
this.#observationContext.set(key, value) | ||
|
||
return this.#observationContext.get(key) | ||
} | ||
} |
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 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