Skip to content

Commit 1fb6275

Browse files
committed
migrate sentry to workers
1 parent 0048861 commit 1fb6275

File tree

6 files changed

+104
-50
lines changed

6 files changed

+104
-50
lines changed

app/api/sentry-tunnel/route.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
3+
interface SentryResponse {
4+
error?: string;
5+
message?: string;
6+
}
7+
8+
const corsHeaders = {
9+
"Access-Control-Allow-Origin": "*",
10+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
11+
"Access-Control-Allow-Headers": "Content-Type",
12+
"Cache-Control": "public, max-age=300",
13+
};
14+
15+
export async function POST(req: NextRequest) {
16+
try {
17+
const sentryDsn =
18+
"https://8296abef723d97e7b8d5a15d1e93be1f@o4509816683823104.ingest.us.sentry.io/4509831375683584";
19+
20+
const dsn = new URL(sentryDsn);
21+
const projectId = dsn.pathname.split("/").pop();
22+
if (!projectId) {
23+
throw new Error("Invalid SENTRY_DSN: missing project ID");
24+
}
25+
26+
const sentryIngestUrl = `https://${dsn.host}/api/${projectId}/envelope/`;
27+
28+
const clientIp =
29+
req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "";
30+
31+
const newRequest = new Request(sentryIngestUrl, {
32+
method: "POST",
33+
headers: {
34+
"Content-Type": "application/x-sentry-envelope",
35+
"X-Forwarded-For": clientIp,
36+
},
37+
body: await req.arrayBuffer(),
38+
});
39+
40+
const response = await fetch(newRequest);
41+
console.error("Sentry proxy response status:", response.status);
42+
43+
return new Response(response.body, {
44+
status: response.status,
45+
headers: {
46+
...corsHeaders,
47+
"Content-Type": "application/x-sentry-envelope",
48+
},
49+
});
50+
} catch (error: unknown) {
51+
console.error("Sentry proxy failed:", error);
52+
const errorMessage =
53+
error instanceof Error ? error.message : "Unknown error";
54+
return NextResponse.json(
55+
{
56+
error: "Internal server error",
57+
message: `Failed to proxy Sentry request: ${errorMessage}`,
58+
} as SentryResponse,
59+
{
60+
status: 500,
61+
headers: corsHeaders,
62+
}
63+
);
64+
}
65+
}
66+
67+
export async function OPTIONS() {
68+
return new Response(null, {
69+
status: 204,
70+
headers: corsHeaders,
71+
});
72+
}

instrumentation.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@radix-ui/react-radio-group": "^1.3.7",
2424
"@radix-ui/react-select": "^2.2.5",
2525
"@radix-ui/react-slot": "^1.2.3",
26+
"@sentry/cloudflare": "^10.5.0",
2627
"@sentry/nextjs": "^10.4.0",
2728
"@tailwindcss/typography": "^0.5.16",
2829
"class-variance-authority": "^0.7.1",

sentry.edge.config.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

sentry.server.config.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)