Skip to content

Commit c7c6896

Browse files
committed
adjust example
1 parent 530fe53 commit c7c6896

File tree

1 file changed

+8
-19
lines changed
  • docs/platforms/javascript/common/enriching-events/process-isolation

1 file changed

+8
-19
lines changed

docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,16 @@ notSupported:
2121

2222
In server-side environments, the <PlatformLink to='/enriching-events/scopes'>isolation scope</PlatformLink> is automatically forked around request boundaries. This means that each request will have its own isolation scope, and data set on the isolation scope will only apply to events captured during that request. This is done automatically by the SDK.
2323

24-
However, the request isolation happens when the request callback itself is being executed. This means that if you e.g. have a middleware where you want to set Sentry data (e.g. `Sentry.setUser()` in an auth middleware), you have to manually fork the isolation scope with `Sentry.withIsolationScope()` - see [Using withIsolationScope](../scopes/#using-withisolationscope).
24+
However, tehre are also other cases where you may want to have isolation, for example in background jobs or when you want to isolate a specific part of your code. In these cases, you can use `Sentry.withIsolationScope()` to create a new isolation scope that is valid inside of the callback you pass to it - see [Using withIsolationScope](../scopes/#using-withisolationscope).
2525

26-
This is also necessary if you want to isolate a non-request process, e.g. a background job.
27-
28-
The following example shows how you can use `withIsolationScope` to attach a user and a tag in an auth middleware:
26+
The following example shows how you can use `withIsolationScope` to attach data for a specific job run:
2927

3028
```javascript
31-
const auth = (req, res, next) => {
32-
Sentry.withIsolationScope(() => {
33-
const authUser = findUserForHeader(req.headers["authorization"]);
34-
if (!authUser) {
35-
Sentry.setTag("Authenticated", false);
36-
Sentry.setUser(null);
37-
next(new Error("Authentication Error"));
38-
} else {
39-
Sentry.setTag("Authenticated", true);
40-
Sentry.setUser(authUser);
41-
next();
42-
}
29+
async function job(jobId) {
30+
return Sentry.withIsolationScope(async () => {
31+
// Only valid for events in this callback
32+
Sentry.setTag("jobId", jobId);
33+
await doSomething();
4334
});
44-
};
35+
}
4536
```
46-
47-
This way, the user & tag will only be attached to events captured during the request that passed the auth middleware.

0 commit comments

Comments
 (0)