Skip to content

Commit

Permalink
enhance: don't init sentry if not configured
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Mar 5, 2025
1 parent 29b0bc1 commit 76ea19e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
12 changes: 7 additions & 5 deletions adminSiteClient/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
SENTRY_ADMIN_DSN,
} from "../settings/clientSettings.js"

Sentry.init({
dsn: SENTRY_ADMIN_DSN,
environment: ENV,
release: COMMIT_SHA,
})
if (SENTRY_ADMIN_DSN) {
Sentry.init({
dsn: SENTRY_ADMIN_DSN,
environment: ENV,
release: COMMIT_SHA,
})
}
3 changes: 2 additions & 1 deletion serverUtils/errorLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import * as Sentry from "@sentry/react"

export async function logErrorAndMaybeCaptureInSentry(err: any) {
console.error(err)
Sentry.captureException(err)

if (!process.env.VITEST) Sentry.captureException(err)
}
27 changes: 15 additions & 12 deletions serverUtils/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import * as Sentry from "@sentry/node"
import { nodeProfilingIntegration } from "@sentry/profiling-node"
import findBaseDir from "../settings/findBaseDir.js"

const baseDir = findBaseDir(__dirname)
if (baseDir === undefined) throw new Error("could not locate base package.json")
if (!process.env.VITEST) {
const baseDir = findBaseDir(__dirname)
if (baseDir === undefined)
throw new Error("could not locate base package.json")

dotenv.config({ path: `${baseDir}/.env` })
dotenv.config({ path: `${baseDir}/.env` })

// Ensure to call this before importing any other modules!
Sentry.init({
dsn: process.env.SENTRY_ADMIN_DSN,
integrations: [nodeProfilingIntegration()],
tracesSampleRate: 0.1,
profilesSampleRate: 1.0, // This is relative to tracesSampleRate
environment: process.env.ENV,
release: process.env.COMMIT_SHA,
})
// Ensure to call this before importing any other modules!
Sentry.init({
dsn: process.env.SENTRY_ADMIN_DSN,
integrations: [nodeProfilingIntegration()],
tracesSampleRate: 0.1,
profilesSampleRate: 1.0, // This is relative to tracesSampleRate
environment: process.env.ENV,
release: process.env.COMMIT_SHA,
})
}
46 changes: 24 additions & 22 deletions site/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ import { isInIFrame } from "@ourworldindata/utils"
import { COMMIT_SHA, ENV, SENTRY_DSN } from "../settings/clientSettings.js"
import { getPreferenceValue, PreferenceType } from "./cookiePreferences.js"

const analyticsConsent = getPreferenceValue(PreferenceType.Analytics)
if (SENTRY_DSN) {
const analyticsConsent = getPreferenceValue(PreferenceType.Analytics)

let sentryOpts: Sentry.BrowserOptions = {}
if (analyticsConsent && !isInIFrame()) {
// only collect session replays from: users that have consented to analytics
// AND where page isn't embedded in an iframe
sentryOpts = {
integrations: [
Sentry.replayIntegration({
maskAllText: false,
maskAllInputs: false,
blockAllMedia: false,
mask: [".sentry-mask"],
}),
],
replaysSessionSampleRate: ENV === "development" ? 1 : 0.1,
replaysOnErrorSampleRate: 0,
let sentryOpts: Sentry.BrowserOptions = {}
if (analyticsConsent && !isInIFrame()) {
// only collect session replays from: users that have consented to analytics
// AND where page isn't embedded in an iframe
sentryOpts = {
integrations: [
Sentry.replayIntegration({
maskAllText: false,
maskAllInputs: false,
blockAllMedia: false,
mask: [".sentry-mask"],
}),
],
replaysSessionSampleRate: ENV === "development" ? 1 : 0.1,
replaysOnErrorSampleRate: 0,
}
}
Sentry.init({
dsn: SENTRY_DSN,
environment: ENV,
release: COMMIT_SHA,
...sentryOpts,
})
}
Sentry.init({
dsn: SENTRY_DSN,
environment: ENV,
release: COMMIT_SHA,
...sentryOpts,
})

0 comments on commit 76ea19e

Please sign in to comment.