Skip to content

Commit c2747b2

Browse files
mydeacoolguyzoneandreiborza
authored
fix(js): Clarify scope setting docs (#13635)
Closes https://linear.app/getsentry/issue/FE-405/user-feedback-elaborate-on-current-scope-usage --------- Co-authored-by: Alex Krawiec <[email protected]> Co-authored-by: Andrei <[email protected]>
1 parent db8fed9 commit c2747b2

File tree

1 file changed

+10
-11
lines changed
  • docs/platforms/javascript/common/enriching-events/scopes

1 file changed

+10
-11
lines changed

docs/platforms/javascript/common/enriching-events/scopes/index.mdx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,34 +106,33 @@ Sentry.captureException(new Error("my error"));
106106

107107
## Configuring the Scope
108108

109-
There are two main ways to interact with the scope. You can access the current scope via `Sentry.getCurrentScope()` and use setters on the resulting scope, or you can use global methods like `Sentry.setTag()` directly, which will set on the respective scope under the hood (which will be the isolation scope).
109+
In most cases, you should use top-level methods like `Sentry.setTag()` directly, which will set the data on the isolation scope, ensuring it is applied to all events related to the current request/process.
110110

111-
You'll first need to import the SDK, as usual:
111+
If you want to apply data to a more narrow scope, you can use `withScope` to create a new scope that is only active for a specific part of your code. `withScope` receives the scope as a callback argument, which you can use to set data on the scope.
112112

113113
<PlatformContent includePath="enriching-events/import" />
114114

115115
You can, for instance, add custom tags or inform Sentry about the currently authenticated user.
116116

117117
```javascript
118-
/// Usually, you don't want to write on the current scope, so use with care!
119-
const scope = Sentry.getCurrentScope();
120-
scope.setTag("my-tag", "my value");
121-
scope.setUser({
122-
id: 42,
123-
124-
});
125-
126-
// Or use the global methods (which will set data on the isolation scope):
127118
Sentry.setTag("my-tag", "my value");
128119
Sentry.setUser({
129120
id: 42,
130121
131122
});
123+
124+
// Alternatively, you can use `withScope` as documented below.
132125
```
133126

134127
To learn what useful information can be associated with scopes see
135128
[context](../context/), [tags](../tags), [users](../identify-user) and [breadcrumbs](../breadcrumbs/).
136129

130+
We recommend you avoid using `getCurrentScope()` and setting data on it, as the current scope can have an unreliable lifetime. Instead:
131+
132+
* If you want to apply data to the current request/process (in Node) or the current page view (in browser), use the top-level methods, e.g. `Sentry.setTag()`.
133+
* If you want to apply data to all events, no matter where they are captured, use methods on the global scope, e.g. `getGlobalScope().setTag()`
134+
* If you want to apply data to a more narrow scope, use `withScope` to create a new scope that is only active for a specific part of your code.
135+
137136
## Using `withScope`
138137

139138
In the following example we use <PlatformIdentifier name="with-scope" /> to attach a `level` and a `tag` to only one specific error:

0 commit comments

Comments
 (0)