Skip to content

Commit 90532ec

Browse files
authored
Merge pull request #1155 from CodeForAfrica/chore/trustlab_sentry
Add Sentry in `@/trustlab`
2 parents 970bf58 + f25cdd8 commit 90532ec

File tree

12 files changed

+263
-17
lines changed

12 files changed

+263
-17
lines changed

apps/codeforafrica/sentry.server.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import * as Sentry from "@sentry/nextjs";
66

7+
const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN;
8+
79
Sentry.init({
8-
dsn: "https://3b2df1ac72fa35fa96ad25b969c551bf@o210651.ingest.us.sentry.io/4507469860831232",
10+
dsn: SENTRY_DSN,
911

1012
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
1113
tracesSampleRate: 1,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This file configures the initialization of Sentry on the client.
2+
// The added config here will be used whenever a users loads a page in their browser.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from "@sentry/nextjs";
6+
7+
const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN;
8+
9+
Sentry.init({
10+
dsn: SENTRY_DSN,
11+
12+
// Add optional integrations for additional features
13+
integrations: [Sentry.replayIntegration()],
14+
15+
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
16+
tracesSampleRate: 1,
17+
18+
// Define how likely Replay events are sampled.
19+
// This sets the sample rate to be 10%. You may want this to be 100% while
20+
// in development and sample at a lower rate in production
21+
replaysSessionSampleRate: 0.1,
22+
23+
// Define how likely Replay events are sampled when an error occurs.
24+
replaysOnErrorSampleRate: 1.0,
25+
26+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
27+
debug: false,
28+
});
29+
30+
export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;
31+
32+
export default undefined;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Sentry from "@sentry/nextjs";
2+
3+
export async function register() {
4+
if (process.env.NEXT_RUNTIME === "nodejs") {
5+
await import("./sentry.server.config");
6+
}
7+
8+
if (process.env.NEXT_RUNTIME === "edge") {
9+
await import("./sentry.edge.config");
10+
}
11+
}
12+
13+
export const onRequestError = Sentry.captureRequestError;

apps/trustlab/next.config.mjs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { withSentryConfig } from "@sentry/nextjs";
12
import { withPayload } from "@payloadcms/next/withPayload";
23
import path from "path";
34

@@ -43,4 +44,38 @@ const nextConfig = {
4344
transpilePackages: ["@commons-ui/core", "@commons-ui/next"],
4445
};
4546

46-
export default withPayload(nextConfig);
47+
export default withPayload(
48+
withSentryConfig(nextConfig, {
49+
// For all available options, see:
50+
// https://www.npmjs.com/package/@sentry/webpack-plugin#options
51+
52+
authToken: process.env.SENTRY_AUTH_TOKEN,
53+
54+
org: process.env.SENTRY_ORG,
55+
project: process.env.SENTRY_PROJECT,
56+
57+
// Only print logs for uploading source maps in CI
58+
silent: !process.env.CI,
59+
60+
// For all available options, see:
61+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
62+
63+
// Upload a larger set of source maps for prettier stack traces (increases build time)
64+
widenClientFileUpload: true,
65+
66+
// Route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
67+
// This can increase your server load as well as your hosting bill.
68+
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
69+
// side errors will fail.
70+
tunnelRoute: "/monitoring",
71+
72+
// Automatically tree-shake Sentry logger statements to reduce bundle size
73+
disableLogger: true,
74+
75+
// Enables automatic instrumentation of Vercel Cron Monitors. (Does not yet work with App Router route handlers.)
76+
// See the following for more information:
77+
// https://docs.sentry.io/product/crons/
78+
// https://vercel.com/docs/cron-jobs
79+
automaticVercelMonitors: true,
80+
}),
81+
);

apps/trustlab/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
"@payloadcms/email-nodemailer": "catalog:payload-v3",
2424
"@payloadcms/next": "catalog:payload-v3",
2525
"@payloadcms/plugin-cloud-storage": "catalog:payload-v3",
26-
"@payloadcms/plugin-seo": "catalog:payload-v3",
2726
"@payloadcms/plugin-nested-docs": "catalog:payload-v3",
27+
"@payloadcms/plugin-sentry": "catalog:payload-v3",
28+
"@payloadcms/plugin-seo": "catalog:payload-v3",
2829
"@payloadcms/richtext-lexical": "catalog:payload-v3",
2930
"@payloadcms/storage-s3": "catalog:payload-v3",
3031
"@payloadcms/ui": "catalog:payload-v3",
32+
"@sentry/nextjs": "catalog:",
3133
"cross-env": "catalog:",
3234
"graphql": "catalog:payload-v3",
3335
"next": "catalog:",
@@ -42,8 +44,8 @@
4244
"@commons-ui/testing-library": "workspace:*",
4345
"@eslint/eslintrc": "catalog:",
4446
"eslint": "catalog:",
45-
"eslint-config-next": "catalog:",
4647
"eslint-config-commons-ui": "workspace:*",
48+
"eslint-config-next": "catalog:",
4749
"eslint-import-resolver-babel-module": "catalog:",
4850
"jest-config-commons-ui": "workspace:*",
4951
"playwright-config-commons-ui": "workspace:*"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file configures the initialization of Sentry for edge features (middleware, edge routes, and so on).
2+
// The config you add here will be used whenever one of the edge features is loaded.
3+
// Note that this config is unrelated to the Vercel Edge Runtime and is also required when running locally.
4+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
5+
6+
import * as Sentry from "@sentry/nextjs";
7+
8+
const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN;
9+
10+
Sentry.init({
11+
dsn: SENTRY_DSN,
12+
13+
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
14+
tracesSampleRate: 1,
15+
16+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
17+
debug: false,
18+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file configures the initialization of Sentry on the server.
2+
// The config you add here will be used whenever the server handles a request.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from "@sentry/nextjs";
6+
7+
const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN;
8+
9+
Sentry.init({
10+
dsn: SENTRY_DSN,
11+
12+
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
13+
tracesSampleRate: 1,
14+
15+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
16+
debug: false,
17+
});

apps/trustlab/src/app/(payload)/admin/importMap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { MetaDescriptionComponent as MetaDescriptionComponent_a8a977ebc872c5d5ea
2727
import { PreviewComponent as PreviewComponent_a8a977ebc872c5d5ea7ee689724c0860 } from "@payloadcms/plugin-seo/client";
2828
import { default as default_f5238f1b70ea6cf94e5a15e7dfec6dac } from "@/trustlab/payload/components/RowLabel";
2929
import { S3ClientUploadHandler as S3ClientUploadHandler_f97aa6c64367fa259c5bc0567239ef24 } from "@payloadcms/storage-s3/client";
30+
import { AdminErrorBoundary as AdminErrorBoundary_e5a9e14bdbe97e70ba60697217fe7688 } from "@payloadcms/plugin-sentry/client";
3031

3132
export const importMap = {
3233
"@payloadcms/richtext-lexical/rsc#RscEntryLexicalCell":
@@ -87,4 +88,6 @@ export const importMap = {
8788
default_f5238f1b70ea6cf94e5a15e7dfec6dac,
8889
"@payloadcms/storage-s3/client#S3ClientUploadHandler":
8990
S3ClientUploadHandler_f97aa6c64367fa259c5bc0567239ef24,
91+
"@payloadcms/plugin-sentry/client#AdminErrorBoundary":
92+
AdminErrorBoundary_e5a9e14bdbe97e70ba60697217fe7688,
9093
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as Sentry from "@sentry/nextjs";
2+
import Error from "next/error";
3+
4+
function CustomErrorComponent({ statusCode }) {
5+
return <Error statusCode={statusCode} />;
6+
}
7+
8+
CustomErrorComponent.getInitialProps = async (contextData) => {
9+
// In case this is running in a serverless function, await this in order to give Sentry
10+
// time to send the error before the lambda exits
11+
await Sentry.captureUnderscoreErrorException(contextData);
12+
13+
// This will contain the status code of the response
14+
return Error.getInitialProps(contextData);
15+
};
16+
17+
export default CustomErrorComponent;

apps/trustlab/src/payload/plugins/index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { nestedDocsPlugin } from "@payloadcms/plugin-nested-docs";
2+
import { sentryPlugin } from "@payloadcms/plugin-sentry";
23
import { seoPlugin } from "@payloadcms/plugin-seo";
34
import { s3Storage } from "@payloadcms/storage-s3";
5+
import * as Sentry from "@sentry/nextjs";
46

57
const plugins = [
8+
nestedDocsPlugin({
9+
collections: ["pages"],
10+
generateLabel: (_, doc) => doc.title,
11+
generateURL: (docs) => docs.reduce((url, doc) => `${url}/${doc.slug}`, ""),
12+
}),
613
s3Storage({
714
collections: {
815
media: true,
@@ -16,10 +23,18 @@ const plugins = [
1623
region: process.env.S3_REGION ?? "",
1724
},
1825
}),
19-
nestedDocsPlugin({
20-
collections: ["pages"],
21-
generateLabel: (_, doc) => doc.title,
22-
generateURL: (docs) => docs.reduce((url, doc) => `${url}/${doc.slug}`, ""),
26+
sentryPlugin({
27+
options: {
28+
context: ({ defaultContext, req }) => {
29+
return {
30+
...defaultContext,
31+
tags: {
32+
locale: req.locale,
33+
},
34+
};
35+
},
36+
},
37+
Sentry,
2338
}),
2439
seoPlugin({
2540
collections: ["pages"],

0 commit comments

Comments
 (0)