Skip to content

Commit a787475

Browse files
committed
Move endpoint and headers logic to Config
1 parent 3e820f7 commit a787475

File tree

5 files changed

+46
-34
lines changed

5 files changed

+46
-34
lines changed

lib/mix/tasks/sentry.send_test_event.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
6161
Mix.shell().info("Client configuration:")
6262

6363
if Config.dsn() do
64-
{endpoint, public_key, secret_key} = Config.dsn()
65-
Mix.shell().info("server: #{endpoint}")
64+
{base_uri, public_key, secret_key} = Config.dsn()
65+
Mix.shell().info("server: #{base_uri}")
6666
Mix.shell().info("public_key: #{public_key}")
6767
Mix.shell().info("secret_key: #{secret_key}")
6868
end

lib/sentry/config.ex

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ defmodule Sentry.Config do
389389
@opts_schema NimbleOptions.new!(@raw_opts_schema)
390390
@valid_keys Keyword.keys(@raw_opts_schema)
391391

392+
@sentry_version 5
393+
@sentry_client "sentry-elixir/#{Mix.Project.config()[:version]}"
394+
392395
@spec validate!() :: keyword()
393396
def validate! do
394397
:sentry
@@ -461,6 +464,39 @@ defmodule Sentry.Config do
461464
@spec dsn() :: nil | {String.t(), String.t(), String.t()}
462465
def dsn, do: get(:dsn)
463466

467+
@spec envelope_endpoint() :: String.t()
468+
def envelope_endpoint do
469+
{base_uri, _, _} = dsn()
470+
base_uri <> "envelope/"
471+
end
472+
473+
@spec spans_endpoint() :: String.t()
474+
def spans_endpoint do
475+
{base_uri, _, _} = dsn()
476+
base_uri <> "spans/"
477+
end
478+
479+
@spec auth_headers() :: [{String.t(), String.t()}]
480+
def auth_headers do
481+
{_, public_key, secret_key} = dsn()
482+
483+
auth_query =
484+
[
485+
sentry_version: @sentry_version,
486+
sentry_client: @sentry_client,
487+
sentry_timestamp: System.system_time(:second),
488+
sentry_key: public_key,
489+
sentry_secret: secret_key
490+
]
491+
|> Enum.reject(fn {_, value} -> is_nil(value) end)
492+
|> Enum.map_join(", ", fn {name, value} -> "#{name}=#{value}" end)
493+
494+
[
495+
{"User-Agent", @sentry_client},
496+
{"X-Sentry-Auth", "Sentry " <> auth_query}
497+
]
498+
end
499+
464500
# TODO: remove me on v11.0.0, :included_environments has been deprecated
465501
# in v10.0.0.
466502
@spec included_environments() :: :all | [String.t()]
@@ -710,10 +746,10 @@ defmodule Sentry.Config do
710746
end
711747

712748
with {:ok, {base_path, project_id}} <- pop_project_id(uri.path) do
713-
new_path = Enum.join([base_path, "api", project_id, "envelope"], "/") <> "/"
714-
endpoint_uri = URI.merge(%URI{uri | userinfo: nil}, new_path)
749+
new_path = Enum.join([base_path, "api", project_id], "/") <> "/"
750+
base_uri = URI.merge(%URI{uri | userinfo: nil}, new_path)
715751

716-
{:ok, {URI.to_string(endpoint_uri), public_key, secret_key}}
752+
{:ok, {URI.to_string(base_uri), public_key, secret_key}}
717753
end
718754
catch
719755
message -> {:error, message}

lib/sentry/transport.ex

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ defmodule Sentry.Transport do
77
alias Sentry.Envelope
88

99
@default_retries [1000, 2000, 4000, 8000]
10-
@sentry_version 5
11-
@sentry_client "sentry-elixir/#{Mix.Project.config()[:version]}"
1210

1311
@spec default_retries() :: [pos_integer(), ...]
1412
def default_retries do
@@ -22,7 +20,7 @@ defmodule Sentry.Transport do
2220
when is_atom(client) and is_list(retries) do
2321
case Envelope.to_binary(envelope) do
2422
{:ok, body} ->
25-
{endpoint, headers} = get_endpoint_and_headers()
23+
{endpoint, headers} = {Config.envelope_endpoint(), Config.auth_headers()}
2624
post_envelope_with_retries(client, endpoint, headers, body, retries)
2725

2826
{:error, reason} ->
@@ -63,26 +61,4 @@ defmodule Sentry.Transport do
6361
catch
6462
kind, data -> {:error, {kind, data, __STACKTRACE__}}
6563
end
66-
67-
defp get_endpoint_and_headers do
68-
{endpoint, public_key, secret_key} = Config.dsn()
69-
70-
auth_query =
71-
[
72-
sentry_version: @sentry_version,
73-
sentry_client: @sentry_client,
74-
sentry_timestamp: System.system_time(:second),
75-
sentry_key: public_key,
76-
sentry_secret: secret_key
77-
]
78-
|> Enum.reject(fn {_, value} -> is_nil(value) end)
79-
|> Enum.map_join(", ", fn {name, value} -> "#{name}=#{value}" end)
80-
81-
auth_headers = [
82-
{"User-Agent", @sentry_client},
83-
{"X-Sentry-Auth", "Sentry " <> auth_query}
84-
]
85-
86-
{endpoint, auth_headers}
87-
end
8864
end

test/mix/sentry.send_test_event_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
4545

4646
assert output =~ """
4747
Client configuration:
48-
server: http://localhost:#{bypass.port}/api/1/envelope/
48+
server: http://localhost:#{bypass.port}/api/1/
4949
public_key: public
5050
secret_key: secret
5151
current environment_name: "test"

test/sentry/config_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ defmodule Sentry.ConfigTest do
66
describe "validate!/0" do
77
test ":dsn from option" do
88
assert Config.validate!(dsn: "https://public:[email protected]/1")[:dsn] ==
9-
{"https://app.getsentry.com/api/1/envelope/", "public", "secret"}
9+
{"https://app.getsentry.com/api/1/", "public", "secret"}
1010

1111
assert Config.validate!(dsn: nil)[:dsn] == nil
1212
end
1313

1414
test ":dsn from system environment" do
1515
with_system_env("SENTRY_DSN", "https://public:[email protected]/1", fn ->
1616
assert Config.validate!([])[:dsn] ==
17-
{"https://app.getsentry.com/api/1/envelope/", "public", "secret"}
17+
{"https://app.getsentry.com/api/1/", "public", "secret"}
1818
end)
1919
end
2020

@@ -213,7 +213,7 @@ defmodule Sentry.ConfigTest do
213213
assert :ok = Config.put_config(:dsn, new_dsn)
214214

215215
assert Config.dsn() ==
216-
{"https://app.getsentry.com/api/2/envelope/", "public", "secret"}
216+
{"https://app.getsentry.com/api/2/", "public", "secret"}
217217
end
218218

219219
test "validates the given key" do

0 commit comments

Comments
 (0)