From b8d03762eb80abfe84bc63319b81e0769dbed7c4 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Tue, 17 Sep 2024 09:32:17 +0200 Subject: [PATCH 1/4] feat: Add docs about process isolation in Node SDK --- .../process-isolation/index.mdx | 47 +++++++++++++++++++ .../common/enriching-events/scopes/index.mdx | 8 ++++ .../with-isolation-scope/javascript.mdx | 13 +++++ 3 files changed, 68 insertions(+) create mode 100644 docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx create mode 100644 platform-includes/enriching-events/scopes/with-isolation-scope/javascript.mdx diff --git a/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx b/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx new file mode 100644 index 0000000000000..bb1b110b4fc4b --- /dev/null +++ b/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx @@ -0,0 +1,47 @@ +--- +title: Process Isolation +description: "Learn more about how process isolation (or request isolation) works in the Sentry SDK." +supported: + - javascript.nextjs + - javascript.node + - javascript.connect + - javascript.express + - javascript.fastify + - javascript.hapi + - javascript.koa + - javascript.nestjs + - javascript.nuxt + - javascript.solidstart + - javascript.sveltekit + - javascript.astro + - javascript.remix +notSupported: + - javascript +--- + +In server-side environments, the isolation scope 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. + +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). + +This is also necessary if you want to isolate a non-request process, e.g. a background job. + +The following example shows how you can use `withIsolationScope` to attach a user and a tag in an auth middleware: + +```javascript +const auth = (req, res, next) => { + Sentry.withIsolationScope(() => { + const authUser = findUserForHeader(req.headers["authorization"]); + if (!authUser) { + Sentry.setTag("Authenticated", false); + Sentry.setUser(null); + next(new Error("Authentication Error")); + } else { + Sentry.setTag("Authenticated", true); + Sentry.setUser(authUser); + next(); + } + }); +}; +``` + +This way, the user & tag will only be attached to events captured during the request that passed the auth middleware. diff --git a/docs/platforms/javascript/common/enriching-events/scopes/index.mdx b/docs/platforms/javascript/common/enriching-events/scopes/index.mdx index 34ba5fa7a67e6..4890d84af2ef5 100644 --- a/docs/platforms/javascript/common/enriching-events/scopes/index.mdx +++ b/docs/platforms/javascript/common/enriching-events/scopes/index.mdx @@ -143,3 +143,11 @@ In the following example we use to atta 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 more easily isolate pieces of context information to specific locations in your code or even call to briefly remove all context information. + + +## Using `withIsolationScope` + +`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: + + + diff --git a/platform-includes/enriching-events/scopes/with-isolation-scope/javascript.mdx b/platform-includes/enriching-events/scopes/with-isolation-scope/javascript.mdx new file mode 100644 index 0000000000000..cd609f40519e3 --- /dev/null +++ b/platform-includes/enriching-events/scopes/with-isolation-scope/javascript.mdx @@ -0,0 +1,13 @@ +```javascript +Sentry.withIsolationScope(function () { + // This user & tag is set inside of this callback + Sentry.setUser({ id: "123" }); + Sentry.setTag("my-tag", "my value"); + + // will be tagged with my-tag="my value" & user + Sentry.captureException(new Error("my error")); +}); + +// will not be tagged with my-tag & user +Sentry.captureException(new Error("my other error")); +``` From 95b1c823f08f7a0f77c917799404e069313d0ae6 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 18 Sep 2024 09:57:22 +0200 Subject: [PATCH 2/4] adjust wording --- .../common/enriching-events/process-isolation/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx b/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx index bb1b110b4fc4b..12ac6024cc6dd 100644 --- a/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx +++ b/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx @@ -21,7 +21,7 @@ notSupported: In server-side environments, the isolation scope 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. -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). +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). This is also necessary if you want to isolate a non-request process, e.g. a background job. From 88bb94aa5fd2dee9c556150c762e17fbe417b1c0 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 18 Sep 2024 10:20:09 +0200 Subject: [PATCH 3/4] adjust example --- .../process-isolation/index.mdx | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx b/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx index 12ac6024cc6dd..6583bcb4cb6a8 100644 --- a/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx +++ b/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx @@ -21,27 +21,16 @@ notSupported: In server-side environments, the isolation scope 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. -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). +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). -This is also necessary if you want to isolate a non-request process, e.g. a background job. - -The following example shows how you can use `withIsolationScope` to attach a user and a tag in an auth middleware: +The following example shows how you can use `withIsolationScope` to attach data for a specific job run: ```javascript -const auth = (req, res, next) => { - Sentry.withIsolationScope(() => { - const authUser = findUserForHeader(req.headers["authorization"]); - if (!authUser) { - Sentry.setTag("Authenticated", false); - Sentry.setUser(null); - next(new Error("Authentication Error")); - } else { - Sentry.setTag("Authenticated", true); - Sentry.setUser(authUser); - next(); - } +async function job(jobId) { + return Sentry.withIsolationScope(async () => { + // Only valid for events in this callback + Sentry.setTag("jobId", jobId); + await doSomething(); }); -}; +} ``` - -This way, the user & tag will only be attached to events captured during the request that passed the auth middleware. From 73317d93a415a744909d25a754f63e26cc23c71f Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 18 Sep 2024 11:06:01 +0200 Subject: [PATCH 4/4] Update docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx Co-authored-by: Lukas Stracke --- .../common/enriching-events/process-isolation/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx b/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx index 6583bcb4cb6a8..09a8f9ddc566f 100644 --- a/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx +++ b/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx @@ -21,7 +21,7 @@ notSupported: In server-side environments, the isolation scope 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. -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). +However, there 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). The following example shows how you can use `withIsolationScope` to attach data for a specific job run: