Skip to content

Commit 726ba0f

Browse files
committed
use before_send_event to filter
1 parent dfcbf23 commit 726ba0f

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

lib/sentry.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ defmodule Sentry do
117117
@spec capture_exception(Exception.t() | atom() | {atom(), atom()}, Keyword.t()) :: send_result
118118
def capture_exception(exception, opts \\ []) do
119119
filter_module = Config.filter()
120-
{source, opts} = Keyword.pop(opts, :event_source)
120+
event_source = Keyword.get(opts, :event_source)
121121

122-
if filter_module.exclude_exception?(exception, source) do
122+
if filter_module.exclude_exception?(exception, event_source) do
123123
:excluded
124124
else
125125
exception

lib/sentry/client.ex

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ defmodule Sentry.Client do
4040

4141
require Logger
4242

43-
@type send_event_result :: {:ok, Task.t() | String.t() | pid()} | :error | :unsampled
43+
@type send_event_result ::
44+
{:ok, Task.t() | String.t() | pid()} | :error | :unsampled | :excluded
4445
@type dsn :: {String.t(), String.t(), String.t()} | :error
4546
@sentry_version 5
4647
@max_attempts 4
@@ -66,10 +67,15 @@ defmodule Sentry.Client do
6667

6768
event = maybe_call_before_send_event(event)
6869

69-
if sample_event?(sample_rate) do
70-
encode_and_send(event, result)
71-
else
72-
:unsampled
70+
case {event, sample_event?(sample_rate)} do
71+
{false, _} ->
72+
:excluded
73+
74+
{%Event{}, false} ->
75+
:unsampled
76+
77+
{%Event{}, true} ->
78+
encode_and_send(event, result)
7379
end
7480
end
7581

@@ -257,14 +263,14 @@ defmodule Sentry.Client do
257263
result
258264
end
259265

260-
@spec maybe_call_before_send_event(Event.t()) :: Event.t()
266+
@spec maybe_call_before_send_event(Event.t()) :: Event.t() | false
261267
def maybe_call_before_send_event(event) do
262268
case Config.before_send_event() do
263269
function when is_function(function, 1) ->
264-
function.(event)
270+
function.(event) || false
265271

266272
{module, function} ->
267-
apply(module, function, [event])
273+
apply(module, function, [event]) || false
268274

269275
nil ->
270276
event

lib/sentry/event.ex

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defmodule Sentry.Event do
2020
server_name: nil,
2121
environment: nil,
2222
exception: nil,
23+
original_exception: nil,
2324
release: nil,
2425
stacktrace: %{
2526
frames: []
@@ -29,7 +30,8 @@ defmodule Sentry.Event do
2930
user: %{},
3031
breadcrumbs: [],
3132
fingerprint: [],
32-
modules: %{}
33+
modules: %{},
34+
event_source: nil
3335

3436
@type t :: %__MODULE__{}
3537

@@ -47,14 +49,16 @@ defmodule Sentry.Event do
4749
@doc """
4850
Creates an Event struct out of context collected and options
4951
## Options
50-
* `:exception` - expection
52+
* `:exception` - Sentry-formatted exception
53+
* `:original_exception` - Original exception
5154
* `:message` - message
5255
* `:stacktrace` - a list of Exception.stacktrace()
5356
* `:extra` - map of extra context
5457
* `:user` - map of user context
5558
* `:tags` - map of tags context
5659
* `:request` - map of request context
5760
* `:breadcrumbs` - list of breadcrumbs
61+
* `:event_source` - the source of the event
5862
* `:level` - error level
5963
* `:fingerprint` - list of the fingerprint for grouping this event
6064
"""
@@ -69,9 +73,12 @@ defmodule Sentry.Event do
6973
} = Sentry.Context.get_all()
7074

7175
exception = Keyword.get(opts, :exception)
76+
original_exception = Keyword.get(opts, :original_exception)
7277

7378
message = Keyword.get(opts, :message)
7479

80+
event_source = Keyword.get(opts, :event_source)
81+
7582
stacktrace =
7683
Keyword.get(opts, :stacktrace, [])
7784
|> coerce_stacktrace()
@@ -113,6 +120,7 @@ defmodule Sentry.Event do
113120
environment: env,
114121
server_name: server_name,
115122
exception: exception,
123+
original_exception: original_exception,
116124
stacktrace: %{
117125
frames: stacktrace_to_frames(stacktrace)
118126
},
@@ -123,7 +131,8 @@ defmodule Sentry.Event do
123131
breadcrumbs: breadcrumbs,
124132
request: request,
125133
fingerprint: fingerprint,
126-
modules: Util.mix_deps_versions(@deps)
134+
modules: Util.mix_deps_versions(@deps),
135+
event_source: event_source
127136
}
128137
|> add_metadata()
129138
end
@@ -163,7 +172,7 @@ defmodule Sentry.Event do
163172
end
164173

165174
module = Keyword.get(opts, :module)
166-
exception = [%{type: type, value: value, module: module}]
175+
transformed_exception = [%{type: type, value: value, module: module}]
167176

168177
message =
169178
:error
@@ -172,8 +181,9 @@ defmodule Sentry.Event do
172181
|> String.trim()
173182

174183
opts
175-
|> Keyword.put(:exception, exception)
184+
|> Keyword.put(:exception, transformed_exception)
176185
|> Keyword.put(:message, message)
186+
|> Keyword.put(:original_exception, exception)
177187
|> create_event()
178188
end
179189

0 commit comments

Comments
 (0)