Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cbf325f

Browse files
committedJan 22, 2025··
Support after_send_event option in send_transaction
1 parent d350faa commit cbf325f

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed
 

‎lib/sentry/client.ex

+6-10
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ defmodule Sentry.Client do
100100
client = Config.client()
101101

102102
# This is a "private" option, only really used in testing.
103-
request_retries =
104-
Application.get_env(:sentry, :request_retries, Transport.default_retries())
103+
request_retries = Application.get_env(:sentry, :request_retries, Transport.default_retries())
105104

106105
client_report
107106
|> Envelope.from_client_report()
@@ -115,6 +114,7 @@ defmodule Sentry.Client do
115114
client = Keyword.get_lazy(opts, :client, &Config.client/0)
116115
sample_rate = Keyword.get_lazy(opts, :sample_rate, &Config.sample_rate/0)
117116
before_send = Keyword.get_lazy(opts, :before_send, &Config.before_send/0)
117+
after_send_event = Keyword.get_lazy(opts, :after_send_event, &Config.after_send_event/0)
118118

119119
request_retries =
120120
Keyword.get_lazy(opts, :request_retries, fn ->
@@ -123,13 +123,9 @@ defmodule Sentry.Client do
123123

124124
with :ok <- sample_event(sample_rate),
125125
{:ok, %Transaction{} = transaction} <- maybe_call_before_send(transaction, before_send) do
126-
case encode_and_send(transaction, result_type, client, request_retries) do
127-
{:ok, id} ->
128-
{:ok, id}
129-
130-
{:error, %ClientError{} = error} ->
131-
{:error, error}
132-
end
126+
send_result = encode_and_send(transaction, result_type, client, request_retries)
127+
_ignored = maybe_call_after_send(transaction, send_result, after_send_event)
128+
send_result
133129
else
134130
:unsampled ->
135131
ClientReport.Sender.record_discarded_events(:sample_rate, [transaction])
@@ -194,7 +190,7 @@ defmodule Sentry.Client do
194190
"""
195191
end
196192

197-
defp maybe_call_after_send(%Event{} = event, result, callback) do
193+
defp maybe_call_after_send(event, result, callback) do
198194
message = ":after_send_event must be an anonymous function or a {module, function} tuple"
199195

200196
case callback do

‎test/sentry_test.exs

+18
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,23 @@ defmodule SentryTest do
329329
end
330330
)
331331
end
332+
333+
test "supports after_send_event option", %{bypass: bypass, transaction: transaction} do
334+
parent = self()
335+
336+
Bypass.expect_once(bypass, "POST", "/api/1/envelope/", fn conn ->
337+
Plug.Conn.send_resp(conn, 200, ~s<{"id": "340"}>)
338+
end)
339+
340+
assert {:ok, "340"} =
341+
Sentry.send_transaction(
342+
transaction,
343+
after_send_event: fn transaction, {:ok, id} ->
344+
send(parent, {:after_send, transaction.transaction, id})
345+
end
346+
)
347+
348+
assert_receive {:after_send, "test-transaction", "340"}
349+
end
332350
end
333351
end

0 commit comments

Comments
 (0)
Please sign in to comment.