Skip to content

Commit c286d3b

Browse files
committed
Update to make global scope truly global.
1 parent eeb576f commit c286d3b

2 files changed

Lines changed: 17 additions & 20 deletions

File tree

5.89 KB
Loading

text/0122-sdk-hub-scope-merge.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -196,26 +196,11 @@ async function setupGlobalData() {
196196
}
197197
```
198198

199-
The global scope is _not_ the initial scope, but a separate, special scope. The global scope is different for each client, in order to avoid polluting this on accident when working with multiple completely separate clients.
199+
The global scope is _not_ the initial scope, but a separate, special scope. The global scope is the same for all clients - if you care about per-client isolation, you should not use the global scope.
200200

201-
You can get the current global scope via `getGlobalScope()`. There _may_ be a function `setGlobalScope(scope)` to replace the global scope - or SDKs can decide that there is no need to replace the global scope, you can only mutate it.
201+
You can get the current global scope via `getGlobalScope()`. There _may_ be a function `setGlobalScope(scope)` to replace the global scope - or SDKs can decide that there is no need to replace the global scope, you can only mutate it.
202202

203-
If you call `getGlobalScope()` before a client is initialized, we should still get a global scope back (tied to a Noop client). Once an actual client is initialized, the global scope of the noop client should be merged into the new global scope for the new client. This should ensure that even if you call `getGlobalScope().setTag(...)` before the SDK is initialized, no data is lost:
204-
205-
```js
206-
// This global scope has a Noop client, because init was not called yet!
207-
const globalScopeBeforeInit = Sentry.getGlobalScope();
208-
globalScopeBeforeInit.setTag("tag1", "aa");
209-
210-
// When init() is called, we make sure to inherit from the global scope, if data was set before
211-
Sentry.init();
212-
213-
const globalScope = Sentry.getGlobalScope();
214-
// A new scope instance is created as global scope when a client is created & bound to current scope
215-
globalScope !== globalScopeBeforeInit;
216-
// Global scope inherits from globalScopeBeforeInit
217-
globalScope.getTags(); // -> tag1: aa
218-
```
203+
If you call `getGlobalScope()` before a client is initialized, we should still get a global scope back. This way, we can avoid race conditions if mutating the global scope before `Sentry.init()` is called.
219204

220205
The reason that the global scope is not the same as the initial scope of a client, is that you cannot accidentally mutate it - nothing ever inherits off the global scope.
221206

@@ -253,9 +238,9 @@ scope.isolate();
253238
export function withIsolationScope(callback: (scope) => void): void;
254239
```
255240

256-
You can fetch the current isolation scope via `getIsolationScope()`. You can define a new isolation scope via `scope.isolate()`, which will define a new isolation scope for this scope, and for all scopes that will be forked off this scope. When a client is created & bound, an isolation scope will immediately be created, similar to the global scope for a client.
241+
You can fetch the current isolation scope via `getIsolationScope()`. You can define a new isolation scope via `scope.isolate()`, which will define a new isolation scope for this scope, and for all scopes that will be forked off this scope. There is always an isolation scope, even if `Sentry.init()` was not called yet.
257242

258-
An isolation scope is attached to the current execution context, similar to the current scope. There is always exactly one current isolation scope. If you call `getIsolationScope()` before a client has been created, a noop isolation scope is returned, which should be merged in once a client is actually created (same as with the global scope).
243+
An isolation scope is attached to the current execution context, similar to the current scope. There is always exactly one current isolation scope.
259244

260245
Similar to the global scope, an isolation scope is always a separate scope, so nothing will inherit off it - except for a potential superseding isolation scope.
261246
If an isolation scope is created, and there is already an isolation scope in the current execution context, then the new isolation scope should be forked off the previous one (with copy-on-write):
@@ -278,6 +263,18 @@ app.get("/my-route", function () {
278263

279264
Note that in environments where you do not have multiple execution contexts (e.g. Browser JS), there is basically only a single isolation scope that is fundamentally the same as the global scope. In these environments, global & isolation scope can be used completely interchangeably. However, for consistency we still have these concepts everywhere - but users do not need to care about them, and we can reflect this in the SDK specific documentation.
280265

266+
### How does the isolation scope differ from the current scope?
267+
268+
If you are unsure why this exists, here an example:
269+
270+
```js
271+
app.get('/my-route', function() {
272+
Sentry.withScope(scope1 => {
273+
274+
})
275+
})
276+
```
277+
281278
### How to document isolation scopes
282279

283280
Isolation scopes will be documented in a new section under "Enriching Events > Per request data" (or similar, based on SDK language & needs), which will explain how to use `getIsolationScope()` and when/how data added there will be applied.

0 commit comments

Comments
 (0)