Skip to content

Commit 7f8442e

Browse files
committed
Improve behavior of :test_mode/:dsn
1 parent faa5dc7 commit 7f8442e

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

lib/sentry.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ defmodule Sentry do
327327
LoggerUtils.log("Cannot report event without message or exception: #{inspect(event)}")
328328
:ignored
329329

330+
# If we're in test mode, let's send the event down the pipeline anyway.
331+
Config.test_mode?() ->
332+
Client.send_event(event, opts)
333+
330334
!Config.dsn() ->
331335
:ignored
332336

lib/sentry/config.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ defmodule Sentry.Config do
6464
doc: """
6565
The DSN for your Sentry project. If this is not set, Sentry will not be enabled.
6666
If the `SENTRY_DSN` environment variable is set, it will be used as the default value.
67+
If `:test_mode` is `true`, the `:dsn` option is sometimes ignored; see `Sentry.Test`
68+
for more information.
6769
"""
6870
],
6971
environment_name: [
@@ -200,7 +202,8 @@ defmodule Sentry.Config do
200202
doc: """
201203
Whether to enable *test mode*. When test mode is enabled, the SDK will check whether
202204
there is a process **collecting events** and avoid sending those events if that's the
203-
case. This is useful for testing. See `Sentry.Test`.
205+
case. This is useful for testing—see `Sentry.Test`. `:test_mode` works in tandem
206+
with `:dsn`; this is described in detail in `Sentry.Test`.
204207
"""
205208
],
206209
integrations: [

lib/sentry/test.ex

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@ defmodule Sentry.Test do
55
## Usage
66
77
This module is based on **collecting** reported events and then retrieving
8-
them to perform assertions. You can start collecting events from a process
8+
them to perform assertions. The functionality here is only available if the
9+
`:test_mode` configuration option is set to `true`—see
10+
[`Sentry`'s configuration section](sentry.html#module-configuration).
11+
You can start collecting events from a process
912
by calling `start_collecting_sentry_reports/0`. Then, you can use Sentry
1013
as normal and report events (through functions such as `Sentry.capture_message/1`
1114
or `Sentry.capture_exception/1`). Finally, you can retrieve the collected events
1215
by calling `pop_sentry_reports/0`.
1316
17+
> #### Test Mode and DSN {: .info}
18+
>
19+
> If `:test_mode` is `true`, the `:dsn` option behaves differently. When `:dsn` is
20+
> not set or `nil` and you're collecting events, you'll still be able to collect
21+
> events—even if under normal circumstances a missing `:dsn` means events don't get
22+
> reported. If `:dsn` is `nil` and you're not collecting events, the event is simply
23+
> ignored. See the table below for a summary for this behavior.
24+
25+
| `:test_mode` | `:dsn` | Collecting events? | Behavior |
26+
|--------------|--------|--------------------|--------------------------------------------------------|
27+
| `true` | `nil` | yes | Event is collected |
28+
| `true` | `nil` | no | Event is ignored (silently) |
29+
| `true` | set | yes | Event is collected |
30+
| `true` | set | no | Makes HTTP request to configured DSN (could be Bypass) |
31+
| `false` | `nil` | irrelevant | Ignores event |
32+
| `false` | set | irrelevant | Makes HTTP request to configured DSN (could be Bypass) |
33+
1434
## Examples
1535
1636
Let's imagine writing a test using the functions in this module. First, we need to
@@ -64,6 +84,7 @@ defmodule Sentry.Test do
6484
@spec maybe_collect(Sentry.Event.t()) :: :collected | :not_collecting
6585
def maybe_collect(%Sentry.Event{} = event) do
6686
if Sentry.Config.test_mode?() do
87+
dsn_set? = not is_nil(Sentry.Config.dsn())
6788
ensure_ownership_server_started()
6889

6990
case NimbleOwnership.fetch_owner(@server, callers(), @key) do
@@ -81,8 +102,13 @@ defmodule Sentry.Test do
81102
raise ArgumentError, "cannot collect Sentry reports: #{Exception.message(error)}"
82103
end
83104

84-
:error ->
105+
:error when dsn_set? ->
85106
:not_collecting
107+
108+
# If the :dsn option is not set and we didn't capture the event, it's alright,
109+
# we can just swallow it.
110+
:error ->
111+
:collected
86112
end
87113
else
88114
:not_collecting

test/sentry_test.exs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,18 @@ defmodule SentryTest do
119119
assert logged_count == 2
120120
end
121121

122-
test "does not send events if :dsn is not configured or nil" do
123-
put_test_config(dsn: nil)
122+
test "does not send events if :dsn is not configured or nil (if not in test mode)" do
123+
put_test_config(dsn: nil, test_mode: false)
124124
event = Sentry.Event.transform_exception(%RuntimeError{message: "oops"}, [])
125125
assert :ignored = Sentry.send_event(event)
126126
end
127127

128+
test "if in test mode, swallows events if the :dsn is nil" do
129+
put_test_config(dsn: nil, test_mode: true)
130+
event = Sentry.Event.transform_exception(%RuntimeError{message: "oops"}, [])
131+
assert {:ok, ""} = Sentry.send_event(event)
132+
end
133+
128134
describe "send_check_in/1" do
129135
test "posts a check-in with all the explicit arguments", %{bypass: bypass} do
130136
put_test_config(environment_name: "test", release: "1.3.2")

0 commit comments

Comments
 (0)