-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Add docs for async context apis (#6641)
Co-authored-by: Shana Matthews <shana.l.matthews@gmail.com>
- Loading branch information
1 parent
d76fe81
commit 1a7626f
Showing
8 changed files
with
56 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: Async Context | ||
sidebar_order: 80 | ||
description: "Learn more about how to isolate Sentry scope and breadcrumbs across requests." | ||
--- | ||
|
||
You can use the `runWithAsyncContext` method to isolate Sentry scope and breadcrumbs to a single request if you are using SDK v7.48.0 or higher. This is useful if you are finding that breadcrumbs and scope are leaking across requests. | ||
|
||
```js | ||
const Sentry = require("@sentry/node"); | ||
|
||
function requestHandlerMiddleware(req, res, next) { | ||
// Any breadcrumbs or tags added will be isolated to the request | ||
return Sentry.runWithAsyncContext(() => { | ||
return next(req, res); | ||
}); | ||
} | ||
``` | ||
|
||
Under the hood, the SDK uses Node's [AsyncLocalStorage API](https://nodejs.org/api/async_context.html#class-asynclocalstorage) to perform the isolation. | ||
|
||
On lower SDK versions you'll have to use [domains](https://nodejs.org/api/domain.html) to isolate Sentry scope and breadcrumbs to a single request. | ||
|
||
```js | ||
const domain = require("domain"); | ||
|
||
function myRequestHandler(req, res, next) { | ||
const localDomain = domain.create(); | ||
return localDomain.bind(() => { | ||
return next(req, res); | ||
})(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1a7626f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sentry-docs – ./
docs.sentry.io
sentry-docs.sentry.dev
sentry-docs-git-master.sentry.dev