Skip to content

Commit dc2c254

Browse files
committed
add configuration and api handler for tunneling sentry requests
1 parent a31fde2 commit dc2c254

File tree

3 files changed

+69
-28
lines changed

3 files changed

+69
-28
lines changed

app/api/sentry-monitoring/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use server';
2+
3+
export const postAction = async ({ req }: any) => {
4+
try {
5+
const envelopeBytes = await req.arrayBuffer();
6+
const envelope = new TextDecoder().decode(envelopeBytes);
7+
const piece = envelope.split('\n')[0];
8+
const header = JSON.parse(piece);
9+
const dsn = new URL(header['dsn']);
10+
const project_id = dsn.pathname?.replace('/', '');
11+
12+
if (dsn.hostname !== process.env.SENTRY_DSN_URL) {
13+
throw new Error(`Invalid sentry hostname: ${dsn.hostname}`);
14+
}
15+
16+
if (!project_id || ![process.env.SENTRY_PROJECT_ID]?.includes(project_id)) {
17+
throw new Error(`Invalid sentry project id: ${project_id}`);
18+
}
19+
20+
const upstream_sentry_url = `https://${process.env.SENTRY_DSN_URL}/api/${project_id}/envelope/`;
21+
await fetch(upstream_sentry_url, {
22+
method: 'POST',
23+
body: envelopeBytes,
24+
});
25+
26+
return new Response(JSON.stringify({}), { status: 200 });
27+
} catch (e) {
28+
console.error('error tunneling to sentry', e);
29+
return new Response(
30+
JSON.stringify({ error: 'error tunneling to sentry' }),
31+
{ status: 500 }
32+
);
33+
}
34+
};

next.config.mjs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {withSentryConfig} from '@sentry/nextjs';
21
/** @type {import('next').NextConfig} */
32
import { fileURLToPath } from 'node:url';
3+
import { withSentryConfig } from '@sentry/nextjs';
44
import createJiti from 'jiti';
55
import createNextIntlPlugin from 'next-intl/plugin';
66

@@ -12,37 +12,41 @@ const nextConfig = withNextIntl({
1212
transpilePackages: ['opub-ui'],
1313
});
1414

15-
export default withSentryConfig(nextConfig, {
16-
// For all available options, see:
17-
// https://github.com/getsentry/sentry-webpack-plugin#options
15+
export default withSentryConfig(
16+
nextConfig,
17+
{
18+
// For all available options, see:
19+
// https://github.com/getsentry/sentry-webpack-plugin#options
1820

19-
org: process.env.SENTRY_ORG_NAME,
20-
project: process.env.SENTRY_PROJECT_NAME,
21+
org: process.env.SENTRY_ORG_NAME,
22+
project: process.env.SENTRY_PROJECT_NAME,
2123

22-
// Only print logs for uploading source maps in CI
23-
silent: !process.env.CI,
24+
// Only print logs for uploading source maps in CI
25+
silent: !process.env.CI,
2426

25-
// For all available options, see:
26-
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
27+
// For all available options, see:
28+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
2729

28-
// Upload a larger set of source maps for prettier stack traces (increases build time)
29-
widenClientFileUpload: true,
30+
// Upload a larger set of source maps for prettier stack traces (increases build time)
31+
widenClientFileUpload: true,
3032

31-
// Uncomment to route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
32-
// This can increase your server load as well as your hosting bill.
33-
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
34-
// side errors will fail.
35-
// tunnelRoute: "/monitoring",
33+
// Uncomment to route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
34+
// This can increase your server load as well as your hosting bill.
35+
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
36+
// side errors will fail.
37+
tunnelRoute: '/api/sentry-monitoring',
3638

37-
// Hides source maps from generated client bundles
38-
hideSourceMaps: true,
39+
// Hides source maps from generated client bundles
40+
hideSourceMaps: true,
3941

40-
// Automatically tree-shake Sentry logger statements to reduce bundle size
41-
disableLogger: true,
42+
// Automatically tree-shake Sentry logger statements to reduce bundle size
43+
disableLogger: true,
4244

43-
// Enables automatic instrumentation of Vercel Cron Monitors. (Does not yet work with App Router route handlers.)
44-
// See the following for more information:
45-
// https://docs.sentry.io/product/crons/
46-
// https://vercel.com/docs/cron-jobs
47-
automaticVercelMonitors: true,
48-
}, process.env.SENTRY_FEATURE_ENABLED);
45+
// Enables automatic instrumentation of Vercel Cron Monitors. (Does not yet work with App Router route handlers.)
46+
// See the following for more information:
47+
// https://docs.sentry.io/product/crons/
48+
// https://vercel.com/docs/cron-jobs
49+
// automaticVercelMonitors: true,
50+
},
51+
process.env.SENTRY_FEATURE_ENABLED
52+
);

sentry.client.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
// The config you add here will be used whenever a users loads a page in their browser.
33
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
44

5-
import * as Sentry from "@sentry/nextjs";
5+
import * as Sentry from '@sentry/nextjs';
66

77
Sentry.init({
88
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN_URL,
99

1010
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
1111
tracesSampleRate: 1,
1212

13+
// The 'tunnel' option allows you to route browser requests to Sentry through a specified Next.js route. This can help circumvent ad-blockers that may prevent Sentry from capturing errors and performance data.
14+
tunnel: '/api/sentry-monitoring',
15+
1316
// Setting this option to true will print useful information to the console while you're setting up Sentry.
1417
debug: false,
1518
});

0 commit comments

Comments
 (0)