Sentry request contexts

This commit is contained in:
Konstantin Wohlwend 2024-12-11 18:21:24 -08:00
parent 49445378eb
commit 2f9a48cc3f
5 changed files with 24 additions and 4 deletions

View File

@ -14,6 +14,8 @@ Sentry.init({
"NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.",
],
normalizeDepth: 5,
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1,
@ -24,8 +26,6 @@ Sentry.init({
replaysOnErrorSampleRate: 1.0,
// This sets the sample rate to be 10%. You may want this to be 100% while
// in development and sample at a lower rate in production
replaysSessionSampleRate: 1.0,
// You can remove this option if you're not planning to use the Sentry Session Replay feature:

View File

@ -21,6 +21,8 @@ export function register() {
"NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.",
],
normalizeDepth: 5,
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1,

View File

@ -1,5 +1,6 @@
import "../polyfills";
import * as Sentry from "@sentry/nextjs";
import { EndpointDocumentation } from "@stackframe/stack-shared/dist/crud";
import { KnownError, KnownErrors } from "@stackframe/stack-shared/dist/known-errors";
import { yupMixed } from "@stackframe/stack-shared/dist/schema-fields";
@ -58,6 +59,16 @@ function catchError(error: unknown): StatusError {
export function handleApiRequest(handler: (req: NextRequest, options: any, requestId: string) => Promise<Response>): (req: NextRequest, options: any) => Promise<Response> {
return async (req: NextRequest, options: any) => {
const requestId = generateSecureRandomString(80);
// Set Sentry scope to include request details
Sentry.setContext("stack-request", {
requestId: requestId,
method: req.method,
url: req.url,
query: Object.fromEntries(req.nextUrl.searchParams),
headers: Object.fromEntries(req.headers),
});
let hasRequestFinished = false;
try {
// censor long query parameters because they might contain sensitive data
@ -208,6 +219,8 @@ export function createSmartRouteHandler<
const fullReq = reqsParsed[0][0][1];
const handler = reqsParsed[0][1];
Sentry.setContext("stack-parsed-smart-request", smartReq as any);
let smartRes = await handler.handler(smartReq as any, fullReq);
return await createResponse(nextRequest, requestId, smartRes, handler.response);
@ -216,6 +229,9 @@ export function createSmartRouteHandler<
return Object.assign(handleApiRequest(async (req, options, requestId) => {
const bodyBuffer = await req.arrayBuffer();
const smartRequest = await createSmartRequest(req, bodyBuffer, options);
Sentry.setContext("stack-full-smart-request", smartRequest);
return await invoke(req, requestId, smartRequest);
}), {
[getSmartRouteHandlerSymbol()]: true,

View File

@ -15,6 +15,8 @@ Sentry.init({
"NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.",
],
normalizeDepth: 5,
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1,
@ -25,8 +27,6 @@ Sentry.init({
replaysOnErrorSampleRate: 1.0,
// This sets the sample rate to be 10%. You may want this to be 100% while
// in development and sample at a lower rate in production
replaysSessionSampleRate: 1.0,
// You can remove this option if you're not planning to use the Sentry Session Replay feature:

View File

@ -14,6 +14,8 @@ export function register() {
"NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.",
],
normalizeDepth: 5,
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1,