-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error reporting in Cloudflare functions with Sentry (#3068)
Add Sentry logging for /donation/* Cloudflare Pages functions routes. At the time of writing, neither [bugsnag](bugsnag/bugsnag-js#637) nor [slack](slackapi/node-slack-sdk#1335) have official Cloudflare worker integrations. Given the criticality of these routes, I favoured reliability and introduced a new logging app which has official Cloudflare support: https://developers.cloudflare.com/pages/functions/plugins/sentry/ We should switch to bugsnag whenever official support is available. Testing: - (ENV and SENTRY_DSN secrets are already defined for the preview environment in Cloudflare's "owid" project) - temporarily disable the [Cloudflare Access rule](https://one.dash.cloudflare.com/078fcdfed9955087315dd86792e71a7e/access/apps/edit/d8c658c3-fd20-477e-ac20-e7ed7fd656de?tab=overview) by setting its subdomain to `temp` - send an empty JSON to https://donate.owid.pages.dev/donation/donate -> an error is logged in sentry and an alert email is sent to [TBD]@ourworldindata.org (this will be configured when the use of Sentry is confirmed). Slack integration is only available on paid plans.
- Loading branch information
Showing
6 changed files
with
127 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import sentryPlugin from "@cloudflare/pages-plugin-sentry" | ||
import { CaptureConsole } from "@sentry/integrations" | ||
|
||
interface SentryEnvVars { | ||
SENTRY_DSN: string | ||
ENV: "production" | "development" | ||
} | ||
|
||
const hasSentryEnvVars = (env: any): env is SentryEnvVars => { | ||
return ( | ||
!!env.SENTRY_DSN && | ||
!!env.ENV && | ||
["production", "development"].includes(env.ENV) | ||
) | ||
} | ||
|
||
export const onRequest: PagesFunction = (context) => { | ||
if (!hasSentryEnvVars(context.env)) { | ||
console.error( | ||
"Missing Sentry environment variables. Continuing without error logging..." | ||
) | ||
// Gracefully continue if Sentry is not configured. | ||
return context.next() | ||
} | ||
|
||
return sentryPlugin({ | ||
dsn: context.env.SENTRY_DSN, | ||
integrations: [new CaptureConsole()], | ||
environment: context.env.ENV, | ||
})(context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters