Process Isolation

Learn more about how process isolation (or request isolation) works in the Sentry SDK.

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.

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:

Copied
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.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").