@@ -3,49 +3,39 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
33
44 import Phoenix.LiveViewTest
55 import Sentry.TestHelpers
6+ import Sentry.Test.Assertions
67
78 setup do
8- setup_bypass ( traces_sample_rate: 1.0 )
9+ Sentry.Test . setup_sentry ( collect_envelopes: true , traces_sample_rate: 1.0 )
910 end
1011
11- test "GET /transaction" , % { conn: conn , bypass: bypass } do
12- ref = setup_bypass_envelope_collector ( bypass )
13-
12+ test "GET /transaction" , % { conn: conn , ref: ref } do
1413 get ( conn , ~p" /transaction" )
1514
16- transactions = collect_envelopes ( ref , 1 ) |> extract_transactions ( )
17-
18- assert length ( transactions ) == 1
19-
20- assert [ tx ] = transactions
21-
22- assert tx [ "transaction" ] == "test_span"
23- assert tx [ "transaction_info" ] == % { "source" => "custom" }
15+ tx =
16+ assert_sentry_transaction ( ref ,
17+ transaction: "test_span" ,
18+ transaction_info: % { "source" => "custom" } ,
19+ contexts: % { trace: % { origin: "phoenix_app" , op: "test_span" } }
20+ )
2421
25- trace = tx [ "contexts" ] [ "trace" ]
26- assert trace [ "origin" ] == "phoenix_app"
27- assert trace [ "op" ] == "test_span"
28- assert trace [ "data" ] == % { }
22+ assert tx [ "contexts" ] [ "trace" ] [ "data" ] == % { }
2923 end
3024
31- test "GET /users" , % { conn: conn , bypass: bypass } do
32- ref = setup_bypass_envelope_collector ( bypass )
33-
25+ test "GET /users" , % { conn: conn , ref: ref } do
3426 get ( conn , ~p" /users" )
3527
36- transactions = collect_envelopes ( ref , 2 ) |> extract_transactions ( )
37-
38- assert length ( transactions ) == 2
39-
40- assert [ mount_tx , handle_params_tx ] = transactions
28+ assert [ mount_tx , handle_params_tx ] = collect_sentry_transactions ( ref , 2 )
4129
42- assert mount_tx [ "transaction" ] == "PhoenixAppWeb.UserLive.Index.mount"
43- assert mount_tx [ "transaction_info" ] == % { "source" => "custom" }
30+ assert_sentry_report ( mount_tx ,
31+ transaction: "PhoenixAppWeb.UserLive.Index.mount" ,
32+ transaction_info: % { "source" => "custom" } ,
33+ contexts: % {
34+ trace: % { origin: "opentelemetry_phoenix" , op: "PhoenixAppWeb.UserLive.Index.mount" }
35+ }
36+ )
4437
45- trace = mount_tx [ "contexts" ] [ "trace" ]
46- assert trace [ "origin" ] == "opentelemetry_phoenix"
47- assert trace [ "op" ] == "PhoenixAppWeb.UserLive.Index.mount"
48- assert trace [ "data" ] == % { }
38+ assert mount_tx [ "contexts" ] [ "trace" ] [ "data" ] == % { }
4939
5040 assert [ span_ecto ] = mount_tx [ "spans" ]
5141
@@ -54,33 +44,29 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
5444 assert span_ecto [ "description" ] ==
5545 "SELECT u0.\" id\" , u0.\" name\" , u0.\" age\" , u0.\" inserted_at\" , u0.\" updated_at\" FROM \" users\" AS u0"
5646
57- assert handle_params_tx [ "transaction" ] ==
58- "PhoenixAppWeb.UserLive.Index.handle_params"
59-
60- assert handle_params_tx [ "transaction_info" ] == % { "source" => "custom" }
61-
62- trace = handle_params_tx [ "contexts" ] [ "trace" ]
63- assert trace [ "origin" ] == "opentelemetry_phoenix"
64- assert trace [ "op" ] == "PhoenixAppWeb.UserLive.Index.handle_params"
65- assert trace [ "data" ] == % { }
47+ assert_sentry_report ( handle_params_tx ,
48+ transaction: "PhoenixAppWeb.UserLive.Index.handle_params" ,
49+ transaction_info: % { "source" => "custom" } ,
50+ contexts: % {
51+ trace: % {
52+ origin: "opentelemetry_phoenix" ,
53+ op: "PhoenixAppWeb.UserLive.Index.handle_params"
54+ }
55+ }
56+ )
57+
58+ assert handle_params_tx [ "contexts" ] [ "trace" ] [ "data" ] == % { }
6659 end
6760
68- test "GET /nested-spans includes grand-child spans" , % { conn: conn , bypass: bypass } do
69- ref = setup_bypass_envelope_collector ( bypass )
70-
61+ test "GET /nested-spans includes grand-child spans" , % { conn: conn , ref: ref } do
7162 get ( conn , ~p" /nested-spans" )
7263
73- transactions = collect_envelopes ( ref , 1 ) |> extract_transactions ( )
74-
75- assert length ( transactions ) == 1
76- assert [ tx ] = transactions
77-
78- assert tx [ "transaction" ] == "root_span"
79- assert tx [ "transaction_info" ] == % { "source" => "custom" }
80-
81- trace = tx [ "contexts" ] [ "trace" ]
82- assert trace [ "origin" ] == "phoenix_app"
83- assert trace [ "op" ] == "root_span"
64+ tx =
65+ assert_sentry_transaction ( ref ,
66+ transaction: "root_span" ,
67+ transaction_info: % { "source" => "custom" } ,
68+ contexts: % { trace: % { origin: "phoenix_app" , op: "root_span" } }
69+ )
8470
8571 assert length ( tx [ "spans" ] ) == 6
8672
@@ -115,16 +101,11 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
115101
116102 test "LiveView mount and handle_params create disconnected transactions with child spans" , % {
117103 conn: conn ,
118- bypass: bypass
104+ ref: ref
119105 } do
120- ref = setup_bypass_envelope_collector ( bypass )
121-
122106 get ( conn , ~p" /users" )
123107
124- transactions = collect_envelopes ( ref , 2 ) |> extract_transactions ( )
125-
126- assert length ( transactions ) == 2
127- assert [ mount_tx , handle_params_tx ] = transactions
108+ assert [ mount_tx , handle_params_tx ] = collect_sentry_transactions ( ref , 2 )
128109
129110 assert mount_tx [ "transaction" ] == "PhoenixAppWeb.UserLive.Index.mount"
130111 assert length ( mount_tx [ "spans" ] ) == 1
@@ -145,10 +126,8 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
145126 describe "distributed tracing with sentry-trace header" do
146127 test "LiveView mount inherits trace context from sentry-trace header" , % {
147128 conn: conn ,
148- bypass: bypass
129+ ref: ref
149130 } do
150- ref = setup_bypass_envelope_collector ( bypass )
151-
152131 trace_id = "1234567890abcdef1234567890abcdef"
153132 parent_span_id = "abcdef1234567890"
154133
@@ -158,34 +137,23 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
158137
159138 get ( conn , ~p" /users" )
160139
161- transactions = collect_envelopes ( ref , 2 ) |> extract_transactions ( )
162-
163- mount_tx =
164- Enum . find ( transactions , fn t ->
165- t [ "transaction" ] == "PhoenixAppWeb.UserLive.Index.mount"
166- end )
140+ transactions = collect_sentry_transactions ( ref , 2 )
167141
168- handle_params_tx =
169- Enum . find ( transactions , fn t ->
170- t [ "transaction" ] == "PhoenixAppWeb.UserLive.Index.handle_params"
171- end )
142+ find_sentry_report! ( transactions ,
143+ transaction: "PhoenixAppWeb.UserLive.Index.mount" ,
144+ contexts: % { trace: % { trace_id: trace_id , parent_span_id: parent_span_id } }
145+ )
172146
173- assert mount_tx != nil
174- assert handle_params_tx != nil
175-
176- assert mount_tx [ "contexts" ] [ "trace" ] [ "trace_id" ] == trace_id
177- assert handle_params_tx [ "contexts" ] [ "trace" ] [ "trace_id" ] == trace_id
178-
179- assert mount_tx [ "contexts" ] [ "trace" ] [ "parent_span_id" ] == parent_span_id
180- assert handle_params_tx [ "contexts" ] [ "trace" ] [ "parent_span_id" ] == parent_span_id
147+ find_sentry_report! ( transactions ,
148+ transaction: "PhoenixAppWeb.UserLive.Index.handle_params" ,
149+ contexts: % { trace: % { trace_id: trace_id , parent_span_id: parent_span_id } }
150+ )
181151 end
182152
183153 test "LiveView handle_event in WebSocket shares trace context with initial request" , % {
184154 conn: conn ,
185- bypass: bypass
155+ ref: ref
186156 } do
187- ref = setup_bypass_envelope_collector ( bypass )
188-
189157 trace_id = "fedcba0987654321fedcba0987654321"
190158 parent_span_id = "1234567890fedcba"
191159
@@ -197,23 +165,15 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
197165
198166 view |> element ( "#increment-btn" ) |> render_click ( )
199167
200- transactions = collect_envelopes ( ref , 5 , timeout: 2000 ) |> extract_transactions ( )
201-
202- handle_event_tx =
203- Enum . find ( transactions , fn t ->
204- String . contains? ( t [ "transaction" ] , "handle_event#increment" )
205- end )
206-
207- assert handle_event_tx != nil ,
208- "Expected handle_event transaction, got: #{ inspect ( Enum . map ( transactions , & & 1 [ "transaction" ] ) ) } "
209-
210- assert handle_event_tx [ "contexts" ] [ "trace" ] [ "trace_id" ] == trace_id ,
211- "Expected trace_id #{ trace_id } , got #{ handle_event_tx [ "contexts" ] [ "trace" ] [ "trace_id" ] } "
168+ find_sentry_transaction! ( ref ,
169+ count: 5 ,
170+ timeout: 2000 ,
171+ transaction: ~r/ handle_event#increment/ ,
172+ contexts: % { trace: % { trace_id: trace_id } }
173+ )
212174 end
213175
214- test "baggage header is preserved through LiveView lifecycle" , % { conn: conn , bypass: bypass } do
215- ref = setup_bypass_envelope_collector ( bypass )
216-
176+ test "baggage header is preserved through LiveView lifecycle" , % { conn: conn , ref: ref } do
217177 trace_id = "abababababababababababababababab"
218178 parent_span_id = "cdcdcdcdcdcdcdcd"
219179 baggage = "sentry-environment=production,sentry-release=1.0.0"
@@ -225,15 +185,11 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
225185
226186 get ( conn , ~p" /users" )
227187
228- transactions = collect_envelopes ( ref , 2 ) |> extract_transactions ( )
229-
230- mount_tx =
231- Enum . find ( transactions , fn t ->
232- t [ "transaction" ] == "PhoenixAppWeb.UserLive.Index.mount"
233- end )
234-
235- assert mount_tx != nil
236- assert mount_tx [ "contexts" ] [ "trace" ] [ "trace_id" ] == trace_id
188+ find_sentry_transaction! ( ref ,
189+ count: 2 ,
190+ transaction: "PhoenixAppWeb.UserLive.Index.mount" ,
191+ contexts: % { trace: % { trace_id: trace_id } }
192+ )
237193 end
238194 end
239195end
0 commit comments