Skip to content

Commit c2794cf

Browse files
committed
Exposes get_dsn! function
1 parent 131a2d8 commit c2794cf

File tree

4 files changed

+35
-37
lines changed

4 files changed

+35
-37
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 & 13 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} = dsn_env |> parse_dsn!
24+
{endpoint, public_key, secret_key} = get_dsn!
2525

2626
auth_headers = authorization_headers(public_key, secret_key)
2727
body = Poison.encode!(event)
@@ -31,13 +31,6 @@ defmodule Sentry.Client do
3131
end)
3232
end
3333

34-
def dsn_env do
35-
case Application.fetch_env!(:sentry, :dsn) do
36-
{:system, env_var} -> System.get_env(env_var)
37-
value -> value
38-
end
39-
end
40-
4134
defp try_request(method, url, headers, body) do
4235
do_try_request(method, url, headers, body, 1)
4336
end
@@ -101,12 +94,12 @@ defmodule Sentry.Client do
10194
end
10295

10396
@doc """
104-
Parses a Sentry DSN which is simply a URI.
97+
Get a Sentry DSN which is simply a URI.
10598
"""
106-
@spec parse_dsn!(String.t) :: parsed_dsn
107-
def parse_dsn!(dsn) do
99+
@spec get_dsn! :: get_dsn
100+
def get_dsn! do
108101
# {PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}
109-
%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)
110103
[public_key, secret_key] = String.split(userinfo, ":", parts: 2)
111104
[_, binary_project_id] = String.split(path, "/")
112105
project_id = String.to_integer(binary_project_id)
@@ -115,6 +108,13 @@ defmodule Sentry.Client do
115108
{endpoint, public_key, secret_key}
116109
end
117110

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+
118118
defp log_api_error(body) do
119119
Logger.warn(fn ->
120120
["Failed to send sentry event.", ?\n, body]

test/client_test.exs

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

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

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")
16-
17-
assert {"http://app.getsentry.com:9000/api/1/store/", "public", "secret"} =
18-
Sentry.Client.parse_dsn!("http://public:[email protected]:9000/1")
12+
test "authorization" do
13+
{_endpoint, public_key, private_key} = Client.get_dsn!
14+
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/
1915
end
2016

21-
test "fetches default dsn_env" do
22-
Application.put_env(:sentry, :dsn, @sentry_dsn)
23-
assert @sentry_dsn == Sentry.Client.dsn_env
17+
test "get dsn with default config" do
18+
assert {"https://app.getsentry.com:443/api/1/store/", "public", "secret"} = Sentry.Client.get_dsn!
2419
end
2520

26-
test "fetches system dsn_env" do
21+
test "get dsn with system config" do
2722
System.put_env("SYSTEM_KEY", @sentry_dsn)
2823
Application.put_env(:sentry, :dsn, {:system, "SYSTEM_KEY"})
29-
assert @sentry_dsn == Sentry.Client.dsn_env
24+
assert {"https://app.getsentry.com:443/api/1/store/", "public", "secret"} = Sentry.Client.get_dsn!
25+
26+
System.delete_env("SYSTEM_KEY")
3027
end
3128

3229
test "logs api errors" do
@@ -35,7 +32,8 @@ defmodule Sentry.ClientTest do
3532
{:ok, _body, conn} = Plug.Conn.read_body(conn)
3633
assert conn.request_path == "/api/1/store/"
3734
assert conn.method == "POST"
38-
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.")
3937
|> Plug.Conn.resp(400, "Something bad happened")
4038
end
4139

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)