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..09a8f9ddc566f --- /dev/null +++ b/docs/platforms/javascript/common/enriching-events/process-isolation/index.mdx @@ -0,0 +1,36 @@ +--- +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, 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: + +```javascript +async function job(jobId) { + return Sentry.withIsolationScope(async () => { + // Only valid for events in this callback + Sentry.setTag("jobId", jobId); + await doSomething(); + }); +} +``` 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")); +```