Skip to content

Commit 7adce94

Browse files
committed
Add tests for Sentry.send_transaction
1 parent 62c1fd3 commit 7adce94

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

lib/sentry.ex

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,22 @@ defmodule Sentry do
362362
end
363363
end
364364

365-
def send_transaction(transaction, opts \\ []) do
365+
def send_transaction(transaction, options \\ []) do
366366
# TODO: remove on v11.0.0, :included_environments was deprecated in 10.0.0.
367367
included_envs = Config.included_environments()
368368

369369
cond do
370370
Config.test_mode?() ->
371-
Client.send_transaction(transaction, opts)
371+
Client.send_transaction(transaction, options)
372+
373+
!Config.dsn() ->
374+
# We still validate options even if we're not sending the event. This aims at catching
375+
# configuration issues during development instead of only when deploying to production.
376+
_options = NimbleOptions.validate!(options, Options.send_event_schema())
377+
:ignored
372378

373379
included_envs == :all or to_string(Config.environment_name()) in included_envs ->
374-
Client.send_transaction(transaction, opts)
380+
Client.send_transaction(transaction, options)
375381

376382
true ->
377383
:ignored

test/sentry_test.exs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,41 @@ defmodule SentryTest do
235235
assert Sentry.get_dsn() == random_dsn
236236
end
237237
end
238+
239+
describe "send_transaction/2" do
240+
setup do
241+
transaction =
242+
Sentry.Transaction.new(%{
243+
transaction: "test-transaction",
244+
start_timestamp: System.system_time(:second),
245+
timestamp: System.system_time(:second)
246+
})
247+
248+
{:ok, transaction: transaction}
249+
end
250+
251+
test "sends transaction to Sentry when configured properly", %{
252+
bypass: bypass,
253+
transaction: transaction
254+
} do
255+
Bypass.expect_once(bypass, "POST", "/api/1/envelope/", fn conn ->
256+
{:ok, body, conn} = Plug.Conn.read_body(conn)
257+
assert [{headers, transaction_body}] = decode_envelope!(body)
258+
259+
assert headers["type"] == "transaction"
260+
assert Map.has_key?(headers, "length")
261+
assert transaction_body["transaction"] == "test-transaction"
262+
263+
Plug.Conn.send_resp(conn, 200, ~s<{"id": "340"}>)
264+
end)
265+
266+
assert {:ok, "340"} = Sentry.send_transaction(transaction)
267+
end
268+
269+
test "ignores transaction when dsn is not configured", %{transaction: transaction} do
270+
put_test_config(dsn: nil, test_mode: false)
271+
272+
assert :ignored = Sentry.send_transaction(transaction)
273+
end
274+
end
238275
end

0 commit comments

Comments
 (0)