Skip to content

Commit 791d93b

Browse files
committed
fix(tests): accept nil as the DSN value again
1 parent 6ee9795 commit 791d93b

3 files changed

Lines changed: 72 additions & 40 deletions

File tree

lib/sentry.ex

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,6 @@ defmodule Sentry do
380380
ClientReport.Sender.record_discarded_events(:event_processor, [event])
381381
:ignored
382382

383-
!Config.dsn() ->
384-
# We still validate options even if we're not sending the event. This aims at catching
385-
# configuration issues during development instead of only when deploying to production.
386-
_options = NimbleOptions.validate!(options, Options.send_event_schema())
387-
:ignored
388-
389383
included_envs == :all or to_string(Config.environment_name()) in included_envs ->
390384
Client.send_event(event, options)
391385

@@ -399,12 +393,6 @@ defmodule Sentry do
399393
included_envs = Config.included_environments()
400394

401395
cond do
402-
!Config.dsn() ->
403-
# We still validate options even if we're not sending the event. This aims at catching
404-
# configuration issues during development instead of only when deploying to production.
405-
_options = NimbleOptions.validate!(options, Options.send_event_schema())
406-
:ignored
407-
408396
included_envs == :all or to_string(Config.environment_name()) in included_envs ->
409397
Client.send_transaction(transaction, options)
410398

@@ -458,13 +446,9 @@ defmodule Sentry do
458446
@spec capture_check_in(keyword()) ::
459447
{:ok, check_in_id :: String.t()} | :ignored | {:error, ClientError.t()}
460448
def capture_check_in(options) when is_list(options) do
461-
if Config.dsn() do
462-
options
463-
|> CheckIn.new()
464-
|> Client.send_check_in(options)
465-
else
466-
:ignored
467-
end
449+
options
450+
|> CheckIn.new()
451+
|> Client.send_check_in(options)
468452
end
469453

