From 7a63d3027dec8c88a405251cc30cf6727b9eab43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ra=C4=8D=C3=A1k?= Date: Fri, 24 Jan 2025 14:51:36 +0100 Subject: [PATCH] Add Sentry to Cloudflare Functions --- functions/_common/env.ts | 1 + functions/_middleware.ts | 11 +++++++++++ functions/package.json | 1 + wrangler.toml | 6 +++++- yarn.lock | 28 ++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 functions/_middleware.ts diff --git a/functions/_common/env.ts b/functions/_common/env.ts index 778f57a2ff7..a594f1a48c1 100644 --- a/functions/_common/env.ts +++ b/functions/_common/env.ts @@ -9,6 +9,7 @@ export interface Env { CLOUDFLARE_IMAGES_API_KEY: string CLOUDFLARE_IMAGES_URL: string ENV: string + SENTRY_DSN: string } // We collect the possible extensions here so we can easily take them into account // when handling redirects diff --git a/functions/_middleware.ts b/functions/_middleware.ts new file mode 100644 index 00000000000..3128310729e --- /dev/null +++ b/functions/_middleware.ts @@ -0,0 +1,11 @@ +import * as Sentry from "@sentry/cloudflare" +import { Env } from "./_common/env.js" + +export const onRequest = [ + // Make sure Sentry is the first middleware. + Sentry.sentryPagesPlugin((context) => ({ + dsn: context.env.SENTRY_DSN, + environment: context.env.ENV, + tracesSampleRate: 0.01, + })), +] diff --git a/functions/package.json b/functions/package.json index 3f5a5881914..e6e11ff758f 100644 --- a/functions/package.json +++ b/functions/package.json @@ -5,6 +5,7 @@ "@ourworldindata/grapher": "workspace:^", "@ourworldindata/types": "workspace:^", "@ourworldindata/utils": "workspace:^", + "@sentry/cloudflare": "^8.51.0", "itty-router": "^5.0.17", "littlezipper": "^0.1.4", "stripe": "^14.20.0", diff --git a/wrangler.toml b/wrangler.toml index 696d1fa0587..760c904758b 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,6 +1,10 @@ # Generated by Wrangler on Mon Aug 05 2024 16:52:18 GMT+0000 (GMT) name = "owid" compatibility_date = "2024-11-11" +# Sentry client needs the `nodejs_als` (AsyncLocalStorage) API. +# https://docs.sentry.io/platforms/javascript/guides/cloudflare/#configure +compatibility_flags = ["nodejs_als"] +upload_source_maps = true # To get source maps in Sentry. pages_build_output_dir = "./localBake" # Vars that should be available in all envs, including local dev @@ -15,7 +19,7 @@ GRAPHER_CONFIG_R2_BUCKET_FALLBACK_PATH = "v1" [env.preview.vars] MAILGUN_DOMAIN = "mg.ourworldindata.org" SLACK_ERROR_CHANNEL_ID = "C016H0BNNB1" -ENV = "preview" +ENV = "staging" GRAPHER_CONFIG_R2_BUCKET_URL = "https://grapher-configs-staging.owid.io" GRAPHER_CONFIG_R2_BUCKET_FALLBACK_URL = "https://grapher-configs.owid.io" GRAPHER_CONFIG_R2_BUCKET_FALLBACK_PATH = "v1" diff --git a/yarn.lock b/yarn.lock index 7873bad0501..45b6ab67eb4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1427,6 +1427,13 @@ __metadata: languageName: node linkType: hard +"@cloudflare/workers-types@npm:^4.x": + version: 4.20250121.0 + resolution: "@cloudflare/workers-types@npm:4.20250121.0" + checksum: 10/4f7800e8f82b91f93745c2ad9f1223173dc7584d9efa1c28185bb6a64daf4ba2cd693aee2be94cb800441fd06b75f08be2ea5afbce3eb39b9b76a9794e8a24a2 + languageName: node + linkType: hard + "@colors/colors@npm:1.5.0": version: 1.5.0 resolution: "@colors/colors@npm:1.5.0" @@ -4515,6 +4522,19 @@ __metadata: languageName: node linkType: hard +"@sentry/cloudflare@npm:^8.51.0": + version: 8.51.0 + resolution: "@sentry/cloudflare@npm:8.51.0" + dependencies: + "@cloudflare/workers-types": "npm:^4.x" + "@sentry/core": "npm:8.51.0" + dependenciesMeta: + "@cloudflare/workers-types": + optional: true + checksum: 10/e3713886e07ebb65da6612fe288899e4473e760577953f338d404a9d4c1b15d30b65b5c3cb1b7209861cca8aee71850745b198f3dc71fac9899a51f2a64c640e + languageName: node + linkType: hard + "@sentry/core@npm:8.48.0": version: 8.48.0 resolution: "@sentry/core@npm:8.48.0" @@ -4522,6 +4542,13 @@ __metadata: languageName: node linkType: hard +"@sentry/core@npm:8.51.0": + version: 8.51.0 + resolution: "@sentry/core@npm:8.51.0" + checksum: 10/02d98706e9639f97f9044d49798d64565b9fe856c1df51f4e2171a6865b221e72116a594985156eb871ec9e70494073cf1c218fac63d62a33592acd14fd13ec9 + languageName: node + linkType: hard + "@sentry/node@npm:8.48.0, @sentry/node@npm:^8.48.0": version: 8.48.0 resolution: "@sentry/node@npm:8.48.0" @@ -16170,6 +16197,7 @@ __metadata: "@ourworldindata/grapher": "workspace:^" "@ourworldindata/types": "workspace:^" "@ourworldindata/utils": "workspace:^" + "@sentry/cloudflare": "npm:^8.51.0" itty-router: "npm:^5.0.17" littlezipper: "npm:^0.1.4" stripe: "npm:^14.20.0"