You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0122-sdk-hub-scope-merge.md
+17-20Lines changed: 17 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,26 +196,11 @@ async function setupGlobalData() {
196
196
}
197
197
```
198
198
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.
200
200
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.
202
202
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!
// When init() is called, we make sure to inherit from the global scope, if data was set before
211
-
Sentry.init();
212
-
213
-
constglobalScope=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.
219
204
220
205
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.
221
206
@@ -253,9 +238,9 @@ scope.isolate();
253
238
export function withIsolationScope(callback: (scope) => void): void;
254
239
```
255
240
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.
257
242
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.
259
244
260
245
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.
261
246
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 () {
278
263
279
264
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.
280
265
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
+
281
278
### How to document isolation scopes
282
279
283
280
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