470454
@doc ~S"""

lib/sentry/client.ex

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,35 @@ defmodule Sentry.Client do
2727
@max_message_length 8_192
2828

2929
@spec send_check_in(CheckIn.t(), keyword()) ::
30-
{:ok, check_in_id :: String.t()} | {:error, ClientError.t()}
30+
{:ok, check_in_id :: String.t()} | :ignored | {:error, ClientError.t()}
3131
def send_check_in(%CheckIn{} = check_in, opts) when is_list(opts) do
32-
if Config.telemetry_processor_category?(:check_in) do
33-
case TelemetryProcessor.add(check_in) do
34-
{:ok, {:rate_limited, data_category}} ->
35-
ClientReport.Sender.record_discarded_events(:ratelimit_backoff, data_category)
32+
cond do
33+
is_nil(Config.dsn()) ->
34+
:ignored
3635

37-
:ok ->
38-
:ok
39-
end
36+
Config.telemetry_processor_category?(:check_in) ->
37+
case TelemetryProcessor.add(check_in) do
38+
{:ok, {:rate_limited, data_category}} ->
39+
ClientReport.Sender.record_discarded_events(:ratelimit_backoff, data_category)
4040

41-
{:ok, check_in.check_in_id}
42-
else
43-
client = Keyword.get_lazy(opts, :client, &Config.client/0)
41+
:ok ->
42+
:ok
43+
end
4444

45-
# This is a "private" option, only really used in testing.
46-
request_retries =
47-
Keyword.get_lazy(opts, :request_retries, fn ->
48-
Application.get_env(:sentry, :request_retries, Transport.default_retries())
49-
end)
45+
{:ok, check_in.check_in_id}
46+
47+
true ->
48+
client = Keyword.get_lazy(opts, :client, &Config.client/0)
5049

51-
check_in
52-
|> Envelope.from_check_in()
53-
|> Transport.encode_and_post_envelope(client, request_retries)
50+
# This is a "private" option, only really used in testing.
51+
request_retries =
52+
Keyword.get_lazy(opts, :request_retries, fn ->
53+
Application.get_env(:sentry, :request_retries, Transport.default_retries())
54+
end)
55+
56+
check_in
57+
|> Envelope.from_check_in()
58+
|> Transport.encode_and_post_envelope(client, request_retries)
5459
end
5560
end
5661

@@ -59,6 +64,7 @@ defmodule Sentry.Client do
5964
@spec send_event(Event.t(), keyword()) ::
6065
{:ok, event_id :: String.t()}
6166
| {:error, ClientError.t()}
67+
| :ignored
6268
| :unsampled
6369
| :excluded
6470
def send_event(%Event{} = event, opts) when is_list(opts) do
@@ -79,7 +85,8 @@ defmodule Sentry.Client do
7985
result =
8086
with {:ok, %Event{} = event} <- maybe_call_before_send(event, before_send),
8187
:ok <- sample_event(sample_rate),
82-
:ok <- maybe_dedupe(event) do
88+
:ok <- maybe_dedupe(event),
89+
:ok <- ensure_dsn_configured() do
8390
send_result = encode_and_send(event, result_type, client, request_retries)
8491
_ignored = maybe_call_after_send(event, send_result, after_send_event)
8592
send_result
@@ -99,6 +106,9 @@ defmodule Sentry.Client do
99106
:excluded ->
100107
:excluded
101108

109+
:ignored ->
110+
:ignored
111+
102112
{:error, %ClientError{} = error} ->
103113
{:error, error}
104114
end
@@ -120,6 +130,7 @@ defmodule Sentry.Client do
120130
@spec send_transaction(Transaction.t(), keyword()) ::
121131
{:ok, transaction_id :: String.t()}
122132
| {:error, ClientError.t()}
133+
| :ignored
123134
| :excluded
124135
def send_transaction(%Transaction{} = transaction, opts \\ []) do
125136
opts = NimbleOptions.validate!(opts, Options.send_transaction_schema())
@@ -134,16 +145,24 @@ defmodule Sentry.Client do
134145
Application.get_env(:sentry, :request_retries, Transport.default_retries())
135146
end)
136147

137-
with {:ok, %Transaction{} = transaction} <- maybe_call_before_send(transaction, before_send) do
148+
with {:ok, %Transaction{} = transaction} <- maybe_call_before_send(transaction, before_send),
149+
:ok <- ensure_dsn_configured() do
138150
send_result = encode_and_send(transaction, result_type, client, request_retries)
139151
_ignored = maybe_call_after_send(transaction, send_result, after_send_event)
140152
send_result
141153
else
142154
:excluded ->
143155
:excluded
156+
157+
:ignored ->
158+
:ignored
144159
end
145160
end
146161

162+
defp ensure_dsn_configured do
163+
if Config.dsn(), do: :ok, else: :ignored
164+
end
165+
147166
defp sample_event(sample_rate) do
148167
cond do
149168
sample_rate == 1 -> :ok

test/sentry_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,35 @@ defmodule SentryTest do
136136
assert :ignored = Sentry.send_event(event)
137137
end
138138

139+
describe "with test_mode: true and dsn: nil" do
140+
setup do
141+
put_test_config(dsn: nil)
142+
:ok
143+
end
144+
145+
test "capture_exception/2 still routes events to the test collector" do
146+
assert :ignored = Sentry.capture_exception(%RuntimeError{message: "boom"})
147+
148+
assert [%Sentry.Event{} = event] = Sentry.Test.pop_sentry_reports()
149+
assert [%Sentry.Interfaces.Exception{type: "RuntimeError", value: "boom"}] = event.exception
150+
end
151+
152+
test "capture_message/2 still routes events to the test collector" do
153+
assert :ignored = Sentry.capture_message("hello world")
154+
155+
assert [%Sentry.Event{} = event] = Sentry.Test.pop_sentry_reports()
156+
assert event.message.formatted == "hello world"
157+
end
158+
159+
test "send_transaction/2 still routes transactions to the test collector" do
160+
transaction = create_transaction()
161+
162+
assert :ignored = Sentry.send_transaction(transaction)
163+
164+
assert [%Sentry.Transaction{}] = Sentry.Test.pop_sentry_transactions()
165+
end
166+
end
167+
139168
describe "send_check_in/1" do
140169
test "posts a check-in with all the explicit arguments", %{bypass: bypass} do
141170
put_test_config(environment_name: "test", release: "1.3.2")

0 commit comments

Comments
 (0)