Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error reporting in Cloudflare functions with Sentry #3068

Merged
merged 3 commits into from
Jan 11, 2024

Conversation

mlbrgl
Copy link
Member

@mlbrgl mlbrgl commented Jan 5, 2024

Add Sentry logging for /donation/* Cloudflare Pages functions routes.

At the time of writing, neither bugsnag nor slack 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:

-> 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.

@mlbrgl mlbrgl force-pushed the cloudflare-functions-sentry branch from 66f845d to f6d827e Compare January 5, 2024 10:43
@mlbrgl mlbrgl requested a review from danyx23 January 5, 2024 11:14
@mlbrgl mlbrgl marked this pull request as ready for review January 5, 2024 11:14
@mlbrgl mlbrgl force-pushed the cloudflare-functions-sentry branch from f6d827e to 5fd4123 Compare January 5, 2024 13:15
@mlbrgl mlbrgl changed the title 🥅 (donate) add sentry logging middleware Error reporting in Cloudflare functions with Sentry Jan 8, 2024
@danyx23
Copy link
Contributor

danyx23 commented Jan 9, 2024

Hm. I don't love adding another service to our stack just for this. It's a bit annoying that neither bugsnag nor slack have CF compatible client libraries yet, but I think a bare-bones slack integration really doesn't need the client api as they have a pretty well designed http api. E.g. so send a message to slack channel is as simple as

POST https://slack.com/api/chat.postMessage
Content-type: application/json
Authorization: Bearer xoxb-your-token
{
  "channel": "YOUR_CHANNEL_ID",
  "text": "Hello world :tada:"
}

I'm kind of leaning towards a preference for a barebones slack message - but I'm happy to discuss the pros and cons of it in the site meeting later today if you like!

@mlbrgl
Copy link
Member Author

mlbrgl commented Jan 9, 2024

sure, we can try this too if it is indeed very straightforward.

My only concern would be around reporting unhandled errors, but this is less of a concern here, since most of the functions bodies are wrapped in try {}.

I would be tempted to run them in parallel for a month or so, just to make sure we're not missing anything. If we end up with perfect symmetry, then yes, by all means, let's strip it down.

@mlbrgl mlbrgl force-pushed the deploy-cloudflare-previews branch from 6442ee7 to 7ed4c7e Compare January 10, 2024 13:52
@mlbrgl mlbrgl force-pushed the cloudflare-functions-sentry branch from 5fd4123 to 7df1fcd Compare January 10, 2024 13:52
@mlbrgl mlbrgl force-pushed the deploy-cloudflare-previews branch from 7ed4c7e to 2a45f8c Compare January 10, 2024 15:22
@mlbrgl mlbrgl force-pushed the cloudflare-functions-sentry branch from 7df1fcd to 5c3b993 Compare January 10, 2024 15:22
@mlbrgl
Copy link
Member Author

mlbrgl commented Jan 11, 2024

see follow up in #3086

@mlbrgl mlbrgl force-pushed the deploy-cloudflare-previews branch from 2a45f8c to aabb029 Compare January 11, 2024 15:01
@mlbrgl mlbrgl force-pushed the cloudflare-functions-sentry branch from 5c3b993 to 4a7c954 Compare January 11, 2024 15:01
Copy link
Contributor

@danyx23 danyx23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@mlbrgl mlbrgl force-pushed the deploy-cloudflare-previews branch from aabb029 to 1bdb023 Compare January 11, 2024 15:52
@mlbrgl mlbrgl force-pushed the cloudflare-functions-sentry branch from 4a7c954 to 2de67c3 Compare January 11, 2024 15:53
@mlbrgl
Copy link
Member Author

mlbrgl commented Jan 11, 2024

Merge activity

  • Jan 11, 12:29 PM: @mlbrgl started a stack merge that includes this pull request via Graphite.
  • Jan 11, 12:39 PM: Graphite rebased this pull request as part of a merge.
  • Jan 11, 12:40 PM: @mlbrgl merged this pull request with Graphite.

mlbrgl added a commit that referenced this pull request Jan 11, 2024
…tion 1) (#2998)

This is part of the migration of the donor flow from Netlify to Cloudflare, tracked in #2947.

This PR deals specifically with the migration of the `donate` function, which handles the creation of a Stripe checkout session upon clicking on "Donate" on /donate. See [README.md](https://github.com/owid/owid-grapher/blob/donate-cloudflare/functions/README.md) for a complete sequence diagram.

Additionally, it tackles:
- upgrading the Stripe API version from 2020-08-27 to 2023-10-16
- switching to [inline pricing](https://stripe.com/docs/products-prices/pricing-models#inline-pricing) for code-driven pricing (vs configuration in the Stripe dashboard)
- fixes reCAPTCHA validation
- a more isomorphic take on errors reported to the donor. Donation payloads are statically typed on the client and the server, and checked for proper type at runtime when received over the wire - all using a single type (thanks to Typebox). There is however no particular reporting of more general errors at this stage, beyond what might be surfaced in the Cloudflare panel. This needs to be addressed.

This PR also brings some reader-facing improvements:
- #2916
- a more customized checkout page (see [notion](https://www.notion.so/owid/2023-12-06-Donor-flow-repay-tech-debt-v2-option-1-2ef260e93f9a42f9a4aa2ad418831c60))
  - [x] custom text next to the submit button
  - [x] add a product image
  - removal of confusing “£0.01 mention” for monthly donations
  - “Donate £50” instead of “Pay” for one-time donations

Non-code actions:
- [x] add env variables to [Cloudflare dashboard](https://dash.cloudflare.com/078fcdfed9955087315dd86792e71a7e/pages/view/owid/settings/environment-variables) (production and preview environments)
- [x] (when deploying live) remove STRIPE_PUBLIC_KEY from .env (it is not used anymore)
- [x] (when deploying live) update DONATE_API_URL env var (`/donation/donate`)
- [ ] (when deploying live) update API version on stripe dashboard
- [ ] (when deploying live) merge owid/owid-donors#1

Still unclear:
- [x] reporting errors happening in functions. EDIT: see #3068 
- [x] testing on staging server ~~(where to set env vars? Configure preview environment on Cloudflare dashboard?)~~
  - http://staging-site-donate-cloudflare/donate doesn't get past the form. I suspect this is because Stripe requires https when not running on localhost. I might either revive hans or install ngrok on staging-site-donate-cloudflare. EDIT: testing functions is better dealt with Cloudflare previews (see #3055)

Reference:
- see #2947 
- see [notion](https://www.notion.so/owid/2023-12-06-Donor-flow-repay-tech-debt-v2-option-1-2ef260e93f9a42f9a4aa2ad418831c60)
@mlbrgl mlbrgl force-pushed the deploy-cloudflare-previews branch from 1bdb023 to a0a3c63 Compare January 11, 2024 17:36
Base automatically changed from deploy-cloudflare-previews to master January 11, 2024 17:38
@mlbrgl mlbrgl force-pushed the cloudflare-functions-sentry branch from 2de67c3 to 7b34197 Compare January 11, 2024 17:39
@mlbrgl mlbrgl merged commit d4723ca into master Jan 11, 2024
12 of 14 checks passed
@mlbrgl mlbrgl deleted the cloudflare-functions-sentry branch January 11, 2024 17:40
mlbrgl added a commit that referenced this pull request Jan 11, 2024
Follow up to #3068.

We decided to replace Sentry with barebones Slack error logging in donation/* Cloudflare pages functions.

We'll keep Sentry around for about a month until we have confidence in our Slack setup.

For simplicity's sake, this PR also now reports the misconfiguration of env variables to the client (this particular error was previously hidden). This isn't an error that is likely to show up once the initial configuration is done, so it'll make our life easier at no real cost (the cost here being the possibility of sending an unhelpful "env variable missing" message to the client)

- donate.owid.pages.dev updated with this branch.
- cloudflare live and preview env vars added on CF dashboard, resp. posting to developers-errors-server and bot-testing

For testing, POST an empty JSON to https://donate.owid.pages.dev/donation/thank-you or https://donate.owid.pages.dev/donation/donate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants