-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Labels
Description
SDK
JavaScript SDK
Description
Add documentation explaining how to properly instrument deferred work scheduled with Next.js's after() function to maintain trace context.
Background
When using after() from next/server, spans created inside the deferred callback become detached root transactions instead of children of the request span. This happens because:
- The SDK flushes the request transaction when the response completes
- Deferred work in
after()continues running afterward - Spans created during deferred work have no in-memory parent to attach to
This results in dozens of small "db" or other transactions instead of one cohesive trace. See getsentry/sentry-javascript#19529 for the original issue and investigation.
Suggested Solution
Users should wrap their deferred work with startSpan inside the after() callback:
// ✓ Correct
after(
Sentry.startSpan({ name: "background.task", op: "task" }, async () => {
await doWork();
})
);
// ✗ Wrong - task starts before after() runs
const task = doWork();
after(task);Reactions are currently unavailable