Skip to content

Commit a734d72

Browse files
Merge pull request #187 from CivicDataLab/159-fix-sentry-sending-blocking-network-call-which-is-failing
add configuration and api handler for tunneling sentry requests
2 parents f0b2503 + dc2c254 commit a734d72

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

@@ -24,37 +24,41 @@ const nextConfig = withNextIntl({
2424
},
2525
});
2626

27-
export default withSentryConfig(nextConfig, {
28-
// For all available options, see:
29-
// https://github.com/getsentry/sentry-webpack-plugin#options
27+
export default withSentryConfig(
28+
nextConfig,
29+
{
30+
// For all available options, see:
31+
// https://github.com/getsentry/sentry-webpack-plugin#options
3032

31-
org: process.env.SENTRY_ORG_NAME,
32-
project: process.env.SENTRY_PROJECT_NAME,
33+
org: process.env.SENTRY_ORG_NAME,
34+
project: process.env.SENTRY_PROJECT_NAME,
3335

34-
// Only print logs for uploading source maps in CI
35-
silent: !process.env.CI,
36+
// Only print logs for uploading source maps in CI
37+
silent: !process.env.CI,
3638

37-
// For all available options, see:
38-
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
39+
// For all available options, see:
40+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
3941

40-
// Upload a larger set of source maps for prettier stack traces (increases build time)
41-
widenClientFileUpload: true,
42+
// Upload a larger set of source maps for prettier stack traces (increases build time)
43+
widenClientFileUpload: true,
4244

43-
// Uncomment to route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
44-
// This can increase your server load as well as your hosting bill.
45-
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
46-
// side errors will fail.
47-
// tunnelRoute: "/monitoring",
45+
// Uncomment to route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
46+
// This can increase your server load as well as your hosting bill.
47+
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
48+
// side errors will fail.
49+
tunnelRoute: '/api/sentry-monitoring',
4850

49-
// Hides source maps from generated client bundles
50-
hideSourceMaps: true,
51+
// Hides source maps from generated client bundles
52+
hideSourceMaps: true,
5153

52-
// Automatically tree-shake Sentry logger statements to reduce bundle size
53-
disableLogger: true,
54+
// Automatically tree-shake Sentry logger statements to reduce bundle size
55+
disableLogger: true,
5456

55-
// Enables automatic instrumentation of Vercel Cron Monitors. (Does not yet work with App Router route handlers.)
56-
// See the following for more information:
57-
// https://docs.sentry.io/product/crons/
58-
// https://vercel.com/docs/cron-jobs
59-
automaticVercelMonitors: true,
60-
}, process.env.SENTRY_FEATURE_ENABLED);
57+
// Enables automatic instrumentation of Vercel Cron Monitors. (Does not yet work with App Router route handlers.)
58+
// See the following for more information:
59+
// https://docs.sentry.io/product/crons/
60+
// https://vercel.com/docs/cron-jobs
61+
// automaticVercelMonitors: true,
62+
},
63+
process.env.SENTRY_FEATURE_ENABLED
64+
);

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)