#Nestjs Breadcrumb
5 messages · Page 1 of 1 (latest)
Hi @dusky blade. I'm happy to dig into this for you! I'll check my internal sources for answers to your question! :wave:
To use Breadcrumbs with NestJS and ensure they reset for each request, you simply use Sentry’s addBreadcrumb API in your code. The Sentry SDK for NestJS automatically isolates the scope and breadcrumbs for each request, so any breadcrumbs you add will only be attached to the current request and will not leak into others.
Here’s how you can manually add a breadcrumb in NestJS:
const Sentry = require("@sentry/node");
Sentry.addBreadcrumb({
category: "auth",
message: "Authenticated user " + user.email,
level: "info",
});
You can add breadcrumbs at any point in your request handling logic to record significant events. The available breadcrumb keys include type, category, message, level, timestamp, and data. Using other keys will result in the data being dropped by Sentry.
Automatic Isolation:
By default, the Sentry SDK uses Node’s AsyncLocalStorage API to ensure that each request’s scope and breadcrumbs are isolated. This means that breadcrumbs are automatically reset for each request, and you don’t need to do anything extra to achieve this behavior. For example:
app.get("/my-route", function () {
Sentry.addBreadcrumb({
message: "This breadcrumb should only be attached to this request",
});
// do something
});
app.get("/my-route-2", function () {
Sentry.addBreadcrumb({
message: "This breadcrumb should only be attached to this request",
});
// do something
});
Each request will have its own breadcrumbs, and they will not be shared between requests. If you need to manually isolate breadcrumbs for background jobs or other async tasks, you can use Sentry.withIsolationScope.
Summary:
- Use Sentry.addBreadcrumb to add breadcrumbs.
- Breadcrumbs are automatically reset for each request in NestJS.
- No extra configuration is needed for request isolation in typical usage.
For more details, see the official documentation: NestJS Breadcrumbs, Async Context in NestJS.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@dusky blade Our robots are still learning and improving, please let us know if this response was correct by reacting below.