neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
mix vigil.test --kind transaction
ea450b1 · gmorell · 2026-12-02 02:38
Files changed
modified
lib/mix/tasks/vigil.test.ex
+92
−9
@@ -5,6 +5,7 @@ defmodule Mix.Tasks.Vigil.Test do
| 5 | 5 | |
| 6 | 6 | mix vigil.test ORG PROJECT |
| 7 | 7 | mix vigil.test ORG PROJECT --kind error --count 5 |
| 8 | + mix vigil.test ORG PROJECT --kind transaction --count 50 | |
| 8 | 9 | mix vigil.test ORG PROJECT --unique |
| 9 | 10 | mix vigil.test ORG PROJECT --http |
| 10 | 11 | mix vigil.test ORG PROJECT --http https://vigil.example.com |
@@ -15,7 +16,10 @@ defmodule Mix.Tasks.Vigil.Test do
| 15 | 16 | so the issues are there when the task ends. Repeated runs land in the |
| 16 | 17 | same issues (their counts go up) unless `--unique` gives each run its own. |
| 17 | 18 | |
| 18 | − * `--kind` `error`, `message` or `both` (default) | |
| 19 | + * `--kind` `error`, `message`, `both` (default), or `transaction`: | |
| 20 | + traced requests to three endpoints, each with database | |
| 21 | + spans and varied durations (a few fail), as sentry-elixir | |
| 22 | + sends them, for the performance pages | |
| 19 | 23 | * `--count` how many of each (default 1) |
| 20 | 24 | * `--unique` a fingerprint per run, so each run makes new issues |
| 21 | 25 | * `--http` POST the envelope to a running server instead, with the |
@@ -57,7 +61,7 @@ defmodule Mix.Tasks.Vigil.Test do
| 57 | 61 | |
| 58 | 62 | _ -> |
| 59 | 63 | Mix.raise( |
| 60 | − "Usage: mix vigil.test ORG PROJECT [--kind error|message|both] [--count N] [--unique] [--http [URL]]" | |
| 64 | + "Usage: mix vigil.test ORG PROJECT [--kind error|message|both|transaction] [--count N] [--unique] [--http [URL]]" | |
| 61 | 65 | ) |
| 62 | 66 | end |
| 63 | 67 | end |
@@ -102,18 +106,97 @@ defmodule Mix.Tasks.Vigil.Test do
| 102 | 106 | messages = |
| 103 | 107 | if kind in ["message", "both"], do: for(i <- 1..count, do: message_event(i, run)), else: [] |
| 104 | 108 | |
| 109 | + transactions = | |
| 110 | + if kind == "transaction", do: for(i <- 1..count, do: transaction_event(i)), else: [] | |
| 111 | + | |
| 105 | 112 | items = |
| 106 | − for event <- errors ++ messages do | |
| 107 | − payload = Jason.encode!(event) | |
| 113 | + for event <- errors ++ messages ++ transactions do | |
| 114 | + type = event["type"] || "event" | |
| 108 | 115 | |
| 109 | 116 | %Envelope.Item{ |
| 110 | − type: "event", | |
| 111 | − headers: %{"type" => "event", "event_id" => event["event_id"]}, | |
| 112 | − payload: payload | |
| 117 | + type: type, | |
| 118 | + headers: %{"type" => type, "event_id" => event["event_id"]}, | |
| 119 | + payload: Jason.encode!(event) | |
| 120 | + } | |
| 121 | + end | |
| 122 | + | |
| 123 | + case items do | |
| 124 | + [] -> Mix.raise("Unknown --kind #{kind}") | |
| 125 | + [first | _] -> %{headers: %{"event_id" => first.headers["event_id"]}, items: items} | |
| 126 | + end | |
| 127 | + end | |
| 128 | + | |
| 129 | + @routes [ | |
| 130 | + {"GET", "/users/:id", "SELECT * FROM users WHERE id = $1", 40}, | |
| 131 | + {"POST", "/orders", "INSERT INTO orders (user_id, total) VALUES ($1, $2)", 180}, | |
| 132 | + {"GET", "/reports/:id", "SELECT * FROM report_rows WHERE report_id = $1", 900} | |
| 133 | + ] | |
| 134 | + | |
| 135 | + # A request as sentry-elixir's OpenTelemetry integration sends it: the | |
| 136 | + # transaction is the server span, with a controller span and db spans. | |
| 137 | + defp transaction_event(i) do | |
| 138 | + {method, route, sql, typical} = Enum.at(@routes, rem(i - 1, length(@routes))) | |
| 139 | + total = round(typical * (0.5 + :rand.uniform() * if(rem(i, 17) == 0, do: 6, else: 1.2))) | |
| 140 | + start = DateTime.add(DateTime.utc_now(), -total - :rand.uniform(5_000), :millisecond) | |
| 141 | + | |
| 142 | + at = fn ms -> | |
| 143 | + start |> DateTime.add(round(ms * 1000), :microsecond) |> DateTime.to_iso8601() | |
| 144 | + end | |
| 145 | + | |
| 146 | + hex = fn n -> :crypto.strong_rand_bytes(n) |> Base.encode16(case: :lower) end | |
| 147 | + trace_id = hex.(16) | |
| 148 | + root = hex.(8) | |
| 149 | + controller = hex.(8) | |
| 150 | + queries = 1 + rem(i, 3) | |
| 151 | + query_ms = total * 0.6 / queries | |
| 152 | + | |
| 153 | + db_spans = | |
| 154 | + for q <- 0..(queries - 1) do | |
| 155 | + from = total * 0.2 + q * query_ms | |
| 156 | + | |
| 157 | + %{ | |
| 158 | + "span_id" => hex.(8), | |
| 159 | + "parent_span_id" => controller, | |
| 160 | + "trace_id" => trace_id, | |
| 161 | + "op" => "db", | |
| 162 | + "description" => sql, | |
| 163 | + "start_timestamp" => at.(from), | |
| 164 | + "timestamp" => at.(from + query_ms * 0.9), | |
| 165 | + "data" => %{"db.system" => "postgresql"} | |
| 113 | 166 | } |
| 114 | 167 | end |
| 115 | 168 | |
| 116 | − %{headers: %{"event_id" => hd(errors ++ messages)["event_id"]}, items: items} | |
| 169 | + %{ | |
| 170 | + "type" => "transaction", | |
| 171 | + "event_id" => hex.(16), | |
| 172 | + "transaction" => "#{method} #{route}", | |
| 173 | + "start_timestamp" => at.(0), | |
| 174 | + "timestamp" => at.(total), | |
| 175 | + "platform" => "elixir", | |
| 176 | + "environment" => "test", | |
| 177 | + "release" => "vigil-test@1.0.0", | |
| 178 | + "request" => %{"method" => method, "url" => "http://localhost" <> route}, | |
| 179 | + "contexts" => %{ | |
| 180 | + "trace" => %{ | |
| 181 | + "trace_id" => trace_id, | |
| 182 | + "span_id" => root, | |
| 183 | + "op" => "http.server", | |
| 184 | + "status" => if(rem(i, 11) == 0, do: "internal_error", else: "ok") | |
| 185 | + } | |
| 186 | + }, | |
| 187 | + "spans" => [ | |
| 188 | + %{ | |
| 189 | + "span_id" => controller, | |
| 190 | + "parent_span_id" => root, | |
| 191 | + "trace_id" => trace_id, | |
| 192 | + "op" => "phoenix.controller", | |
| 193 | + "description" => "#{route} controller", | |
| 194 | + "start_timestamp" => at.(total * 0.1), | |
| 195 | + "timestamp" => at.(total * 0.95) | |
| 196 | + } | |
| 197 | + | db_spans | |
| 198 | + ] | |
| 199 | + } | |
| 117 | 200 | end |
| 118 | 201 | |
| 119 | 202 | defp base_event(level, run) do |
@@ -194,7 +277,7 @@ defmodule Mix.Tasks.Vigil.Test do
| 194 | 277 | drain_all(project.id) |
| 195 | 278 | |
| 196 | 279 | Mix.shell().info( |
| 197 | − "Sent #{kept} event(s) to #{org.slug}/#{project.slug}, and processed them." | |
| 280 | + "Sent #{kept} item(s) to #{org.slug}/#{project.slug}, and processed them." | |
| 198 | 281 | ) |
| 199 | 282 | |
| 200 | 283 | Mix.shell().info(VigilWeb.base_url() <> "/orgs/#{org.slug}/projects/#{project.slug}") |
modified
test/mix/vigil_test_task_test.exs
+14
−1
@@ -25,7 +25,7 @@ defmodule Mix.Tasks.Vigil.TestTaskTest do
| 25 | 25 | project: project |
| 26 | 26 | } do |
| 27 | 27 | out = capture_io(fn -> Mix.Tasks.Vigil.Test.run([org.slug, "web"]) end) |
| 28 | − assert out =~ "Sent 2 event(s)" | |
| 28 | + assert out =~ "Sent 2 item(s)" | |
| 29 | 29 | assert [%{type: :error, count: 1}, %{type: :default, count: 1}] = issues(project) |
| 30 | 30 | |
| 31 | 31 | capture_io(fn -> |
@@ -35,6 +35,19 @@ defmodule Mix.Tasks.Vigil.TestTaskTest do
| 35 | 35 | assert [%{type: :error, count: 4}, %{count: 1}] = issues(project) |
| 36 | 36 | end |
| 37 | 37 | |
| 38 | + test "--kind transaction sends traced requests to three endpoints", %{org: org, project: project} do | |
| 39 | + out = | |
| 40 | + capture_io(fn -> | |
| 41 | + Mix.Tasks.Vigil.Test.run([org.slug, "web", "--kind", "transaction", "--count", "6"]) | |
| 42 | + end) | |
| 43 | + | |
| 44 | + assert out =~ "Sent 6 item(s)" | |
| 45 | + groups = Vigil.Performance.list_groups(project.id) | |
| 46 | + assert groups |> Enum.map(& &1.stats.count) |> Enum.sort() == [2, 2, 2] | |
| 47 | + assert Enum.all?(groups, &(&1.op == "http.server")) | |
| 48 | + assert Repo.aggregate(Vigil.Performance.Span, :count) > 6 | |
| 49 | + end | |
| 50 | + | |
| 38 | 51 | test "--unique makes new issues each run", %{org: org, project: project} do |
| 39 | 52 | capture_io(fn -> |
| 40 | 53 | Mix.Tasks.Vigil.Test.run([org.slug, "web", "--kind", "error", "--unique"]) |
Parents: e9331a4