@@ -2,79 +2,88 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
2
2
use Mix.Task
3
3
alias Sentry.Config
4
4
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
+
6
7
@ 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
+
8
14
"""
9
15
10
- def run ( args ) do
16
+ @ impl true
17
+ def run ( args ) when is_list ( args ) do
11
18
unless "--no-compile" in args do
12
19
Mix.Task . run ( "compile" , args )
13
20
end
14
21
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
16
29
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 ( ) )
19
45
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
21
54
end
22
55
23
- defp print_environment_info ( { endpoint , public_key , secret_key } ) do
56
+ defp print_environment_info ( { endpoint , public_key , secret_key } , included_environments ) do
24
57
Mix . shell ( ) . info ( "Client configuration:" )
25
58
Mix . shell ( ) . info ( "server: #{ endpoint } " )
26
59
Mix . shell ( ) . info ( "public_key: #{ public_key } " )
27
60
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 ) } " )
29
62
Mix . shell ( ) . info ( "current environment_name: #{ inspect ( to_string ( Config . environment_name ( ) ) ) } " )
30
63
Mix . shell ( ) . info ( "hackney_opts: #{ inspect ( Config . hackney_opts ( ) ) } \n " )
31
64
end
32
65
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..." )
44
68
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 )
48
71
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 , "\n Event ID: #{ id } " ] )
51
75
52
- { :current_stacktrace , stacktrace } = Process . info ( self ( ) , :current_stacktrace )
76
+ { :error , reason } ->
77
+ Mix . raise ( "Error sending event:\n \n #{ inspect ( reason ) } " )
53
78
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" )
58
81
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
+ """ )
78
87
end
79
88
end
80
89
end
0 commit comments