Skip to content

Commit ef0913f

Browse files
Merge pull request #109 from rafaeljesus/read-dsn-from-system
Read dsn from system
2 parents 0b106b7 + c2794cf commit ef0913f

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

lib/mix/tasks/sentry.send_test_event.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
55

66
def run(_args) do
77
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
8+
{endpoint, public_key, secret_key} = Sentry.Client.get_dsn!
139

1410
included_environments = case Application.fetch_env(:sentry, :included_environments) do
1511
{:ok, envs} when is_list(envs) -> envs
@@ -30,11 +26,15 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
3026
maybe_send_event(environment_name, included_environments)
3127
end
3228

33-
def maybe_send_event(env_name, included_envs) do
29+
defp maybe_send_event(env_name, included_envs) do
3430
if env_name in included_envs do
3531
Mix.shell.info "Sending test event..."
36-
Sentry.capture_exception(RuntimeError.exception("Testing sending Sentry event"))
37-
|> Task.await()
32+
33+
"Testing sending Sentry event"
34+
|> RuntimeError.exception
35+
|> Sentry.capture_exception
36+
|> Task.await
37+
3838
Mix.shell.info "Test event sent!"
3939
else
4040
Mix.shell.info "#{inspect env_name} is not in #{inspect included_envs} so no test event will be sent"

lib/sentry/client.ex

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Sentry.Client do
22
alias Sentry.{Event, Util}
33
require Logger
4-
@type parsed_dsn :: {String.t, String.t, Integer.t}
4+
@type get_dsn :: {String.t, String.t, Integer.t}
55
@sentry_version 5
66
@max_attempts 4
77

@@ -21,7 +21,7 @@ defmodule Sentry.Client do
2121
"""
2222
@spec send_event(%Event{}) :: {:ok, String.t} | :error
2323
def send_event(%Event{} = event) do
24-
{endpoint, public_key, secret_key} = parse_dsn!(Application.fetch_env!(:sentry, :dsn))
24+
{endpoint, public_key, secret_key} = get_dsn!
2525

2626
auth_headers = authorization_headers(public_key, secret_key)
2727
body = Poison.encode!(event)
@@ -94,12 +94,12 @@ defmodule Sentry.Client do
9494
end
9595

9696
@doc """
97-
Parses a Sentry DSN which is simply a URI.
97+
Get a Sentry DSN which is simply a URI.
9898
"""
99-
@spec parse_dsn!(String.t) :: parsed_dsn
100-
def parse_dsn!(dsn) do
99+
@spec get_dsn! :: get_dsn
100+
def get_dsn! do
101101
# {PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}
102-
%URI{userinfo: userinfo, host: host, port: port, path: path, scheme: protocol} = URI.parse(dsn)
102+
%URI{userinfo: userinfo, host: host, port: port, path: path, scheme: protocol} = URI.parse(fetch_dsn)
103103
[public_key, secret_key] = String.split(userinfo, ":", parts: 2)
104104
[_, binary_project_id] = String.split(path, "/")
105105
project_id = String.to_integer(binary_project_id)
@@ -108,6 +108,13 @@ defmodule Sentry.Client do
108108
{endpoint, public_key, secret_key}
109109
end
110110

111+
defp fetch_dsn do
112+
case Application.fetch_env!(:sentry, :dsn) do
113+
{:system, env_var} -> System.get_env(env_var)
114+
value -> value
115+
end
116+
end
117+
111118
defp log_api_error(body) do
112119
Logger.warn(fn ->
113120
["Failed to send sentry event.", ?\n, body]

test/client_test.exs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@ defmodule Sentry.ClientTest do
55
alias Sentry.Client
66
@sentry_dsn "https://public:[email protected]/1"
77

8+
setup do
9+
Application.put_env(:sentry, :dsn, @sentry_dsn)
10+
end
11+
812
test "authorization" do
9-
{_endpoint, public_key, private_key} = Client.parse_dsn!("https://public:[email protected]/1")
13+
{_endpoint, public_key, private_key} = Client.get_dsn!
1014
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/
1115
end
1216

13-
test "parsing dsn" do
14-
assert {"https://app.getsentry.com:443/api/1/store/", "public", "secret"} =
15-
Sentry.Client.parse_dsn!("https://public:[email protected]/1")
17+
test "get dsn with default config" do
18+
assert {"https://app.getsentry.com:443/api/1/store/", "public", "secret"} = Sentry.Client.get_dsn!
19+
end
20+
21+
test "get dsn with system config" do
22+
System.put_env("SYSTEM_KEY", @sentry_dsn)
23+
Application.put_env(:sentry, :dsn, {:system, "SYSTEM_KEY"})
24+
assert {"https://app.getsentry.com:443/api/1/store/", "public", "secret"} = Sentry.Client.get_dsn!
1625

17-
assert {"http://app.getsentry.com:9000/api/1/store/", "public", "secret"} =
18-
Sentry.Client.parse_dsn!("http://public:[email protected]:9000/1")
26+
System.delete_env("SYSTEM_KEY")
1927
end
2028

2129
test "logs api errors" do
@@ -24,7 +32,8 @@ defmodule Sentry.ClientTest do
2432
{:ok, _body, conn} = Plug.Conn.read_body(conn)
2533
assert conn.request_path == "/api/1/store/"
2634
assert conn.method == "POST"
27-
Plug.Conn.put_resp_header(conn, "X-Sentry-Error", "Creation of this event was denied due to rate limiting.")
35+
conn
36+
|> Plug.Conn.put_resp_header("X-Sentry-Error", "Creation of this event was denied due to rate limiting.")
2837
|> Plug.Conn.resp(400, "Something bad happened")
2938
end
3039

test/support/test_client.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Sentry.TestClient do
22
def send_event(%Sentry.Event{} = event) do
3-
{endpoint, _public_key, _secret_key} = Sentry.Client.parse_dsn!(Application.fetch_env!(:sentry, :dsn))
3+
{endpoint, _public_key, _secret_key} = Sentry.Client.get_dsn!
44
body = Poison.encode!(event)
55
Sentry.Client.request(:post, endpoint, [], body)
66
end

0 commit comments

Comments
 (0)