Skip to content

Commit

Permalink
filter healthcheck events and static assets for sentry transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Nov 8, 2023
1 parent ff60ae3 commit b4fe720
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 7 additions & 6 deletions app/routes/resources+/healthcheck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import { prisma } from '#app/utils/db.server.ts'

export async function loader({ request }: DataFunctionArgs) {
const host =
request.headers.get('X-Forwarded-Host') ?? request.headers.get('host')
request.headers.get('X-Forwarded-Host') ?? request.headers.get('host')

try {
// if we can connect to the database and make a simple query
// and make a HEAD request to ourselves, then we're good.
await Promise.all([
prisma.user.count(),
fetch(`${new URL(request.url).protocol}${host}`, {method: 'HEAD'}).then(
r => {
if (!r.ok) return Promise.reject(r)
},
),
fetch(`${new URL(request.url).protocol}${host}`, {
method: 'HEAD',
headers: { 'X-Healthcheck': 'true' },
}).then(r => {
if (!r.ok) return Promise.reject(r)
}),
])
return new Response('OK')
} catch (error: unknown) {
Expand Down
18 changes: 17 additions & 1 deletion app/utils/monitoring.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ export function init() {
Sentry.init({
dsn: ENV.SENTRY_DSN,
environment: ENV.MODE,
tracesSampleRate: 1,
tracesSampleRate: ENV.MODE === 'production' ? 1 : 0,
denyUrls: [
/\/resources\/healthcheck/,
// TODO: be smarter about the public assets...
/\/build\//,
/\/favicons\//,
/\/img\//,
/\/fonts\//,
/\/favicon.ico/,
/\/site\.webmanifest/,
],
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Sentry.Integrations.Prisma({ client: prisma }),
],
beforeSendTransaction(event) {
// ignore all healthcheck related transactions
if (event.request?.headers?.['X-Healthcheck'] === 'true') return null

return event
},
})
}

0 comments on commit b4fe720

Please sign in to comment.