Skip to content

Commit 49c20a6

Browse files
authored
Clean up docs and tests for "mix sentry.send_test_event" (#610)
1 parent aaffdf4 commit 49c20a6

File tree

2 files changed

+81
-77
lines changed

2 files changed

+81
-77
lines changed

lib/mix/tasks/sentry.send_test_event.ex

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,88 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
22
use Mix.Task
33
alias Sentry.Config
44

5-
@shortdoc "Attempts to send a test event to check Sentry configuration"
5+
@shortdoc "Attempts to send a test event to check the Sentry configuration"
6+
67
@moduledoc """
7-
Send test even to check if Sentry configuration is correct.
8+
Send test event to check if Sentry configuration is correct.
9+
10+
## Options
11+
12+
* `--no-compile` - does not compile, even if files require compilation.
13+
814
"""
915

10-
def run(args) do
16+
@impl true
17+
def run(args) when is_list(args) do
1118
unless "--no-compile" in args do
1219
Mix.Task.run("compile", args)
1320
end
1421

15-
Application.ensure_all_started(:sentry)
22+
case Application.ensure_all_started(:sentry) do
23+
{:ok, _apps} ->
24+
:ok
25+
26+
{:error, reason} ->
27+
Mix.raise("Failed to start the :sentry application:\n\n#{inspect(reason)}")
28+
end
1629

17-
Sentry.Transport.get_dsn()
18-
|> print_environment_info()
30+
included_environments =
31+
case Application.fetch_env(:sentry, :included_environments) do
32+
{:ok, envs} when is_list(envs) or envs == :all ->
33+
envs
34+
35+
:error ->
36+
Mix.raise("""
37+
The :included_environments application configuration option is not configured for the \
38+
:sentry application.
39+
""")
40+
end
41+
42+
print_environment_info(Sentry.Transport.get_dsn(), included_environments)
43+
44+
env_name = to_string(Config.environment_name())
1945

20-
maybe_send_event()
46+
if included_environments == :all or env_name in included_environments do
47+
send_event()
48+
else
49+
Mix.shell().info([
50+
:yellow,
51+
"#{inspect(env_name)} is not in #{inspect(included_environments)} so no test event will be sent"
52+
])
53+
end
2154
end
2255

23-
defp print_environment_info({endpoint, public_key, secret_key}) do
56+
defp print_environment_info({endpoint, public_key, secret_key}, included_environments) do
2457
Mix.shell().info("Client configuration:")
2558
Mix.shell().info("server: #{endpoint}")
2659
Mix.shell().info("public_key: #{public_key}")
2760
Mix.shell().info("secret_key: #{secret_key}")
28-
Mix.shell().info("included_environments: #{inspect(included_environments())}")
61+
Mix.shell().info("included_environments: #{inspect(included_environments)}")
2962
Mix.shell().info("current environment_name: #{inspect(to_string(Config.environment_name()))}")
3063
Mix.shell().info("hackney_opts: #{inspect(Config.hackney_opts())}\n")
3164
end
3265

33-
defp included_environments do
34-
case Application.fetch_env(:sentry, :included_environments) do
35-
{:ok, envs} when is_list(envs) or envs == :all ->
36-
envs
37-
38-
_ ->
39-
Mix.raise(
40-
"Sentry included_environments is not configured in :sentry, :included_environments"
41-
)
42-
end
43-
end
66+
defp send_event do
67+
Mix.shell().info("Sending test event...")
4468

45-
defp maybe_send_event do
46-
env_name = to_string(Config.environment_name())
47-
included_envs = included_environments()
69+
exception = %RuntimeError{message: "Testing sending Sentry event"}
70+
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
4871

49-
if included_envs == :all or env_name in included_envs do
50-
Mix.shell().info("Sending test event...")
72+
case Sentry.capture_exception(exception, result: :sync, stacktrace: stacktrace) do
73+
{:ok, id} ->
74+
Mix.shell().info([:green, :bright, "Test event sent", :reset, "\nEvent ID: #{id}"])
5175

52-
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
76+
{:error, reason} ->
77+
Mix.raise("Error sending event:\n\n#{inspect(reason)}")
5378

54-
result =
55-
"Testing sending Sentry event"
56-
|> RuntimeError.exception()
57-
|> Sentry.capture_exception(result: :sync, stacktrace: stacktrace)
79+
:excluded ->
80+
Mix.shell().info("No test event was sent because the event was excluded by a filter")
5881

59-
case result do
60-
{:ok, id} ->
61-
Mix.shell().info("Test event sent! Event ID: #{id}")
62-
63-
{:error, reason} ->
64-
Mix.shell().info("Error sending event: #{inspect(reason)}")
65-
66-
:excluded ->
67-
Mix.shell().info("No test event was sent because the event was excluded by a filter")
68-
69-
:unsampled ->
70-
Mix.shell().info(
71-
"No test event was sent because the event was excluded according to the sample_rate"
72-
)
73-
end
74-
else
75-
Mix.shell().info(
76-
"#{inspect(env_name)} is not in #{inspect(included_envs)} so no test event will be sent"
77-
)
82+
:unsampled ->
83+
Mix.shell().info("""
84+
No test event was sent because the event was excluded according to the :sample_rate \
85+
configuration option.
86+
""")
7887
end
7988
end
8089
end
Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
defmodule Mix.Tasks.Sentry.SendTestEventTest do
22
use ExUnit.Case
3+
34
import ExUnit.CaptureIO
4-
import ExUnit.CaptureLog
55
import Sentry.TestEnvironmentHelper
66

77
test "prints if environment_name is not in included_environments" do
88
modify_env(:sentry, dsn: "http://public:secret@localhost:43/1", included_environments: [])
99

10-
assert capture_io(fn ->
11-
Mix.Tasks.Sentry.SendTestEvent.run([])
12-
end) == """
10+
output =
11+
capture_io(fn ->
12+
Mix.Tasks.Sentry.SendTestEvent.run([])
13+
end)
14+
15+
assert output =~ """
1316
Client configuration:
1417
server: http://localhost:43/api/1/envelope/
1518
public_key: public
1619
secret_key: secret
1720
included_environments: []
1821
current environment_name: "test"
1922
hackney_opts: [recv_timeout: 50]
20-
21-
"test" is not in [] so no test event will be sent
2223
"""
24+
25+
assert output =~ ~s("test" is not in [] so no test event will be sent)
2326
end
2427

2528
test "sends event successfully when configured to" do
@@ -39,22 +42,27 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
3942
included_environments: :all
4043
)
4144

42-
assert capture_io(fn ->
43-
Mix.Tasks.Sentry.SendTestEvent.run([])
44-
end) == """
45+
output =
46+
capture_io(fn ->
47+
Mix.Tasks.Sentry.SendTestEvent.run([])
48+
end)
49+
50+
assert output =~ """
4551
Client configuration:
4652
server: http://localhost:#{bypass.port}/api/1/envelope/
4753
public_key: public
4854
secret_key: secret
4955
included_environments: :all
5056
current environment_name: "test"
5157
hackney_opts: [recv_timeout: 50]
52-
53-
Sending test event...
54-
Test event sent! Event ID: 340
5558
"""
59+
60+
assert output =~ "Sending test event..."
61+
assert output =~ "Test event sent"
62+
assert output =~ "Event ID: 340"
5663
end
5764

65+
@tag :capture_log
5866
test "handles error when Sentry server is failing" do
5967
bypass = Bypass.open()
6068

@@ -68,21 +76,8 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
6876
request_retries: []
6977
)
7078

71-
assert capture_log(fn ->
72-
assert capture_io(fn ->
73-
Mix.Tasks.Sentry.SendTestEvent.run([])
74-
end) == """
75-
Client configuration:
76-
server: http://localhost:#{bypass.port}/api/1/envelope/
77-
public_key: public
78-
secret_key: secret
79-
included_environments: ["test"]
80-
current environment_name: "test"
81-
hackney_opts: [recv_timeout: 50]
82-
83-
Sending test event...
84-
Error sending event: {:request_failure, "Received 500 from Sentry server: "}
85-
"""
86-
end) =~ "Failed to send Sentry event"
79+
assert_raise Mix.Error, ~r/Error sending event/, fn ->
80+
capture_io(fn -> Mix.Tasks.Sentry.SendTestEvent.run([]) end)
81+
end
8782
end
8883
end

0 commit comments

Comments
 (0)