Skip to content

Commit d571a6c

Browse files
Remove bandit from default excluded domains and recommend PlugCapture only for Cowboy (#900)
Co-authored-by: Andrea Leopardi <an.leopardi@gmail.com>
1 parent cdcbc94 commit d571a6c

File tree

7 files changed

+37
-16
lines changed

7 files changed

+37
-16
lines changed

lib/sentry/logger_backend.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Sentry.LoggerBackend do
3333
## Configuration
3434
3535
* `:excluded_domains` - Any messages with a domain in the configured
36-
list will not be sent. Defaults to `[:cowboy, :bandit]` to avoid double reporting
36+
list will not be sent. Defaults to `[:cowboy]` to avoid double reporting
3737
events from `Sentry.PlugCapture`.
3838
3939
* `:metadata` - To include non-Sentry Logger metadata in reports, the
@@ -72,7 +72,7 @@ defmodule Sentry.LoggerBackend do
7272

7373
defstruct level: :error,
7474
metadata: [],
75-
excluded_domains: [:cowboy, :bandit],
75+
excluded_domains: [:cowboy],
7676
capture_log_messages: false
7777

7878
## Callbacks

lib/sentry/logger_handler.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule Sentry.LoggerHandler do
2626
],
2727
excluded_domains: [
2828
type: {:list, :atom},
29-
default: [:cowboy, :bandit],
29+
default: [:cowboy],
3030
type_doc: "list of `t:atom/0`",
3131
doc: """
3232
Any messages with a domain in the configured list will not be sent. The default is so as

lib/sentry/plug_capture.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
defmodule Sentry.PlugCapture do
22
@moduledoc """
3-
Provides basic functionality to capture and send errors occurring within
4-
Plug applications, including Phoenix.
3+
Ensures proper error reporting for Plug applications that use Cowboy.
54
65
It is intended for usage with `Sentry.PlugContext`, which adds relevant request
76
metadata to the Sentry context before errors are captured.
87
8+
> #### Only for Cowboy {: .info}
9+
>
10+
> `Sentry.PlugCapture` is only recommended for Cowboy applications.
11+
> For applications running on Bandit, which is the most recent default webserver
12+
> in Phoenix, `Sentry.PlugContext` should be enough, and using `Sentry.PlugCapture`
13+
> might result in duplicate errors.
14+
915
## Usage
1016
1117
### With Phoenix
@@ -69,7 +75,6 @@ defmodule Sentry.PlugCapture do
6975
* scrubs sensitive body params just like `Sentry.PlugContext.default_body_scrubber/1`
7076
7177
"""
72-
7378
defmacro __using__(opts) do
7479
quote do
7580
opts = unquote(opts)

lib/sentry/plug_context.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ defmodule Sentry.PlugContext do
1212
1313
plug Sentry.PlugContext
1414
15-
However, this module is generally intended to be used with `Sentry.PlugCapture`:
16-
this plug will add context metadata to the request, while `Sentry.PlugCapture` will
17-
capture raised exceptions and errors and report them to Sentry with the added metadata.
15+
This plug will add context metadata to the request, which will be added to
16+
reported errors that happen during plug execution. For Cowboy
17+
applications, you will also need to use `Sentry.PlugCapture`.
1818
1919
### Scrubbing `POST` Body Params
2020

pages/setup-with-plug-and-phoenix.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# Setup with Plug and Phoenix
22

3-
You can capture errors in Plug (and Phoenix) applications with `Sentry.PlugContext` and `Sentry.PlugCapture`. `Sentry.PlugContext` adds contextual metadata from the current request which is then included in errors that are captured and reported by `Sentry.PlugCapture`.
3+
You can enrich errors in Plug (and Phoenix) applications with `Sentry.PlugContext`. `Sentry.PlugContext` adds contextual metadata from the current request which is then included in errors.
44

55
## For Phoenix Applications
66

77
If you are using Phoenix:
88

9-
1. Add `Sentry.PlugCapture` above the `use Phoenix.Endpoint` line in your endpoint file
109
1. Add `Sentry.PlugContext` below `Plug.Parsers`
1110

1211
```diff
1312
defmodule MyAppWeb.Endpoint
14-
+ use Sentry.PlugCapture
1513
use Phoenix.Endpoint, otp_app: :my_app
1614

1715
# ...
@@ -24,6 +22,16 @@ If you are using Phoenix:
2422
+ plug Sentry.PlugContext
2523
```
2624

25+
1. If you're using Cowboy, also add `Sentry.PlugCapture` above the `use Phoenix.Endpoint` line in your endpoint file
26+
27+
```diff
28+
defmodule MyAppWeb.Endpoint
29+
+ use Sentry.PlugCapture
30+
use Phoenix.Endpoint, otp_app: :my_app
31+
32+
# ...
33+
```
34+
2735
If you're also using [Phoenix LiveView](https://github.com/phoenixframework/phoenix_live_view), consider also setting up your LiveViews to use the `Sentry.LiveViewHook` hook:
2836

2937
```elixir
@@ -72,13 +80,11 @@ end
7280

7381
If you are in a non-Phoenix Plug application:
7482

75-
1. Add `Sentry.PlugCapture` at the top of your Plug application
7683
1. Add `Sentry.PlugContext` below `Plug.Parsers` (if it is in your stack)
7784

7885
```diff
7986
defmodule MyApp.Router do
8087
use Plug.Router
81-
+ use Sentry.PlugCapture
8288

8389
# ...
8490

@@ -87,3 +93,13 @@ If you are in a non-Phoenix Plug application:
8793

8894
+ plug Sentry.PlugContext
8995
```
96+
97+
1. If you're using Cowboy, add `Sentry.PlugCapture` at the top of your Plug application
98+
99+
```diff
100+
defmodule MyApp.Router do
101+
use Plug.Router
102+
+ use Sentry.PlugCapture
103+
104+
# ...
105+
```

test/logger_backend_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ defmodule Sentry.LoggerBackendTest do
143143
Logger.configure_backend(Sentry.LoggerBackend, excluded_domains: [:cowboy, :bandit])
144144
end
145145

146-
test "sends two errors when a Plug process crashes if bandit domain is not excluded" do
146+
test "sends two errors when a Plug process crashes if PlugCapture is used and :bandit not excluded" do
147147
Logger.configure_backend(Sentry.LoggerBackend, excluded_domains: [])
148148

149149
ref = register_before_send()

test/sentry/logger_handler_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ defmodule Sentry.LoggerHandlerTest do
110110
end
111111

112112
@tag handler_config: %{excluded_domains: []}
113-
test "sends two errors when a Plug process crashes if bandit domain is not excluded",
113+
test "sends two errors when a Plug process crashes if PlugCapture is used and :bandit not excluded",
114114
%{sender_ref: ref} do
115115
start_supervised!({Sentry.ExamplePlugApplication, server: :bandit}, restart: :temporary)
116116

0 commit comments

Comments
 (0)