Skip to content

Commit

Permalink
wip - add options validation for send_transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Dec 27, 2024
1 parent 7632cd9 commit 78c75f0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
6 changes: 3 additions & 3 deletions lib/sentry/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ defmodule Sentry.Client do
Event,
Interfaces,
LoggerUtils,
Transport,
Options,
Transaction
Transaction,
Transport
}

require Logger
Expand Down Expand Up @@ -109,7 +109,7 @@ defmodule Sentry.Client do
end

def send_transaction(%Transaction{} = transaction, opts \\ []) do
# opts = validate_options!(opts)
opts = NimbleOptions.validate!(opts, Options.send_transaction_schema())

result_type = Keyword.get_lazy(opts, :result, &Config.send_result/0)
client = Keyword.get_lazy(opts, :client, &Config.client/0)
Expand Down
60 changes: 36 additions & 24 deletions lib/sentry/options.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Sentry.Options do
@moduledoc false

@send_event_opts_schema_as_keyword [
@common_opts_schema_as_keyword [
result: [
type: {:in, [:sync, :none]},
doc: """
Expand All @@ -15,29 +15,6 @@ defmodule Sentry.Options do
call ends up being successful or not.
"""
],
sample_rate: [
type: :float,
doc: """
Same as the global `:sample_rate` configuration, but applied only to
this call. See the module documentation. *Available since v10.0.0*.
"""
],
before_send: [
type: {:or, [{:fun, 1}, {:tuple, [:atom, :atom]}]},
type_doc: "`t:before_send_event_callback/0`",
doc: """
Same as the global `:before_send` configuration, but
applied only to this call. See the module documentation. *Available since v10.0.0*.
"""
],
after_send_event: [
type: {:or, [{:fun, 2}, {:tuple, [:atom, :atom]}]},
type_doc: "`t:after_send_event_callback/0`",
doc: """
Same as the global `:after_send_event` configuration, but
applied only to this call. See the module documentation. *Available since v10.0.0*.
"""
],
client: [
type: :atom,
type_doc: "`t:module/0`",
Expand All @@ -54,6 +31,32 @@ defmodule Sentry.Options do
]
]

@send_event_opts_schema_as_keyword Keyword.merge(@common_opts_schema_as_keyword,
sample_rate: [
type: :float,
doc: """
Same as the global `:sample_rate` configuration, but applied only to
this call. See the module documentation. *Available since v10.0.0*.
"""
],
before_send: [
type: {:or, [{:fun, 1}, {:tuple, [:atom, :atom]}]},
type_doc: "`t:before_send_event_callback/0`",
doc: """
Same as the global `:before_send` configuration, but
applied only to this call. See the module documentation. *Available since v10.0.0*.
"""
],
after_send_event: [
type: {:or, [{:fun, 2}, {:tuple, [:atom, :atom]}]},
type_doc: "`t:after_send_event_callback/0`",
doc: """
Same as the global `:after_send_event` configuration, but
applied only to this call. See the module documentation. *Available since v10.0.0*.
"""
]
)

@create_event_opts_schema_as_keyword [
exception: [
type: {:custom, Sentry.Event, :__validate_exception__, [:exception]},
Expand Down Expand Up @@ -191,6 +194,10 @@ defmodule Sentry.Options do

@create_event_opts_schema NimbleOptions.new!(@create_event_opts_schema_as_keyword)

@send_transaction_opts_schema_as_keyword Keyword.merge(@common_opts_schema_as_keyword, [])

@send_transaction_opts_schema NimbleOptions.new!(@send_transaction_opts_schema_as_keyword)

@spec send_event_schema() :: NimbleOptions.t()
def send_event_schema do
@send_event_opts_schema
Expand All @@ -206,6 +213,11 @@ defmodule Sentry.Options do
@create_event_opts_schema
end

@spec send_transaction_schema() :: NimbleOptions.t()
def send_transaction_schema do
@send_transaction_opts_schema
end

@spec docs_for(atom()) :: String.t()
def docs_for(type)

Expand Down
6 changes: 6 additions & 0 deletions test/sentry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ defmodule SentryTest do
assert {:ok, "340"} = Sentry.send_transaction(transaction)
end

test "validates options", %{transaction: transaction} do
assert_raise NimbleOptions.ValidationError, fn ->
Sentry.send_transaction(transaction, client: "oops")
end
end

test "ignores transaction when dsn is not configured", %{transaction: transaction} do
put_test_config(dsn: nil, test_mode: false)

Expand Down

0 comments on commit 78c75f0

Please sign in to comment.