From c4b2facee4b7c2235fd132b8513c0c9f97a947ac Mon Sep 17 00:00:00 2001 From: Mat Trudel Date: Mon, 24 Jun 2024 05:24:11 -0400 Subject: [PATCH] Add :bandit to list of ignored logger domains (#739) --- lib/sentry/logger_backend.ex | 7 +++++-- lib/sentry/logger_handler.ex | 2 +- mix.exs | 2 ++ mix.lock | 3 +++ test/event_test.exs | 2 +- test/logger_backend_test.exs | 15 ++++++++++++++- test/sentry/logger_handler_test.exs | 10 ++++++++++ test/support/example_plug_application.ex | 15 ++++++++++----- 8 files changed, 46 insertions(+), 10 deletions(-) diff --git a/lib/sentry/logger_backend.ex b/lib/sentry/logger_backend.ex index 43e30840..8b3a20d8 100644 --- a/lib/sentry/logger_backend.ex +++ b/lib/sentry/logger_backend.ex @@ -33,7 +33,7 @@ defmodule Sentry.LoggerBackend do ## Configuration * `:excluded_domains` - Any messages with a domain in the configured - list will not be sent. Defaults to `[:cowboy]` to avoid double reporting + list will not be sent. Defaults to `[:cowboy, :bandit]` to avoid double reporting events from `Sentry.PlugCapture`. * `:metadata` - To include non-Sentry Logger metadata in reports, the @@ -70,7 +70,10 @@ defmodule Sentry.LoggerBackend do ## State - defstruct level: :error, metadata: [], excluded_domains: [:cowboy], capture_log_messages: false + defstruct level: :error, + metadata: [], + excluded_domains: [:cowboy, :bandit], + capture_log_messages: false ## Callbacks diff --git a/lib/sentry/logger_handler.ex b/lib/sentry/logger_handler.ex index 6e28d715..90132972 100644 --- a/lib/sentry/logger_handler.ex +++ b/lib/sentry/logger_handler.ex @@ -26,7 +26,7 @@ defmodule Sentry.LoggerHandler do ], excluded_domains: [ type: {:list, :atom}, - default: [:cowboy], + default: [:cowboy, :bandit], type_doc: "list of `t:atom/0`", doc: """ Any messages with a domain in the configured list will not be sent. The default is so as diff --git a/mix.exs b/mix.exs index 78e75d74..a90cba3f 100644 --- a/mix.exs +++ b/mix.exs @@ -97,6 +97,8 @@ defmodule Sentry.Mixfile do {:telemetry, "~> 0.4 or ~> 1.0", optional: true}, # Dev and test dependencies + {:plug_cowboy, "~> 2.7", only: [:test]}, + {:bandit, "~> 1.0", only: [:test]}, {:bypass, "~> 2.0", only: [:test]}, {:dialyxir, "~> 1.0", only: [:test, :dev], runtime: false}, {:ex_doc, "~> 0.29.0", only: :dev}, diff --git a/mix.lock b/mix.lock index 6544c905..7df03ecd 100644 --- a/mix.lock +++ b/mix.lock @@ -1,4 +1,5 @@ %{ + "bandit": {:hex, :bandit, "1.5.5", "df28f1c41f745401fe9e85a6882033f5f3442ab6d30c8a2948554062a4ab56e0", [:mix], [{:hpax, "~> 0.2.0", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "f21579a29ea4bc08440343b2b5f16f7cddf2fea5725d31b72cf973ec729079e1"}, "bypass": {:hex, :bypass, "2.1.0", "909782781bf8e20ee86a9cabde36b259d44af8b9f38756173e8f5e2e1fabb9b1", [:mix], [{:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "d9b5df8fa5b7a6efa08384e9bbecfe4ce61c77d28a4282f79e02f1ef78d96b80"}, "castore": {:hex, :castore, "1.0.7", "b651241514e5f6956028147fe6637f7ac13802537e895a724f90bf3e36ddd1dd", [:mix], [], "hexpm", "da7785a4b0d2a021cd1292a60875a784b6caef71e76bf4917bdee1f390455cf5"}, "certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"}, @@ -18,6 +19,7 @@ "floki": {:hex, :floki, "0.36.1", "712b7f2ba19a4d5a47dfe3e74d81876c95bbcbee44fe551f0af3d2a388abb3da", [:mix], [], "hexpm", "21ba57abb8204bcc70c439b423fc0dd9f0286de67dc82773a14b0200ada0995f"}, "gen_stage": {:hex, :gen_stage, "1.2.1", "19d8b5e9a5996d813b8245338a28246307fd8b9c99d1237de199d21efc4c76a1", [:mix], [], "hexpm", "83e8be657fa05b992ffa6ac1e3af6d57aa50aace8f691fcf696ff02f8335b001"}, "hackney": {:hex, :hackney, "1.17.4", "99da4674592504d3fb0cfef0db84c3ba02b4508bae2dff8c0108baa0d6e0977c", [:rebar3], [{:certifi, "~>2.6.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "de16ff4996556c8548d512f4dbe22dd58a587bf3332e7fd362430a7ef3986b16"}, + "hpax": {:hex, :hpax, "0.2.0", "5a58219adcb75977b2edce5eb22051de9362f08236220c9e859a47111c194ff5", [:mix], [], "hexpm", "bea06558cdae85bed075e6c036993d43cd54d447f76d8190a8db0dc5893fa2f1"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, "makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"}, @@ -44,6 +46,7 @@ "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, "telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"}, "telemetry_registry": {:hex, :telemetry_registry, "0.2.1", "fe648a691f2128e4279d993cd010994c67f282354dc061e697bf070d4b87b480", [:mix, :rebar3], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4221cefbcadd0b3e7076960339223742d973f1371bc20f3826af640257bc3690"}, + "thousand_island": {:hex, :thousand_island, "1.3.5", "6022b6338f1635b3d32406ff98d68b843ba73b3aa95cfc27154223244f3a6ca5", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2be6954916fdfe4756af3239fb6b6d75d0b8063b5df03ba76fd8a4c87849e180"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, "websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"}, "websock_adapter": {:hex, :websock_adapter, "0.5.6", "0437fe56e093fd4ac422de33bf8fc89f7bc1416a3f2d732d8b2c8fd54792fe60", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "e04378d26b0af627817ae84c92083b7e97aca3121196679b73c73b99d0d133ea"}, diff --git a/test/event_test.exs b/test/event_test.exs index bc75a8ad..63c95da7 100644 --- a/test/event_test.exs +++ b/test/event_test.exs @@ -420,7 +420,7 @@ defmodule Sentry.EventTest do exception = RuntimeError.exception("error") event = Sentry.Event.transform_exception(exception, []) - assert ["asn1", "bypass" | _rest] = + assert ["asn1", "bandit", "bypass" | _rest] = event.modules |> Map.keys() |> Enum.sort() diff --git a/test/logger_backend_test.exs b/test/logger_backend_test.exs index e4f2c843..6a5df18e 100644 --- a/test/logger_backend_test.exs +++ b/test/logger_backend_test.exs @@ -139,7 +139,20 @@ defmodule Sentry.LoggerBackendTest do :hackney.get("http://127.0.0.1:8003/error_route", [], "", []) assert_receive {^ref, _event}, 1000 after - Logger.configure_backend(Sentry.LoggerBackend, excluded_domains: [:cowboy]) + Logger.configure_backend(Sentry.LoggerBackend, excluded_domains: [:cowboy, :bandit]) + end + + test "sends two errors when a Plug process crashes if bandit domain is not excluded" do + Logger.configure_backend(Sentry.LoggerBackend, excluded_domains: []) + + ref = register_before_send() + + start_supervised!({Sentry.ExamplePlugApplication, server: :bandit}, restart: :temporary) + + :hackney.get("http://127.0.0.1:8003/error_route", [], "", []) + assert_receive {^ref, _event}, 1000 + after + Logger.configure_backend(Sentry.LoggerBackend, excluded_domains: [:cowboy, :bandit]) end test "ignores log messages with excluded domains" do diff --git a/test/sentry/logger_handler_test.exs b/test/sentry/logger_handler_test.exs index fc56ddb0..296ecba2 100644 --- a/test/sentry/logger_handler_test.exs +++ b/test/sentry/logger_handler_test.exs @@ -97,6 +97,16 @@ defmodule Sentry.LoggerHandlerTest do assert_receive {^ref, _event}, 1000 end + + @tag handler_config: %{excluded_domains: []} + test "sends two errors when a Plug process crashes if bandit domain is not excluded", + %{sender_ref: ref} do + start_supervised!({Sentry.ExamplePlugApplication, server: :bandit}, restart: :temporary) + + :hackney.get("http://127.0.0.1:8003/error_route", [], "", []) + + assert_receive {^ref, _event}, 1000 + end end describe "with capture_log_messages: true" do diff --git a/test/support/example_plug_application.ex b/test/support/example_plug_application.ex index e20b9e18..ee6e88c5 100644 --- a/test/support/example_plug_application.ex +++ b/test/support/example_plug_application.ex @@ -11,11 +11,16 @@ defmodule Sentry.ExamplePlugApplication do plug :dispatch @spec child_spec(keyword()) :: Supervisor.child_spec() - def child_spec([]) do - Supervisor.child_spec( - {Plug.Cowboy, scheme: :http, plug: __MODULE__, options: [port: 8003]}, - [] - ) + def child_spec(opts \\ []) do + case Keyword.get(opts, :server, :cowboy) do + :cowboy -> + Supervisor.child_spec( + {Plug.Cowboy, scheme: :http, plug: __MODULE__, options: [port: 8003]}, + [] + ) + :bandit -> + Supervisor.child_spec({Bandit, plug: __MODULE__, port: 8003}, []) + end end get "/exit_route" do