Skip to content

Commit

Permalink
Merge pull request #462 from skip-mev/create-sentry-proxy-api
Browse files Browse the repository at this point in the history
[FRE-1410] Add sentry proxy API
  • Loading branch information
toddkao authored Jan 17, 2025
2 parents 3e3d704 + 4fdb99d commit e840b90
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/pages/api/sentryProxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NextApiRequest, NextApiResponse } from "next";

const SENTRY_HOST = "o4504768725909504.ingest.us.sentry.io";
const SENTRY_PROJECT_IDS = ["4508485201231872"];

export const route = "/api/sentry";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "POST") {
return res.status(405).json({ error: "Method not allowed" });
}

try {
const requestBody = req.body;
const [headerPiece] = requestBody.split("\n");
const header = JSON.parse(headerPiece);
const dsn = new URL(header["dsn"]);
const project_id = dsn.pathname?.replace("/", "");

if (dsn.hostname !== SENTRY_HOST) {
throw new Error(`Invalid sentry hostname: ${dsn.hostname}`);
}

if (!project_id || !SENTRY_PROJECT_IDS.includes(project_id)) {
throw new Error(`Invalid sentry project id: ${project_id}`);
}

const upstream_sentry_url = `https://${SENTRY_HOST}/api/${project_id}/envelope/`;
await fetch(upstream_sentry_url, {
method: "POST",
body: requestBody,
headers: {
"Content-Type": req.headers["content-type"] || "application/octet-stream",
},
});

res.status(200).json({});
} catch (e) {
console.error("Error tunneling to Sentry:", e);
res.status(500).json({ error: "Error tunneling to Sentry" });
}
}

0 comments on commit e840b90

Please sign in to comment.