From 28559a317fa3024d5803ffa489a618b4ecd015f2 Mon Sep 17 00:00:00 2001 From: Jamil Date: Tue, 18 Feb 2025 13:55:23 -0800 Subject: [PATCH] chore(portal): Optionally drop `NotFoundError` to sentry (#8183) By specifying the `before_send` hook, we can easily drop events based on their data, such as `original_exception` which contains the original exception instance raised. Leveraging this, we can add a `report_to_sentry` parameter to `Web.LiveErrors.NotFound` to optionally ignore certain not found errors from going to Sentry. --- elixir/apps/domain/lib/domain/telemetry/sentry.ex | 13 +++++++++++++ elixir/apps/web/lib/web/live/sign_in.ex | 2 +- elixir/apps/web/lib/web/live_errors.ex | 2 +- elixir/config/config.exs | 13 +++++++++++++ elixir/config/dev.exs | 3 +++ elixir/config/runtime.exs | 11 ++--------- elixir/config/test.exs | 3 +++ 7 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 elixir/apps/domain/lib/domain/telemetry/sentry.ex diff --git a/elixir/apps/domain/lib/domain/telemetry/sentry.ex b/elixir/apps/domain/lib/domain/telemetry/sentry.ex new file mode 100644 index 000000000..ca48388f2 --- /dev/null +++ b/elixir/apps/domain/lib/domain/telemetry/sentry.ex @@ -0,0 +1,13 @@ +defmodule Domain.Telemetry.Sentry do + def before_send(%{original_exception: %{report_to_sentry: report_to_sentry}} = event) do + if report_to_sentry do + event + else + nil + end + end + + def before_send(event) do + event + end +end diff --git a/elixir/apps/web/lib/web/live/sign_in.ex b/elixir/apps/web/lib/web/live/sign_in.ex index 508cd8b28..440ba0ab0 100644 --- a/elixir/apps/web/lib/web/live/sign_in.ex +++ b/elixir/apps/web/lib/web/live/sign_in.ex @@ -25,7 +25,7 @@ defmodule Web.SignIn do {:ok, socket} else _other -> - raise Web.LiveErrors.NotFoundError + raise Web.LiveErrors.NotFoundError, report_to_sentry: false end end diff --git a/elixir/apps/web/lib/web/live_errors.ex b/elixir/apps/web/lib/web/live_errors.ex index e9ca49872..0064d0186 100644 --- a/elixir/apps/web/lib/web/live_errors.ex +++ b/elixir/apps/web/lib/web/live_errors.ex @@ -1,6 +1,6 @@ defmodule Web.LiveErrors do defmodule NotFoundError do - defexception message: "Not Found" + defexception message: "Not Found", report_to_sentry: true defimpl Plug.Exception do def status(_exception), do: 404 diff --git a/elixir/config/config.exs b/elixir/config/config.exs index 468d20828..323c35fc1 100644 --- a/elixir/config/config.exs +++ b/elixir/config/config.exs @@ -278,6 +278,19 @@ config :workos, WorkOS.Client, client_id: "client_123456789", baseurl: "https://api.workos.com" +# Base Sentry config +config :sentry, + before_send: {Domain.Telemetry.Sentry, :before_send}, + # disable Sentry by default, enable in runtime.exs + dsn: nil, + environment_name: :unknown, + enable_source_code_context: true, + root_source_code_paths: [ + Path.join(File.cwd!(), "apps/domain"), + Path.join(File.cwd!(), "apps/web"), + Path.join(File.cwd!(), "apps/api") + ] + # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{Mix.env()}.exs" diff --git a/elixir/config/dev.exs b/elixir/config/dev.exs index 0b8499306..4c68147bd 100644 --- a/elixir/config/dev.exs +++ b/elixir/config/dev.exs @@ -115,3 +115,6 @@ config :workos, WorkOS.Client, api_key: System.get_env("WORKOS_API_KEY"), client_id: System.get_env("WORKOS_CLIENT_ID"), baseurl: System.get_env("WORKOS_BASE_URL", "https://api.workos.com") + +config :sentry, + environment_name: :dev diff --git a/elixir/config/runtime.exs b/elixir/config/runtime.exs index a224c7120..8f0da91e6 100644 --- a/elixir/config/runtime.exs +++ b/elixir/config/runtime.exs @@ -247,17 +247,10 @@ if config_env() == :prod do # Sentry - # Base Sentry config + # Enable Sentry by default in runtime prod env config :sentry, dsn: - "https://29f4ab7c6c473c17bc01f8aeffb0ac16@o4507971108339712.ingest.us.sentry.io/4508756715569152", - environment_name: :unknown, - enable_source_code_context: true, - root_source_code_paths: [ - Path.join(File.cwd!(), "apps/domain"), - Path.join(File.cwd!(), "apps/web"), - Path.join(File.cwd!(), "apps/api") - ] + "https://29f4ab7c6c473c17bc01f8aeffb0ac16@o4507971108339712.ingest.us.sentry.io/4508756715569152" # Environment-specific Sentry overrides if api_external_url = compile_config!(:api_external_url) do diff --git a/elixir/config/test.exs b/elixir/config/test.exs index 0b5c691a8..814034c2c 100644 --- a/elixir/config/test.exs +++ b/elixir/config/test.exs @@ -106,3 +106,6 @@ config :phoenix, :plug_init_mode, :runtime config :workos, WorkOS.Client, api_key: "sk_example_123456789", client_id: "client_123456789" + +config :sentry, + environment_name: :test