Skip to content

Commit d95a77c

Browse files
authored
Soft-deprecate Sentry.EventFilter (#608)
1 parent efec72f commit d95a77c

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
- Make the `Sentry.HTTPClient.child_spec/0` callback optional
2222
- Add `:all` as a possible value of the `:metadata` configuration option for `Sentry.LoggerBackend`
2323

24+
### Deprecations
25+
26+
- Soft-deprecate `Sentry.EventFilter` in favour of `:before_send_event` callbacks.
27+
2428
## 8.1.0
2529

2630
### Various fixes & improvements

lib/sentry.ex

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ defmodule Sentry do
139139
to keep. Defaults to `100`. See `Sentry.Context.add_breadcrumb/1`.
140140
141141
* `:before_send_event` (`t:before_send_event_callback/0`) - allows performing operations
142-
on the event *before* it is sent. If the callback returns `nil` or `false`,
143-
the event is not reported. If it returns an updated `Sentry.Event`, then
144-
the updated event is used instead. See the [*Event Callbacks*
142+
on the event *before* it is sent as well as filtering out the event altogether.
143+
If the callback returns `nil` or `false`, the event is not reported. If it returns an
144+
updated `Sentry.Event`, then the updated event is used instead. See the [*Event Callbacks*
145145
section](#module-event-callbacks) below for more information.
146146
147147
* `:after_send_event` (`t:after_send_event_callback/0`) - callback that is called *after*
148-
attempting to send an event. The result of the HTTP call as well as the event will
148+
attempting to send an event. The result of the HTTP call as well as the event will
149149
be passed as arguments. The return value of the callback is not returned. See the
150150
[*Event Callbacks* section](#module-event-callbacks) below for more information.
151151
@@ -231,25 +231,12 @@ defmodule Sentry do
231231
## Filtering Exceptions
232232
233233
If you would like to prevent Sentry from sending certain exceptions, you can
234-
use the `:filter` configuration option. It must be configured to be a module
235-
that implements the `Sentry.EventFilter` behaviour.
236-
237-
A configuration like the one below prevents sending `Phoenix.Router.NoRouteError`
238-
exceptions coming from `Sentry.Plug`, but allows other exceptions to be sent.
239-
240-
defmodule MyApp.SentryEventFilter do
241-
@behaviour Sentry.EventFilter
242-
243-
@impl true
244-
def exclude_exception?(%Phoenix.Router.NoRouteError{}, :plug), do: true
245-
def exclude_exception?(_exception, _source), do: false
246-
end
247-
248-
# In config/config.exs
249-
config :sentry,
250-
filter: MyApp.SentryEventFilter,
251-
# other config...
234+
use the `:before_send_event` configuration option. See the [*Event Callbacks*
235+
section](#module-event-callbacks) below.
252236
237+
Before v9.0.0, the recommended way to filter out exceptions was to use a *filter*,
238+
that is, a module implementing the `Sentry.EventFilter` behaviour. This is still supported,
239+
but is not deprecated. See `Sentry.EventFilter` for more information.
253240
254241
## Event Callbacks
255242

lib/sentry/event_filter.ex

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ defmodule Sentry.EventFilter do
44
55
There's only one callback to implement, `c:exclude_exception?/2`.
66
7+
> #### Soft-deprecated {: .warning}
8+
>
9+
> This behaviour is soft-deprecated in favor of filtering events through the
10+
> `:before_send_event` callback functionality. `:before_send_event` is described in
11+
> details in the documentation for the `Sentry` module. It's a more general
12+
> mechanism to filter or modify events before sending them to Sentry. See below for
13+
> an example of how to replace an event filter with a `:before_send_event` callback.
14+
>
15+
> In future major versions of this library, we might hard-deprecate or remove this
16+
> behaviour altogether.
17+
718
## Usage
819
920
To use a custom event filter module, configure the `:filter` option
@@ -54,6 +65,38 @@ defmodule Sentry.EventFilter do
5465
end
5566
end
5667
68+
## Replacing With `:before_send_event`
69+
70+
Let's look at an example of how to filter non-500 exceptions in a Plug app through
71+
the `:before_send_event` callback. We can start with a module:
72+
73+
defmodule MyApp.SentryEventFilter do
74+
def filter_non_500(%Sentry.Event{ __original_exception__: exception} = event) do
75+
cond do
76+
if Plug.Exception.status(exception) < 500 ->
77+
false
78+
79+
# Fall back to the default event filter.
80+
Sentry.DefaultEventFilter.exclude_exception?(exception, event.__source__) ->
81+
false
82+
83+
true ->
84+
event
85+
end
86+
end
87+
end
88+
89+
Then, we can configure the `:before_send_event` callback.
90+
91+
config :sentry,
92+
before_send_event: {MyApp.SentryEventFilter, :filter_non_500}
93+
94+
> #### Multiple Callbacks {: .tip}
95+
>
96+
> You can only have one `:before_send_event` callback. If you change the value
97+
> of this configuration option, you'll *override* the previous callback. If you
98+
> want to do multiple things in a `:before_send_event` callback, create a function
99+
> that does all the things you need and register *that* as the callback.
57100
"""
58101

59102
@doc """

0 commit comments

Comments
 (0)