Skip to content

Commit 0c53967

Browse files
committed
feat: Add docs about process isolation in Node SDK
1 parent 64b65ea commit 0c53967

File tree

3 files changed

+68
-0
lines changed
  • docs/platforms/javascript/common/enriching-events
  • platform-includes/enriching-events/scopes/with-isolation-scope

3 files changed

+68
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: Process Isolation
3+
description: "Learn more about how process isolation (or request isolation) works in the Sentry SDK."
4+
supported:
5+
- javascript.nextjs
6+
- javascript.node
7+
- javascript.connect
8+
- javascript.express
9+
- javascript.fastify
10+
- javascript.hapi
11+
- javascript.koa
12+
- javascript.nestjs
13+
- javascript.nuxt
14+
- javascript.solidstart
15+
- javascript.sveltekit
16+
- javascript.astro
17+
- javascript.remix
18+
notSupported:
19+
- javascript
20+
---
21+
22+
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.
23+
24+
However, the request isolation happens when the request itself is processed. 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).
25+
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:
29+
30+
```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+
}
43+
});
44+
};
45+
```
46+
47+
This way, the user & tag will only be attached to events captured during the request that passed the auth middleware.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,11 @@ In the following example we use <PlatformIdentifier name="with-scope" /> to atta
143143
The scope inside the `withScope()` callback is only valid inside of the callback. Once the callback ends, the scope will be removed and no longer applied. The inner scope is only applied to events that are captured inside of the callback. `withScope()` will clone (or fork) the current scope, so that the current scope is not modified. This allows you to
144144
more easily isolate pieces of context information to specific locations in your code or
145145
even call <PlatformIdentifier name="clear" /> to briefly remove all context information.
146+
147+
<PlatformCategorySection supported={['server']}>
148+
## Using `withIsolationScope`
149+
150+
`withIsolationScope` works fundamentally the same as `withScope`, but it will fork the isolation scope instead of the current scope. Generally, the isolation scope is meant to be forked less frequently than the current scope, and in most cases the SDK will handle this automatically for you. But in cases where you e.g. want to capture SDK events in a middleware (which happens before the request is processed and thus before the SDKs automatic handling), or if you want to isolate a non-request process (e.g. a background job), you can use `withIsolationScope` to create a new isolation scope that is only active for the duration of the callback:
151+
152+
<PlatformContent includePath="enriching-events/scopes/with-isolation-scope" />
153+
</PlatformCategorySection>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
```javascript
2+
Sentry.withIsolationScope(function () {
3+
// This user & tag is set inside of this callback
4+
Sentry.setUser({ id: "123" });
5+
Sentry.setTag("my-tag", "my value");
6+
7+
// will be tagged with my-tag="my value" & user
8+
Sentry.captureException(new Error("my error"));
9+
});
10+
11+
// will not be tagged with my-tag & user
12+
Sentry.captureException(new Error("my other error"));
13+
```

0 commit comments

Comments
 (0)