From 006ea4c6fd27b97e7fe67cec0b8d78486e77eda4 Mon Sep 17 00:00:00 2001 From: Jamil Date: Wed, 5 Feb 2025 15:36:06 -0800 Subject: [PATCH] fix(infra): Inline sentry logging config (#8031) It appears that something is initializing the Sentry.LoggerHandler before we try to load it when starting: ``` Invalid logger handler config: {:logger, {:invalid_handler, {:function_not_exported, {Sentry.LoggerHandler, :log, 2}}}} ``` This doesn't seem to actually inhibit the Sentry logger at all, presumably because it initializes just fine in the application start callback. Instead of defining the config in the `config/` directory, we can pass it directly to `:logger` on start which solves the above issue. --- elixir/apps/domain/lib/domain/application.ex | 9 +++- elixir/config/config.exs | 11 ----- elixir/config/runtime.exs | 48 ++++++++++---------- 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/elixir/apps/domain/lib/domain/application.ex b/elixir/apps/domain/lib/domain/application.ex index 47fcf608e..77631fcaf 100644 --- a/elixir/apps/domain/lib/domain/application.ex +++ b/elixir/apps/domain/lib/domain/application.ex @@ -8,7 +8,14 @@ defmodule Domain.Application do _ = OpentelemetryLoggerMetadata.setup() _ = OpentelemetryEcto.setup([:domain, :repo]) - Logger.add_handlers(:logger) + # Configure Sentry to capture Logger messages + :logger.add_handler(:sentry, Sentry.LoggerHandler, %{ + config: %{ + level: :warning, + metadata: :all, + capture_log_messages: true + } + }) # Can be uncommented when this bug is fixed: https://github.com/open-telemetry/opentelemetry-erlang-contrib/issues/327 # _ = OpentelemetryFinch.setup() diff --git a/elixir/config/config.exs b/elixir/config/config.exs index 92aa222f5..7b32f6201 100644 --- a/elixir/config/config.exs +++ b/elixir/config/config.exs @@ -232,17 +232,6 @@ config :logger, :default_formatter, format: "$time $metadata[$level] $message\n", metadata: :all -config :logger, :logger, [ - {:handler, :sentry, Sentry.LoggerHandler, - %{ - config: %{ - level: :warning, - metadata: :all, - capture_log_messages: true - } - }} -] - # Use Jason for JSON parsing in Phoenix config :phoenix, :json_library, Jason diff --git a/elixir/config/runtime.exs b/elixir/config/runtime.exs index 7817942fa..5d5ea4a91 100644 --- a/elixir/config/runtime.exs +++ b/elixir/config/runtime.exs @@ -236,34 +236,36 @@ if config_env() == :prod do # Sentry - api_external_url_host = URI.parse(compile_config!(:api_external_url)).host - - sentry_environment_name = - case api_external_url_host do - "api.firezone.dev" -> :production - "api.firez.one" -> :staging - _ -> :unknown - end - - sentry_dsn = - case api_external_url_host do - "api.firezone.dev" -> - "https://29f4ab7c6c473c17bc01f8aeffb0ac16@o4507971108339712.ingest.us.sentry.io/4508756715569152" - - "api.firez.one" -> - "https://29f4ab7c6c473c17bc01f8aeffb0ac16@o4507971108339712.ingest.us.sentry.io/4508756715569152" - - _ -> - nil - end - + # Base Sentry config config :sentry, - dsn: sentry_dsn, - environment_name: sentry_environment_name, + 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") ] + + # Environment-specific Sentry overrides + if api_external_url = compile_config!(:api_external_url) do + api_external_url_host = URI.parse(api_external_url).host + + # Set environment_name based on which API URL we're using + sentry_environment_name = + case api_external_url_host do + "api.firezone.dev" -> :production + "api.firez.one" -> :staging + _ -> :unknown + end + + config :sentry, :environment_name, sentry_environment_name + + # Disable Sentry for unknown environments + # Comment this out to enable Sentry in development and test environments + if sentry_environment_name == :unknown do + config :sentry, :dsn, nil + end + end end