Skip to content

Commit aec18a5

Browse files
committed
refa(tests): leverage the new transaction assertion helper
1 parent 2e5b36e commit aec18a5

4 files changed

Lines changed: 112 additions & 153 deletions

File tree

test/sentry/opentelemetry/span_processor_test.exs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
489489
Process.sleep(1)
490490
end
491491

492-
assert_sentry_report(collect_sentry_transactions(ref, 1),
492+
assert_sentry_transaction(ref,
493493
contexts: %{"trace" => %{"op" => "http.server", "description" => "GET /api/users"}}
494494
)
495495
end
@@ -509,7 +509,7 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
509509
Process.sleep(1)
510510
end
511511

512-
assert_sentry_report(collect_sentry_transactions(ref, 1),
512+
assert_sentry_transaction(ref,
513513
contexts: %{"trace" => %{"op" => "http.server", "description" => "GET"}}
514514
)
515515
end
@@ -530,7 +530,7 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
530530
Process.sleep(1)
531531
end
532532

533-
assert_sentry_report(collect_sentry_transactions(ref, 1),
533+
assert_sentry_transaction(ref,
534534
contexts: %{"trace" => %{"op" => "http.client", "description" => "GET /external/api"}}
535535
)
536536
end
@@ -552,7 +552,7 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
552552
Process.sleep(1)
553553
end
554554

555-
assert_sentry_report(collect_sentry_transactions(ref, 1),
555+
assert_sentry_transaction(ref,
556556
contexts: %{
557557
"trace" => %{
558558
"op" => "http.server",
@@ -578,7 +578,7 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
578578
Process.sleep(1)
579579
end
580580

581-
assert_sentry_report(collect_sentry_transactions(ref, 1),
581+
assert_sentry_transaction(ref,
582582
contexts: %{
583583
"trace" => %{"op" => "db", "description" => "SELECT * FROM users WHERE id = $1"}
584584
}
@@ -600,7 +600,7 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
600600
Process.sleep(1)
601601
end
602602

603-
assert_sentry_report(collect_sentry_transactions(ref, 1),
603+
assert_sentry_transaction(ref,
604604
contexts: %{"trace" => %{"op" => "db", "description" => nil}}
605605
)
606606
end
@@ -621,7 +621,7 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
621621
Process.sleep(1)
622622
end
623623

624-
assert_sentry_report(collect_sentry_transactions(ref, 1),
624+
assert_sentry_transaction(ref,
625625
contexts: %{
626626
"trace" => %{"op" => "queue.process", "description" => "MyApp.Workers.EmailWorker"}
627627
},
@@ -639,7 +639,7 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
639639
Process.sleep(1)
640640
end
641641

642-
assert_sentry_report(collect_sentry_transactions(ref, 1),
642+
assert_sentry_transaction(ref,
643643
contexts: %{"trace" => %{"op" => "custom_operation", "description" => "custom_operation"}}
644644
)
645645
end

test_integrations/phoenix_app/test/phoenix_app/repo_test.exs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,36 @@ defmodule PhoenixApp.RepoTest do
44
alias PhoenixApp.{Repo, Accounts.User}
55

66
import Sentry.TestHelpers
7+
import Sentry.Test.Assertions
78

89
setup do
9-
setup_bypass(traces_sample_rate: 1.0)
10+
Sentry.Test.setup_sentry(collect_envelopes: true, traces_sample_rate: 1.0)
1011
end
1112

12-
test "instrumented top-level ecto transaction span", %{bypass: bypass} do
13-
ref = setup_bypass_envelope_collector(bypass)
14-
13+
test "instrumented top-level ecto transaction span", %{ref: ref} do
1514
Repo.all(User) |> Enum.map(& &1.id)
1615

17-
assert [tx] = collect_sentry_transactions(ref, 1)
18-
19-
assert tx["transaction_info"] == %{"source" => "custom"}
20-
21-
assert tx["contexts"]["trace"]["op"] == "db"
22-
23-
assert tx["contexts"]["trace"]["data"]["db.system"] == "sqlite"
24-
assert tx["contexts"]["trace"]["data"]["db.type"] == "sql"
25-
assert tx["contexts"]["trace"]["data"]["db.instance"] == "db/test.sqlite3"
26-
assert tx["contexts"]["trace"]["data"]["db.name"] == "db/test.sqlite3"
27-
28-
assert String.starts_with?(tx["contexts"]["trace"]["description"], "SELECT")
29-
assert String.starts_with?(tx["contexts"]["trace"]["data"]["db.statement"], "SELECT")
30-
31-
refute Map.has_key?(tx["contexts"]["trace"]["data"], "db.url")
16+
tx =
17+
assert_sentry_transaction(ref,
18+
transaction_info: %{"source" => "custom"},
19+
contexts: %{
20+
trace: %{
21+
op: "db",
22+
data: %{
23+
"db.system" => "sqlite",
24+
"db.type" => "sql",
25+
"db.instance" => "db/test.sqlite3",
26+
"db.name" => "db/test.sqlite3"
27+
}
28+
}
29+
}
30+
)
31+
32+
trace = tx["contexts"]["trace"]
33+
34+
assert String.starts_with?(trace["description"], "SELECT")
35+
assert String.starts_with?(trace["data"]["db.statement"], "SELECT")
36+
37+
refute Map.has_key?(trace["data"], "db.url")
3238
end
3339
end

test_integrations/phoenix_app/test/phoenix_app_web/controllers/transaction_test.exs

Lines changed: 64 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -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
239195
end

0 commit comments

Comments
 (0)