Skip to content

Commit 24c320e

Browse files
authored
Rename :before_send_event to :before_send (and hard-deprecate) (#649)
1 parent 97302e8 commit 24c320e

File tree

10 files changed

+129
-35
lines changed

10 files changed

+129
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add `~r"/test/"` to the default source code exclude patterns (see the `:source_code_exclude_patterns` option).
88
- `:environment_name` now defaults to `production` (if it wasn't configured explicitly and if the `SENTRY_ENVIRONMENT` environment variable is not set).
99
- Hard-deprecate `:included_environments`. To control whether to send events to Sentry, use the `:dsn` configuration option instead. `:included_environments` now emits a warning if used, but will still work until v11.0.0 of this library.
10+
- Hard-deprecate `:before_send_event` in favor of the new `:before_send`. This brings this SDK in line with all other Sentry SDKs.
1011

1112
## 9.1.0
1213

lib/sentry.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ defmodule Sentry do
9999
## Filtering Exceptions
100100
101101
If you would like to prevent Sentry from sending certain exceptions, you can
102-
use the `:before_send_event` configuration option. See the [*Event Callbacks*
102+
use the `:before_send` configuration option. See the [*Event Callbacks*
103103
section](#module-event-callbacks) below.
104104
105105
Before v9.0.0, the recommended way to filter out exceptions was to use a *filter*,
@@ -108,14 +108,14 @@ defmodule Sentry do
108108
109109
## Event Callbacks
110110
111-
You can configure the `:before_send_event` and `:after_send_event` options to
112-
customize what happens before and/or after sending an event. The `:before_send_event`
111+
You can configure the `:before_send` and `:after_send_event` options to
112+
customize what happens before and/or after sending an event. The `:before_send`
113113
callback must be of type `t:before_send_event_callback/0` and the `:after_send_event`
114114
callback must be of type `t:after_send_event_callback/0`. For example, you
115115
can set:
116116
117117
config :sentry,
118-
before_send_event: {MyModule, :before_send},
118+
before_send: {MyModule, :before_send},
119119
after_send_event: {MyModule, :after_send}
120120
121121
`MyModule` could look like this:
@@ -176,7 +176,7 @@ defmodule Sentry do
176176
require Logger
177177

178178
@typedoc """
179-
A callback to use with the `:before_send_event` configuration option.
179+
A callback to use with the `:before_send` configuration option.
180180
configuration options.k
181181
182182
If this is `{module, function_name}`, then `module.function_name(event)` will
@@ -294,7 +294,7 @@ defmodule Sentry do
294294
* `:sample_rate` - same as the global `:sample_rate` configuration, but applied only to
295295
this call. See the module documentation. *Available since v10.0.0*.
296296
297-
* `:before_send_event` - same as the global `:before_send_event` configuration, but
297+
* `:before_send` - same as the global `:before_send` configuration, but
298298
applied only to this call. See the module documentation. *Available since v10.0.0*.
299299
300300
* `:after_send_event` - same as the global `:after_send_event` configuration, but

lib/sentry/client.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule Sentry.Client do
1919
def send_event(%Event{} = event, opts) when is_list(opts) do
2020
result_type = Keyword.get_lazy(opts, :result, &Config.send_result/0)
2121
sample_rate = Keyword.get_lazy(opts, :sample_rate, &Config.sample_rate/0)
22-
before_send_event = Keyword.get_lazy(opts, :before_send_event, &Config.before_send_event/0)
22+
before_send = Keyword.get_lazy(opts, :before_send, &Config.before_send/0)
2323
after_send_event = Keyword.get_lazy(opts, :after_send_event, &Config.after_send_event/0)
2424
client = Keyword.get_lazy(opts, :client, &Config.client/0)
2525

@@ -30,7 +30,7 @@ defmodule Sentry.Client do
3030
end)
3131

3232
result =
33-
with {:ok, %Event{} = event} <- maybe_call_before_send(event, before_send_event),
33+
with {:ok, %Event{} = event} <- maybe_call_before_send(event, before_send),
3434
:ok <- sample_event(sample_rate) do
3535
send_result = encode_and_send(event, result_type, client, request_retries)
3636
_ignored = maybe_call_after_send(event, send_result, after_send_event)
@@ -86,7 +86,7 @@ defmodule Sentry.Client do
8686

8787
defp call_before_send(_event, other) do
8888
raise ArgumentError, """
89-
:before_send_event must be an anonymous function or a {module, function} tuple, got: \
89+
:before_send must be an anonymous function or a {module, function} tuple, got: \
9090
#{inspect(other)}\
9191
"""
9292
end

lib/sentry/config.ex

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ defmodule Sentry.Config do
7171
doc: """
7272
The percentage of events to send to Sentry. A value of `0.0` will deny sending any events,
7373
and a value of `1.0` will send 100% of events. Sampling is applied
74-
**after** the `:before_send_event` callback. See where [the Sentry
74+
**after** the `:before_send` callback. See where [the Sentry
7575
documentation](https://develop.sentry.dev/sdk/sessions/#filter-order)
7676
suggests this. Must be between `0.0` and `1.0` (included).
7777
"""
@@ -238,7 +238,7 @@ defmodule Sentry.Config do
238238
]
239239

240240
hook_opts_schema = [
241-
before_send_event: [
241+
before_send: [
242242
type: {:or, [{:fun, 1}, {:tuple, [:atom, :atom]}]},
243243
type_doc: "`t:before_send_event_callback/0`",
244244
doc: """
@@ -247,6 +247,16 @@ defmodule Sentry.Config do
247247
If the callback returns `nil` or `false`, the event is not reported. If it returns an
248248
updated `Sentry.Event`, then the updated event is used instead. See the [*Event Callbacks*
249249
section](#module-event-callbacks) below for more information.
250+
251+
`:before_send` is available *since v10.0.0*. Before, it was called `:before_send_event`.
252+
"""
253+
],
254+
before_send_event: [
255+
type: {:or, [{:fun, 1}, {:tuple, [:atom, :atom]}]},
256+
type_doc: "`t:before_send_event_callback/0`",
257+
deprecated: "Use :before_send instead.",
258+
doc: """
259+
Exactly the same as `:before_send`, but has been **deprecated since v10.0.0**.
250260
"""
251261
],
252262
after_send_event: [
@@ -299,6 +309,7 @@ defmodule Sentry.Config do
299309
|> normalize_included_environments()
300310
|> normalize_environment()
301311
|> assert_dsn_has_no_query_params!()
312+
|> handle_deprecated_before_send()
302313

303314
{:error, error} ->
304315
raise ArgumentError, """
@@ -413,8 +424,8 @@ defmodule Sentry.Config do
413424
@spec hackney_opts() :: keyword()
414425
def hackney_opts, do: fetch!(:hackney_opts)
415426

416-
@spec before_send_event() :: (Sentry.Event.t() -> Sentry.Event.t()) | {module(), atom()} | nil
417-
def before_send_event, do: get(:before_send_event)
427+
@spec before_send() :: (Sentry.Event.t() -> Sentry.Event.t()) | {module(), atom()} | nil
428+
def before_send, do: get(:before_send)
418429

419430
@spec after_send_event() ::
420431
(Sentry.Event.t(), term() -> Sentry.Event.t()) | {module(), atom()} | nil
@@ -463,6 +474,29 @@ defmodule Sentry.Config do
463474
end)
464475
end
465476

477+
# TODO: remove me on v11.0.0, :included_environments has been deprecated
478+
# in v10.0.0.
479+
defp handle_deprecated_before_send(opts) do
480+
{before_send_event, opts} = Keyword.pop(opts, :before_send_event)
481+
482+
case Keyword.fetch(opts, :before_send) do
483+
{:ok, _before_send} when not is_nil(before_send_event) ->
484+
raise ArgumentError, """
485+
you cannot configure both :before_send and :before_send_event. :before_send_event
486+
is deprecated, so only use :before_send from now on.
487+
"""
488+
489+
{:ok, _before_send} ->
490+
opts
491+
492+
:error when not is_nil(before_send_event) ->
493+
Keyword.put(opts, :before_send, before_send_event)
494+
495+
:error ->
496+
opts
497+
end
498+
end
499+
466500
defp normalize_environment(config) do
467501
Keyword.update!(config, :environment_name, &to_string/1)
468502
end

lib/sentry/event_filter.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ defmodule Sentry.EventFilter do
77
> #### Soft-deprecated {: .warning}
88
>
99
> This behaviour is soft-deprecated in favor of filtering events through the
10-
> `:before_send_event` callback functionality. `:before_send_event` is described in
10+
> `:before_send` callback functionality. `:before_send` is described in
1111
> details in the documentation for the `Sentry` module. It's a more general
1212
> 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.
13+
> an example of how to replace an event filter with a `:before_send` callback.
1414
>
1515
> In future major versions of this library, we might hard-deprecate or remove this
1616
> behaviour altogether.
@@ -65,10 +65,10 @@ defmodule Sentry.EventFilter do
6565
end
6666
end
6767
68-
## Replacing With `:before_send_event`
68+
## Replacing With `:before_send`
6969
7070
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:
71+
the `:before_send` callback. We can start with a module:
7272
7373
defmodule MyApp.SentryEventFilter do
7474
def filter_non_500(%Sentry.Event{original_exception: exception} = event) do
@@ -86,16 +86,16 @@ defmodule Sentry.EventFilter do
8686
end
8787
end
8888
89-
Then, we can configure the `:before_send_event` callback.
89+
Then, we can configure the `:before_send` callback.
9090
9191
config :sentry,
92-
before_send_event: {MyApp.SentryEventFilter, :filter_non_500}
92+
before_send: {MyApp.SentryEventFilter, :filter_non_500}
9393
9494
> #### Multiple Callbacks {: .tip}
9595
>
96-
> You can only have one `:before_send_event` callback. If you change the value
96+
> You can only have one `:before_send` callback. If you change the value
9797
> 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
98+
> want to do multiple things in a `:before_send` callback, create a function
9999
> that does all the things you need and register *that* as the callback.
100100
"""
101101

pages/upgrade-10.x.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Now, packaging source code is an active step that you have to take. The [`mix se
2626

2727
Now, if you're not explicitly setting he `:environment_name` option in your config or setting the `SENTRY_ENVIRONMENT` environment variable, the environment will default to `production` (which is in line with the other Sentry SDKs).
2828

29+
## Rename `:before_send_event` to `:before_send`
30+
31+
To be in line with all other Sentry SDKs, we renamed the `:before_send_event` configuration option to `:before_send`. Just rename `:before_send_event` to `:before_send` in your configuration and potentially in any call where you pass it directly.
32+
2933
## Stop Using `:included_environments`
3034

3135
We hard-deprecated `:included_environments`. It's a bit of a confusing option that essentially no other Sentry SDKs use. To control whether to send events to Sentry, use the `:dsn` configuration instead (if set then we send events, if not set then we don't send events). `:included_environments` will be removed in v11.0.0.

test/logger_backend_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ defmodule Sentry.LoggerBackendTest do
339339
ref = make_ref()
340340

341341
TestEnvironmentHelper.modify_env(:sentry,
342-
before_send_event: fn event ->
342+
before_send: fn event ->
343343
send(pid, {ref, event})
344344
false
345345
end

test/sentry/client_test.exs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ defmodule Sentry.ClientTest do
8282
end
8383
end
8484

85-
test "calls anonymous :before_send_event callback", %{bypass: bypass} do
85+
test "calls anonymous :before_send callback", %{bypass: bypass} do
8686
Bypass.expect(bypass, fn conn ->
8787
assert {:ok, body, conn} = Plug.Conn.read_body(conn)
8888

@@ -96,7 +96,7 @@ defmodule Sentry.ClientTest do
9696

9797
modify_env(
9898
:sentry,
99-
before_send_event: fn event ->
99+
before_send: fn event ->
100100
metadata = Map.new(Logger.metadata())
101101
{user_id, rest_metadata} = Map.pop(metadata, :user_id)
102102

@@ -114,17 +114,55 @@ defmodule Sentry.ClientTest do
114114
assert {:ok, _} = Client.send_event(event, result: :sync)
115115
end
116116

117-
test "if :before_send_event callback returns falsey, the event is not sent" do
117+
# TODO: Remove in v11.0.0, :before_send_event has been deprecated in v10.0.0.
118+
test "still supports :before_send_event callback", %{bypass: bypass} do
119+
Bypass.expect(bypass, fn conn ->
120+
assert {:ok, body, conn} = Plug.Conn.read_body(conn)
121+
122+
event = TestHelpers.decode_event_from_envelope!(body)
123+
124+
assert event.extra == %{"key" => "value"}
125+
assert event.user["id"] == 1
126+
127+
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
128+
end)
129+
130+
stderr =
131+
ExUnit.CaptureIO.capture_io(:stderr, fn ->
132+
modify_env(
133+
:sentry,
134+
before_send_event: fn event ->
135+
metadata = Map.new(Logger.metadata())
136+
{user_id, rest_metadata} = Map.pop(metadata, :user_id)
137+
138+
%Event{
139+
event
140+
| extra: Map.merge(event.extra, rest_metadata),
141+
user: Map.put(event.user, :id, user_id)
142+
}
143+
end
144+
)
145+
146+
event = Event.create_event([])
147+
Logger.metadata(key: "value", user_id: 1)
148+
149+
assert {:ok, _} = Client.send_event(event, result: :sync)
150+
end)
151+
152+
assert stderr =~ ":before_send_event option is deprecated. Use :before_send instead."
153+
end
154+
155+
test "if :before_send callback returns falsey, the event is not sent" do
118156
defmodule CallbackModuleArithmeticError do
119-
def before_send_event(event) do
157+
def before_send(event) do
120158
case event.original_exception do
121159
%ArithmeticError{} -> false
122160
_ -> event
123161
end
124162
end
125163
end
126164

127-
modify_env(:sentry, before_send_event: {CallbackModuleArithmeticError, :before_send_event})
165+
modify_env(:sentry, before_send: {CallbackModuleArithmeticError, :before_send})
128166

129167
try do
130168
:rand.uniform() + "1"
@@ -138,13 +176,13 @@ defmodule Sentry.ClientTest do
138176
:code.purge(CallbackModuleArithmeticError)
139177
end
140178

141-
test "calls the :before_send_event callback before using the sample rate and sets the session" do
179+
test "calls the :before_send callback before using the sample rate and sets the session" do
142180
test_pid = self()
143181
ref = make_ref()
144182
event = Event.create_event(source: :plug)
145183

146184
modify_env(:sentry,
147-
before_send_event: fn event ->
185+
before_send: fn event ->
148186
send(test_pid, {ref, event})
149187
event
150188
end

test/sentry/config_test.exs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,35 @@ defmodule Sentry.ConfigTest do
120120
end
121121
end
122122

123-
test ":before_send_event" do
124-
assert Config.validate!(before_send_event: {MyMod, :my_fun})[:before_send_event] ==
123+
test ":before_send" do
124+
assert Config.validate!(before_send: {MyMod, :my_fun})[:before_send] ==
125125
{MyMod, :my_fun}
126126

127127
fun = & &1
128-
assert Config.validate!(before_send_event: fun)[:before_send_event] == fun
128+
assert Config.validate!(before_send: fun)[:before_send] == fun
129129

130-
assert_raise ArgumentError, ~r/invalid value for :before_send_event option/, fn ->
131-
Config.validate!(before_send_event: :not_a_function)
130+
assert_raise ArgumentError, ~r/invalid value for :before_send option/, fn ->
131+
Config.validate!(before_send: :not_a_function)
132132
end
133133
end
134134

135+
# TODO: Remove in v11.0.0, we deprecated it in v10.0.0.
136+
test ":before_send_event" do
137+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
138+
assert Config.validate!(before_send_event: {MyMod, :my_fun})[:before_send] ==
139+
{MyMod, :my_fun}
140+
end) =~ ":before_send_event option is deprecated. Use :before_send instead."
141+
142+
ExUnit.CaptureIO.capture_io(:stderr, fn ->
143+
assert_raise ArgumentError, ~r/you cannot configure both :before_send and/, fn ->
144+
assert Config.validate!(
145+
before_send_event: {MyMod, :my_fun},
146+
before_send: {MyMod, :my_fun}
147+
)
148+
end
149+
end)
150+
end
151+
135152
test ":after_send_event" do
136153
assert Config.validate!(after_send_event: {MyMod, :my_fun})[:after_send_event] ==
137154
{MyMod, :my_fun}

test/sentry/logger_handler_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ defmodule Sentry.LoggerHandlerTest do
328328
ref = make_ref()
329329

330330
TestEnvironmentHelper.modify_env(:sentry,
331-
before_send_event: fn event ->
331+
before_send: fn event ->
332332
send(pid, {ref, event})
333333
false
334334
end

0 commit comments

Comments
 (0)