Skip to content

Commit db95d53

Browse files
committed
wip - oops, add missing Transaction module
1 parent 75e9c37 commit db95d53

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

Diff for: lib/sentry/transaction.ex

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
defmodule Sentry.Transaction do
2+
@type t() :: %__MODULE__{}
3+
4+
alias Sentry.{Config, UUID}
5+
6+
@enforce_keys ~w(event_id span_id spans)a
7+
8+
defstruct [
9+
:event_id,
10+
:environment,
11+
:span_id,
12+
:transaction,
13+
:transaction_info,
14+
:contexts,
15+
:measurements,
16+
:spans,
17+
type: "transaction"
18+
]
19+
20+
def new(attrs) do
21+
struct!(
22+
__MODULE__,
23+
attrs
24+
|> Map.put(:event_id, UUID.uuid4_hex())
25+
|> Map.put(:environment, Config.environment_name())
26+
)
27+
end
28+
29+
# Used to then encode the returned map to JSON.
30+
@doc false
31+
def to_map(%__MODULE__{} = transaction) do
32+
transaction_attrs =
33+
Map.take(transaction, [
34+
:event_id,
35+
:environment,
36+
:transaction,
37+
:transaction_info,
38+
:contexts,
39+
:measurements,
40+
:type
41+
])
42+
43+
{[root_span], child_spans} = Enum.split_with(transaction.spans, &is_nil(&1.parent_span_id))
44+
45+
root_span
46+
|> Sentry.Span.to_map()
47+
|> Map.put(:spans, Enum.map(child_spans, &Sentry.Span.to_map/1))
48+
|> Map.drop([:description])
49+
|> Map.merge(transaction_attrs)
50+
end
51+
end
52+
53+
defmodule Sentry.Span do
54+
@enforce_keys ~w(span_id trace_id start_timestamp timestamp)a
55+
56+
defstruct [
57+
:trace_id,
58+
:span_id,
59+
:parent_span_id,
60+
:start_timestamp,
61+
:timestamp,
62+
:description,
63+
:op,
64+
:status,
65+
:tags,
66+
:data,
67+
:origin
68+
]
69+
70+
# Used to then encode the returned map to JSON.
71+
@doc false
72+
def to_map(%__MODULE__{} = span) do
73+
Map.from_struct(span)
74+
end
75+
end

0 commit comments

Comments
 (0)