Skip to content

Commit

Permalink
feat(sentry): setup and auth (#1747)
Browse files Browse the repository at this point in the history
* feat(sentry): setup and auth

* revert(): next-env file

* chore(sentry): add error to error-page

* chore: remove experimental hook

* chore(): order of packages

* chore(env): set dsn url in env variable and update readme
  • Loading branch information
emilielr authored Nov 26, 2024
1 parent 0f1bdb0 commit 1f4604d
Show file tree
Hide file tree
Showing 12 changed files with 1,381 additions and 67 deletions.
20 changes: 17 additions & 3 deletions tavla/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,28 @@ cd tavla/tavla
yarn install
```

## Running the application
## Environment Configuration

The project integrates with Sentry for error tracking and performance monitoring. However, Sentry is not required for running the application locally or in development mode. If you want to use Sentry, follow these steps:

1. Create a `.env.local` file in the root of the project (if it doesn’t already exist).
2. Add the following lines to your `.env.local` file:

```bash
SENTRY_DSN_URL=your-sentry-dsn-url
SENTRY_AUTH_TOKEN=your-sentry-auth-token
```

Replace `your-sentry-dsn-url` and `your-sentry-auth-token` with the DSN provided by your Sentry project.

## Running the Application

To start the application in development mode, use:

```bash
yarn dev
# or
yarn dev:persist #persists the local database
yarn dev:persist # persists the local database
```

This will start the development server at `http://localhost:3000`
This will start the development server at `http://localhost:3000`.
11 changes: 10 additions & 1 deletion tavla/app/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ import { Heading3 } from '@entur/typography'
import Image from 'next/image'
import Link from 'next/link'
import BeaverIllustration from 'assets/illustrations/Beaver.png'
import { useEffect } from 'react'
import * as Sentry from '@sentry/nextjs'

export default function Error() {
export default function Error({
error,
}: {
error: Error & { digest?: string }
}) {
useEffect(() => {
Sentry.captureException(error)
}, [error])
return (
<main className="container pb-10 flex flex-col items-center">
<Heading3>Au da! Noe gikk galt!</Heading3>
Expand Down
13 changes: 11 additions & 2 deletions tavla/app/global-error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ import { Button } from '@entur/button'
import { Heading3 } from '@entur/typography'
import Image from 'next/image'
import Link from 'next/link'

import BeaverIllustration from 'assets/illustrations/Beaver.png'
import * as Sentry from '@sentry/nextjs'
import { useEffect } from 'react'

export default function Error({
error,
}: {
error: Error & { digest?: string }
}) {
useEffect(() => {
Sentry.captureException(error)
}, [error])

export default function Error() {
return (
<html>
<body>
Expand Down
6 changes: 6 additions & 0 deletions tavla/helm/tavla/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ common:
name: slack-webhook
key: url

- name: SENTRY_AUTH_TOKEN
valueFrom:
secretKeyRef:
name: sentry
key: auth-token

probes:
enabled: true
spec:
Expand Down
13 changes: 13 additions & 0 deletions tavla/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Sentry from '@sentry/nextjs'

export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
await import('./sentry.server.config')
}

if (process.env.NEXT_RUNTIME === 'edge') {
await import('./sentry.edge.config')
}
}

export const onRequestError = Sentry.captureRequestError
17 changes: 17 additions & 0 deletions tavla/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { PHASE_DEVELOPMENT_SERVER } = require('next/constants')
const { withSentryConfig } = require("@sentry/nextjs");

/** @type {import('next').NextConfig} */
const nextConfig = {
Expand Down Expand Up @@ -36,3 +37,19 @@ module.exports = async (phase, { defaultConfig }) => {

return nextConfig
}

module.exports = withSentryConfig(nextConfig, {
// For all available options, see:
// https://github.com/getsentry/sentry-webpack-plugin#options

org: "entur",
project: "tavla",
authToken: process.env.SENTRY_AUTH_TOKEN,
silent: !process.env.CI,

// For all available options, see:
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
hideSourceMaps: true,
disableLogger: true,
}
);
1 change: 1 addition & 0 deletions tavla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@entur/tokens": "3.17.2",
"@entur/tooltip": "5.1.1",
"@entur/typography": "1.8.47",
"@sentry/nextjs": "^8",
"abortcontroller-polyfill": "1.7.6",
"classnames": "2.5.1",
"firebase": "11.0.1",
Expand Down
12 changes: 10 additions & 2 deletions tavla/pages/_error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import { Heading3 } from '@entur/typography'
import Image from 'next/image'
import Link from 'next/link'
import BeaverIllustration from 'assets/illustrations/Beaver.png'
import * as Sentry from '@sentry/nextjs'
import type { NextPageContext } from 'next'
import Error from 'next/error'

function Error() {
Error.getInitialProps = async (contextData: NextPageContext) => {
await Sentry.captureUnderscoreErrorException(contextData)
return Error.getInitialProps(contextData)
}

function CustomError() {
return (
<main className="container pb-10 flex flex-col items-center">
<Heading3>Au da! Noe gikk galt!</Heading3>
Expand All @@ -23,4 +31,4 @@ function Error() {
)
}

export default Error
export default CustomError
13 changes: 13 additions & 0 deletions tavla/sentry.client.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file configures the initialization of Sentry on the client.
// The config you add here will be used whenever a users loads a page in their browser.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from '@sentry/nextjs'

Sentry.init({
dsn: process.env.SENTRY_DSN_URL,

tracesSampleRate: 1,

enabled: process.env.NODE_ENV !== 'development',
})
14 changes: 14 additions & 0 deletions tavla/sentry.edge.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This file configures the initialization of Sentry for edge features (middleware, edge routes, and so on).
// The config you add here will be used whenever one of the edge features is loaded.
// Note that this config is unrelated to the Vercel Edge Runtime and is also required when running locally.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from '@sentry/nextjs'

Sentry.init({
dsn: process.env.SENTRY_DSN_URL,

tracesSampleRate: 1,

enabled: process.env.NODE_ENV !== 'development',
})
13 changes: 13 additions & 0 deletions tavla/sentry.server.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file configures the initialization of Sentry on the server.
// The config you add here will be used whenever the server handles a request.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from '@sentry/nextjs'

Sentry.init({
dsn: process.env.SENTRY_DSN_URL,

tracesSampleRate: 1,

enabled: process.env.NODE_ENV !== 'development',
})
Loading

0 comments on commit 1f4604d

Please sign in to comment.