Skip to content

Commit 8ee657f

Browse files
authored
Clean up transactions docs and code (#852)
1 parent c5dd1fc commit 8ee657f

File tree

4 files changed

+68
-41
lines changed

4 files changed

+68
-41
lines changed

lib/sentry/client.ex

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ defmodule Sentry.Client do
107107
|> Transport.encode_and_post_envelope(client, request_retries)
108108
end
109109

110+
@spec send_transaction(Transaction.t(), keyword()) ::
111+
{:ok, transaction_id :: String.t()}
112+
| {:error, ClientError.t()}
113+
| :unsampled
114+
| :excluded
110115
def send_transaction(%Transaction{} = transaction, opts \\ []) do
111116
opts = NimbleOptions.validate!(opts, Options.send_transaction_schema())
112117

@@ -190,12 +195,12 @@ defmodule Sentry.Client do
190195
"""
191196
end
192197

193-
defp maybe_call_after_send(event, result, callback) do
198+
defp maybe_call_after_send(event_or_transaction, result, callback) do
194199
message = ":after_send_event must be an anonymous function or a {module, function} tuple"
195200

196201
case callback do
197-
function when is_function(function, 2) -> function.(event, result)
198-
{module, function} -> apply(module, function, [event, result])
202+
function when is_function(function, 2) -> function.(event_or_transaction, result)
203+
{module, function} -> apply(module, function, [event_or_transaction, result])
199204
nil -> nil
200205
_ -> raise ArgumentError, message
201206
end
@@ -294,13 +299,7 @@ defmodule Sentry.Client do
294299
def render_transaction(%Transaction{} = transaction) do
295300
transaction
296301
|> Transaction.to_payload()
297-
|> Map.merge(%{
298-
platform: "elixir",
299-
sdk: %{
300-
name: "sentry.elixir",
301-
version: Application.spec(:sentry, :vsn)
302-
}
303-
})
302+
|> update_if_present(:sdk, &Map.from_struct/1)
304303
end
305304

306305
defp render_exception(%Interfaces.Exception{} = exception) do

lib/sentry/interfaces.ex

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,24 +272,26 @@ defmodule Sentry.Interfaces do
272272
@moduledoc """
273273
The struct for the **span** interface.
274274
275-
See <https://develop.sentry.dev/sdk/event-payloads/spans>.
275+
See <https://develop.sentry.dev/sdk/data-model/event-payloads/span/>.
276276
"""
277277

278278
@moduledoc since: "11.0.0"
279279

280280
@typedoc since: "11.0.0"
281281
@type t() :: %__MODULE__{
282-
trace_id: String.t(),
283-
span_id: String.t(),
284-
start_timestamp: String.t(),
285-
timestamp: String.t(),
286-
parent_span_id: String.t(),
287-
description: String.t(),
288-
op: String.t(),
289-
status: String.t(),
290-
tags: map(),
291-
data: map(),
292-
origin: String.t()
282+
trace_id: <<_::256>>,
283+
span_id: <<_::128>>,
284+
start_timestamp: String.t() | number(),
285+
timestamp: String.t() | number(),
286+
287+
# Optional
288+
parent_span_id: <<_::128>> | nil,
289+
description: String.t() | nil,
290+
op: String.t() | nil,
291+
status: String.t() | nil,
292+
tags: %{optional(String.t()) => term()} | nil,
293+
data: %{optional(String.t()) => term()} | nil,
294+
origin: String.t() | nil
293295
}
294296

295297
@enforce_keys [:trace_id, :span_id, :start_timestamp, :timestamp]
@@ -304,10 +306,5 @@ defmodule Sentry.Interfaces do
304306
:data,
305307
:origin
306308
]
307-
308-
@doc false
309-
def to_payload(%__MODULE__{} = span) do
310-
Map.from_struct(span)
311-
end
312309
end
313310
end

lib/sentry/transaction.ex

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,42 @@ defmodule Sentry.Transaction do
22
@moduledoc """
33
The struct for the **transaction** interface.
44
5-
See <https://develop.sentry.dev/sdk/event-payloads/transactions>.
5+
See <https://develop.sentry.dev/sdk/data-model/event-payloads/transaction/>.
66
"""
77

88
@moduledoc since: "11.0.0"
99

10-
alias Sentry.{Config, UUID, Interfaces.Span, Interfaces.SDK}
10+
alias Sentry.{Config, UUID, Interfaces, Interfaces.Span, Interfaces.SDK}
11+
12+
@sdk %Interfaces.SDK{
13+
name: "sentry-elixir",
14+
version: Mix.Project.config()[:version]
15+
}
16+
17+
@typedoc """
18+
A map of measurements.
19+
20+
See
21+
[here](https://github.com/getsentry/sentry/blob/a8c960a933d2ded5225841573d8fc426a482ca9c/static/app/utils/discover/fields.tsx#L654-L676)
22+
for the list of supported keys (which could change in the future).
23+
"""
24+
@typedoc since: "11.0.0"
25+
@type measurements() :: %{
26+
optional(key :: atom()) => %{
27+
required(:value) => number(),
28+
optional(:unit) => String.t()
29+
}
30+
}
31+
32+
@typedoc """
33+
Transaction information.
34+
35+
Should only be set by integrations and not developers directly.
36+
"""
37+
@typedoc since: "11.0.0"
38+
@type transaction_info() :: %{
39+
required(:source) => String.t()
40+
}
1141

1242
@typedoc since: "11.0.0"
1343
@type t() ::
@@ -16,7 +46,8 @@ defmodule Sentry.Transaction do
1646
event_id: UUID.t(),
1747
start_timestamp: String.t() | number(),
1848
timestamp: String.t() | number(),
19-
platform: :elixir,
49+
platform: String.t(),
50+
# See https://develop.sentry.dev/sdk/data-model/event-payloads/contexts/#trace-context
2051
contexts: %{
2152
required(:trace) => %{
2253
required(:trace_id) => String.t(),
@@ -31,9 +62,8 @@ defmodule Sentry.Transaction do
3162
# Optional
3263
environment: String.t(),
3364
transaction: String.t(),
34-
transaction_info: map(),
35-
measurements: map(),
36-
type: String.t(),
65+
transaction_info: transaction_info(),
66+
measurements: measurements(),
3767
tags: map(),
3868
data: map(),
3969

@@ -55,8 +85,7 @@ defmodule Sentry.Transaction do
5585
:platform,
5686
:environment,
5787
:tags,
58-
:data,
59-
type: "transaction"
88+
:data
6089
]
6190

6291
@doc false
@@ -66,13 +95,16 @@ defmodule Sentry.Transaction do
6695
attrs
6796
|> Map.put(:event_id, UUID.uuid4_hex())
6897
|> Map.put(:environment, Config.environment_name())
98+
|> Map.put(:sdk, @sdk)
99+
|> Map.put(:platform, "elixir")
69100
)
70101
end
71102

72103
@doc false
73104
def to_payload(%__MODULE__{} = transaction) do
74105
transaction
75106
|> Map.from_struct()
76-
|> Map.update(:spans, [], fn spans -> Enum.map(spans, &Span.to_payload/1) end)
107+
|> Map.put(:type, "transaction")
108+
|> update_in([Access.key(:spans, []), Access.all()], &Map.from_struct/1)
77109
end
78110
end

test/sentry/transaction_test.exs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ defmodule Sentry.TransactionTest do
66
import Sentry.TestHelpers
77

88
describe "to_payload/1" do
9-
setup do
10-
{:ok, %{transaction: create_transaction()}}
11-
end
12-
13-
test "returns a map representation of the transaction", %{transaction: transaction} do
9+
test "returns a map representation of the transaction" do
10+
transaction = create_transaction()
1411
transaction_payload = Transaction.to_payload(transaction)
1512

13+
assert transaction_payload.type == "transaction"
14+
1615
[child_span] = transaction_payload[:spans]
1716

1817
assert transaction_payload[:contexts][:trace][:trace_id] == "trace-312"

0 commit comments

Comments
 (0)