Skip to content

Commit 7b11ca1

Browse files
Merge pull request #68 from getsentry/configuration-test-task
Configuration test task
2 parents e2b7241 + 4bca39e commit 7b11ca1

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ config :sentry,
7878
| `server_name` | False | None | |
7979
| `use_error_logger` | False | False | |
8080

81+
## Testing Your Configuration
82+
83+
To ensure you've set up your configuration correctly we recommend running the
84+
included mix task. It can be tested on different Mix environments and will tell you if it is not currently configured to send events in that environment:
85+
86+
```bash
87+
$ MIX_ENV=dev mix sentry.send_test_event
88+
Client configuration:
89+
server: https://sentry.io/
90+
public_key: public
91+
secret_key: secret
92+
included_environments: [:prod]
93+
current environment_name: :dev
94+
95+
:dev is not in [:prod] so no test event will be sent
96+
97+
$ MIX_ENV=prod mix sentry.send_test_event
98+
Client configuration:
99+
server: https://sentry.io/
100+
public_key: public
101+
secret_key: secret
102+
included_environments: [:prod]
103+
current environment_name: :prod
104+
105+
Sending test event!
106+
```
81107

82108
## Docs
83109

docs/config.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,30 @@ Optional settings
5151

5252
Set this to true if you want to capture all exceptions that occur even outside of a request cycle. This
5353
defaults to false.
54+
55+
Testing Your Configuration
56+
--------------------------
57+
58+
To ensure you've set up your configuration correctly we recommend running the
59+
included mix task. It can be tested on different Mix environments and will tell you if it is not currently configured to send events in that environment:
60+
61+
.. code-block:: bash
62+
$ MIX_ENV=dev mix sentry.send_test_event
63+
Client configuration:
64+
server: https://sentry.io/
65+
public_key: public
66+
secret_key: secret
67+
included_environments: [:prod]
68+
current environment_name: :dev
69+
70+
:dev is not in [:prod] so no test event will be sent
71+
72+
$ MIX_ENV=prod mix sentry.send_test_event
73+
Client configuration:
74+
server: https://sentry.io/
75+
public_key: public
76+
secret_key: secret
77+
included_environments: [:prod]
78+
current environment_name: :prod
79+
80+
Sending test event!
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule Mix.Tasks.Sentry.SendTestEvent do
2+
use Mix.Task
3+
4+
@shortdoc "Attempts to send a test event to check Sentry configuration"
5+
6+
def run(_args) do
7+
Application.ensure_all_started(:sentry)
8+
{endpoint, public_key, secret_key} = case Application.fetch_env(:sentry, :dsn) do
9+
{:ok, dsn} when is_binary(dsn) -> Sentry.Client.parse_dsn!(dsn)
10+
_ ->
11+
Mix.raise "Sentry DSN is not configured in :sentry, :dsn"
12+
end
13+
14+
included_environments = case Application.fetch_env(:sentry, :included_environments) do
15+
{:ok, envs} when is_list(envs) -> envs
16+
_ ->
17+
Mix.raise "Sentry included_environments is not configured in :sentry, :included_environments"
18+
end
19+
20+
environment_name = Application.get_env(:sentry, :environment_name)
21+
Mix.shell.info "Client configuration:"
22+
Mix.shell.info "server: #{endpoint}"
23+
Mix.shell.info "public_key: #{public_key}"
24+
Mix.shell.info "secret_key: #{secret_key}"
25+
Mix.shell.info "included_environments: #{inspect included_environments}"
26+
Mix.shell.info "current environment_name: #{inspect environment_name}\n"
27+
28+
maybe_send_event(environment_name, included_environments)
29+
end
30+
31+
def maybe_send_event(env_name, included_envs) do
32+
if env_name in included_envs do
33+
Mix.shell.info "Sending test event!"
34+
Sentry.capture_exception(RuntimeError.exception("Testing sending Sentry event"))
35+
else
36+
Mix.shell.info "#{inspect env_name} is not in #{inspect included_envs} so no test event will be sent"
37+
end
38+
end
39+
end

test/client_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Sentry.ClientTest do
55

66
test "authorization" do
77
{_endpoint, public_key, private_key} = Client.parse_dsn!("https://public:[email protected]/1")
8-
assert Client.authorization_header(public_key, private_key) =~ ~r/Sentry sentry_version=5, sentry_client=sentry-elixir\/1.0.0, sentry_timestamp=\d{10}, sentry_key=public, sentry_secret=secret/
8+
assert Client.authorization_header(public_key, private_key) =~ ~r/Sentry sentry_version=5, sentry_client=sentry-elixir\/#{Application.spec(:sentry, :vsn)}, sentry_timestamp=\d{10}, sentry_key=public, sentry_secret=secret/
99
end
1010

1111
test "parning dsn" do

0 commit comments

Comments
 (0)