Skip to content

Commit

Permalink
Fix Sentry setup
Browse files Browse the repository at this point in the history
  • Loading branch information
N2D4 committed Nov 22, 2024
1 parent c6ed648 commit 7a9a11f
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 191 deletions.
46 changes: 0 additions & 46 deletions apps/backend/sentry.edge.config.ts

This file was deleted.

46 changes: 0 additions & 46 deletions apps/backend/sentry.server.config.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export const GET = createSmartRouteHandler({
bodyType: yupString().oneOf(["success"]).defined(),
}),
handler: async (req) => {
throw new StackAssertionError(`Server debug error thrown successfully! ${Math.random()}`);
throw new StackAssertionError(`Server debug error thrown successfully!`);
},
});
2 changes: 1 addition & 1 deletion apps/backend/src/app/health/error-handler-debug/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function Page() {
return <div>
This page is useful for testing error handling.<br />
Your observability platform should pick up on the errors thrown below.<br />
<button onClick={() => throwErr(`Client debug error thrown successfully! ${Math.random()}`)}>Throw client error</button>
<button onClick={() => throwErr(`Client debug error thrown successfully!`)}>Throw client error</button>
<button onClick={() => {
console.log("Endpoint request", fetch("/health/error-handler-debug/endpoint"));
}}>Throw server error</button>
Expand Down
49 changes: 47 additions & 2 deletions apps/backend/src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
import { PrismaInstrumentation } from "@prisma/instrumentation";
import "./polyfills";

import * as Sentry from "@sentry/nextjs";
import { getEnvVariable, getNodeEnvironment } from "@stackframe/stack-shared/dist/utils/env";
import { nicify } from "@stackframe/stack-shared/dist/utils/strings";
import { registerOTel } from '@vercel/otel';
import "./polyfills";

export function register() {
registerOTel({
serviceName: 'stack-backend',
instrumentations: [new PrismaInstrumentation()],
});

if (getEnvVariable("NEXT_RUNTIME") === "nodejs" || getEnvVariable("NEXT_RUNTIME") === "edge") {
Sentry.init({
dsn: getEnvVariable("NEXT_PUBLIC_SENTRY_DSN"),

ignoreErrors: [
// React throws these errors when used with some browser extensions (eg. Google Translate)
"NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.",
"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.",
],

// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1,

// Setting this option to true will print useful information to the console while you're setting up Sentry.
debug: true,

enabled: getNodeEnvironment() !== "development" && !getEnvVariable("CI"),

// Add exception metadata to the event
beforeSend(event, hint) {
const error = hint.originalException;
let nicified;
try {
nicified = nicify(error, { maxDepth: 8 });
} catch (e) {
nicified = `Error occurred during nicification: ${e}`;
}
if (error instanceof Error) {
event.extra = {
...event.extra,
cause: error.cause,
errorProps: {
...error,
},
nicifiedError: nicified,
};
}
return event;
},
});

}
}
47 changes: 0 additions & 47 deletions apps/dashboard/sentry.edge.config.ts

This file was deleted.

46 changes: 0 additions & 46 deletions apps/dashboard/sentry.server.config.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors"
export const dynamic = "force-dynamic";

export function GET() {
throw new StackAssertionError(`Server debug error thrown successfully! ${Math.random()}`);
throw new StackAssertionError(`Server debug error thrown successfully!`);
}
2 changes: 1 addition & 1 deletion apps/dashboard/src/app/health/error-handler-debug/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function Page() {
return <div>
This page is useful for testing error handling.<br />
Your observability platform should pick up on the errors thrown below.<br />
<Button onClick={() => throwErr(`Client debug error thrown successfully! ${Math.random()}`)}>Throw client error</Button>
<Button onClick={() => throwErr(`Client debug error thrown successfully!`)}>Throw client error</Button>
<Button onClick={async () => {
console.log("Endpoint response", await fetch("/health/error-handler-debug/endpoint"));
}}>Throw server error</Button>
Expand Down
49 changes: 49 additions & 0 deletions apps/dashboard/src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as Sentry from "@sentry/nextjs";
import { getEnvVariable, getNodeEnvironment } from "@stackframe/stack-shared/dist/utils/env";
import { nicify } from "@stackframe/stack-shared/dist/utils/strings";
import "./polyfills";

export function register() {
if (getEnvVariable("NEXT_RUNTIME") === "nodejs" || getEnvVariable("NEXT_RUNTIME") === "edge") {
Sentry.init({
dsn: getEnvVariable("NEXT_PUBLIC_SENTRY_DSN"),

ignoreErrors: [
// React throws these errors when used with some browser extensions (eg. Google Translate)
"NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.",
"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.",
],

// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1,

// Setting this option to true will print useful information to the console while you're setting up Sentry.
debug: false,

enabled: getNodeEnvironment() !== "development" && !getEnvVariable("CI"),

// Add exception metadata to the event
beforeSend(event, hint) {
const error = hint.originalException;
let nicified;
try {
nicified = nicify(error, { maxDepth: 8 });
} catch (e) {
nicified = `Error occurred during nicification: ${e}`;
}
if (error instanceof Error) {
event.extra = {
...event.extra,
cause: error.cause,
errorProps: {
...error,
},
nicifiedError: nicified,
};
}
return event;
},
});

}
}

0 comments on commit 7a9a11f

Please sign in to comment.