neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Slice 2: events into issues; mix vigil.test
1a4b866 · gmorell · 2026-12-01 22:22
Message
{commit_body(@commit)}
Files changed
modified
.gitignore
+3
−0
| 39 | 39 | # Rust NIF build output (native/*/target) and the built libraries. |
| 40 | 40 | /native/*/target/ |
| 41 | 41 | /priv/native/ |
| 42 | + | |
| 43 | +# Claude Code worktrees for background agents. | |
| 44 | +/.claude/worktrees/ |
modified
ROADMAP.md
+8
−1
| 232 | 232 | - **DSNs carry `projects.dsn_id`, an integer.** sentry-python (`int(path.pop())`) and sentry-elixir (`Integer.parse/1`) refuse anything else in that position, so this is the one id that isn't a UUIDv7; the primary key still is. |
| 233 | 233 | - The drain claims a batch in one statement, then processes each item in its own transaction, so one bad item can't roll back the rest. |
| 234 | 234 | |
| 235 | −### 2. Events into issues · large | |
| 235 | +### 2. Events into issues · large · **done** | |
| 236 | 236 | - **Normalisation:** |
| 237 | 237 | - Tolerant per field, as GlitchTip: a bad interface becomes `nil` plus an entry in `errors[]`, never a rejection. |
| 238 | 238 | - Bare arrays and `values` are unified, and `logentry` is formatted. |
| 267 | 267 | - **Search:** a tsvector over title, culprit, transaction and frame filenames, plus a trigram index on title. |
| 268 | 268 | - **User feedback:** `user_report` and `feedback` items are linked to the issue by `event_id`. |
| 269 | 269 | - **Exit:** errors from both SDKs group sensibly and survive regression and resolve cycles. |
| 270 | +- **As built:** | |
| 271 | + - Grouping is a port of 2019 Sentry's `newstyle:2019-10-29` and `legacy:2019-03-12`, which matches all 200 of Sentry's own grouping snapshot fixtures (100 inputs × 2 configs, whole variant trees, not just hashes), plus `glitchtip:compat`. | |
| 272 | + - Stack-trace enhancement rules are Sentry's own rule files, parsed at compile time. Projects can add rule text of their own (`Vigil.Grouping` opts). | |
| 273 | + - Sentry's Rust glob matcher was rebuilt from its tests; no fixture exercises the bracket cases. | |
| 274 | + - The user-agent NIF is `woothee`, with no rules file to ship. | |
| 275 | + - Real sentry-python and sentry-elixir events go all the way into issues in `VigilWeb.SdkConformanceTest`. | |
| 276 | + - `mix vigil.test ORG PROJECT` sends test events. | |
| 270 | 277 | |
| 271 | 278 | ### 3. Issue UI · large |
| 272 | 279 | - **Project issue list:** |
modified
config/config.exs
+10
−0
| 53 | 53 | ]} |
| 54 | 54 | ] |
| 55 | 55 | |
| 56 | +# The partitioned tables (Vigil.Partitions): how far ahead partitions are | |
| 57 | +# made, and how long data is kept (days). | |
| 58 | +config :vigil, Vigil.Partitions, | |
| 59 | + tables: [ | |
| 60 | + %{table: "issue_events", kind: :uuid7_daily, ahead_days: 3, keep_days: 90}, | |
| 61 | + %{table: "issue_tags", kind: :weekly, ahead_days: 21, keep_days: 90}, | |
| 62 | + %{table: "issue_counts", kind: :weekly, ahead_days: 21, keep_days: 90}, | |
| 63 | + %{table: "project_counts", kind: :weekly, ahead_days: 21, keep_days: 400} | |
| 64 | + ] | |
| 65 | + | |
| 56 | 66 | # Configure the endpoint |
| 57 | 67 | config :vigil, VigilWeb.Endpoint, |
| 58 | 68 | url: [host: "localhost"], |
added
lib/mix/tasks/vigil.test.ex
+249
−0
| 1 | +defmodule Mix.Tasks.Vigil.Test do | |
| 2 | + @shortdoc "Send a test error and message to a project" | |
| 3 | + @moduledoc """ | |
| 4 | + Sends test events to a project, to see it receive them: | |
| 5 | + | |
| 6 | + mix vigil.test ORG PROJECT | |
| 7 | + mix vigil.test ORG PROJECT --kind error --count 5 | |
| 8 | + mix vigil.test ORG PROJECT --unique | |
| 9 | + mix vigil.test ORG PROJECT --http | |
| 10 | + mix vigil.test ORG PROJECT --http https://vigil.example.com | |
| 11 | + | |
| 12 | + `ORG` and `PROJECT` are slugs. By default an error (`Vigil.TestError`, | |
| 13 | + with a stack trace) and a message are sent, through the same pipeline | |
| 14 | + SDKs' envelopes take (`Vigil.Ingest.accept/3`), and processed right away, | |
| 15 | + so the issues are there when the task ends. Repeated runs land in the | |
| 16 | + same issues (their counts go up) unless `--unique` gives each run its own. | |
| 17 | + | |
| 18 | + * `--kind` `error`, `message` or `both` (default) | |
| 19 | + * `--count` how many of each (default 1) | |
| 20 | + * `--unique` a fingerprint per run, so each run makes new issues | |
| 21 | + * `--http` POST the envelope to a running server instead, with the | |
| 22 | + project's DSN, through the whole ingest edge (default URL: | |
| 23 | + the endpoint's configured one); the server processes it | |
| 24 | + in the background | |
| 25 | + """ | |
| 26 | + | |
| 27 | + use Mix.Task | |
| 28 | + | |
| 29 | + import Ecto.Query | |
| 30 | + | |
| 31 | + alias Vigil.Ingest.{Drain, Envelope, Keys} | |
| 32 | + alias Vigil.{Organizations, Projects, Repo} | |
| 33 | + alias Vigil.Projects.Project | |
| 34 | + | |
| 35 | + @requirements ["app.start"] | |
| 36 | + | |
| 37 | + @impl Mix.Task | |
| 38 | + def run(args) do | |
| 39 | + {opts, rest, _} = | |
| 40 | + OptionParser.parse(args, | |
| 41 | + strict: [kind: :string, count: :integer, unique: :boolean, http: :boolean, url: :string], | |
| 42 | + aliases: [k: :kind, n: :count] | |
| 43 | + ) | |
| 44 | + | |
| 45 | + {http?, url, rest} = http_option(opts, rest) | |
| 46 | + | |
| 47 | + with [org_slug, project_slug] <- rest, | |
| 48 | + {:ok, org, project, key} <- find(org_slug, project_slug) do | |
| 49 | + envelope = envelope(opts) | |
| 50 | + | |
| 51 | + if http?, | |
| 52 | + do: send_http(envelope, project, key, url), | |
| 53 | + else: send_local(envelope, org, project, key) | |
| 54 | + else | |
| 55 | + {:error, message} -> | |
| 56 | + Mix.raise(message) | |
| 57 | + | |
| 58 | + _ -> | |
| 59 | + Mix.raise( | |
| 60 | + "Usage: mix vigil.test ORG PROJECT [--kind error|message|both] [--count N] [--unique] [--http [URL]]" | |
| 61 | + ) | |
| 62 | + end | |
| 63 | + end | |
| 64 | + | |
| 65 | + # `--http` alone, or `--http URL` (OptionParser reads booleans strictly, | |
| 66 | + # so a URL after it lands in the positional arguments). | |
| 67 | + defp http_option(opts, rest) do | |
| 68 | + case {opts[:http], rest} do | |
| 69 | + {true, [a, b, url]} -> {true, url, [a, b]} | |
| 70 | + {true, _} -> {true, opts[:url] || VigilWeb.base_url(), rest} | |
| 71 | + _ -> {false, nil, rest} | |
| 72 | + end | |
| 73 | + end | |
| 74 | + | |
| 75 | + defp find(org_slug, project_slug) do | |
| 76 | + with %{} = org <- | |
| 77 | + Organizations.get_organization_by_slug(org_slug) || | |
| 78 | + {:error, "No organization #{org_slug}"}, | |
| 79 | + %Project{} = project <- | |
| 80 | + Repo.one( | |
| 81 | + from p in Project, | |
| 82 | + where: | |
| 83 | + p.organization_id == ^org.id and p.slug == ^project_slug and is_nil(p.deleted_at) | |
| 84 | + ) || {:error, "No project #{project_slug} in #{org_slug}"}, | |
| 85 | + %{} = key <- | |
| 86 | + Enum.find(Projects.list_keys(project), & &1.active) || | |
| 87 | + {:error, "#{org_slug}/#{project_slug} has no active key"} do | |
| 88 | + {:ok, org, project, key} | |
| 89 | + end | |
| 90 | + end | |
| 91 | + | |
| 92 | + ## The events | |
| 93 | + | |
| 94 | + defp envelope(opts) do | |
| 95 | + count = max(opts[:count] || 1, 1) | |
| 96 | + kind = opts[:kind] || "both" | |
| 97 | + run = if opts[:unique], do: System.unique_integer([:positive]) | |
| 98 | + | |
| 99 | + errors = | |
| 100 | + if kind in ["error", "both"], do: for(i <- 1..count, do: error_event(i, run)), else: [] | |
| 101 | + | |
| 102 | + messages = | |
| 103 | + if kind in ["message", "both"], do: for(i <- 1..count, do: message_event(i, run)), else: [] | |
| 104 | + | |
| 105 | + items = | |
| 106 | + for event <- errors ++ messages do | |
| 107 | + payload = Jason.encode!(event) | |
| 108 | + | |
| 109 | + %Envelope.Item{ | |
| 110 | + type: "event", | |
| 111 | + headers: %{"type" => "event", "event_id" => event["event_id"]}, | |
| 112 | + payload: payload | |
| 113 | + } | |
| 114 | + end | |
| 115 | + | |
| 116 | + %{headers: %{"event_id" => hd(errors ++ messages)["event_id"]}, items: items} | |
| 117 | + end | |
| 118 | + | |
| 119 | + defp base_event(level, run) do | |
| 120 | + %{ | |
| 121 | + "event_id" => Vigil.Schema.uuid7() |> String.replace("-", ""), | |
| 122 | + "timestamp" => DateTime.utc_now() |> DateTime.to_iso8601(), | |
| 123 | + "platform" => "elixir", | |
| 124 | + "level" => level, | |
| 125 | + "environment" => "test", | |
| 126 | + "release" => "vigil-test@1.0.0", | |
| 127 | + "server_name" => node() |> to_string(), | |
| 128 | + "logger" => "mix vigil.test", | |
| 129 | + "sdk" => %{"name" => "vigil.test", "version" => to_string(Application.spec(:vigil, :vsn))}, | |
| 130 | + "tags" => %{"source" => "mix vigil.test"} | |
| 131 | + } | |
| 132 | + |> then(&if(run, do: Map.put(&1, "fingerprint", ["{{ default }}", "run-#{run}"]), else: &1)) | |
| 133 | + end | |
| 134 | + | |
| 135 | + defp error_event(i, run) do | |
| 136 | + base_event("error", run) | |
| 137 | + |> Map.merge(%{ | |
| 138 | + "exception" => %{ | |
| 139 | + "values" => [ | |
| 140 | + %{ | |
| 141 | + "type" => "Vigil.TestError", | |
| 142 | + "value" => "This is a test error from mix vigil.test", | |
| 143 | + "module" => "Vigil", | |
| 144 | + "mechanism" => %{"type" => "generic", "handled" => true}, | |
| 145 | + "stacktrace" => %{ | |
| 146 | + "frames" => [ | |
| 147 | + frame("Mix.Task", "run/2", "lib/mix/task.ex", 501, false), | |
| 148 | + frame("Mix.Tasks.Vigil.Test", "run/1", "lib/mix/tasks/vigil.test.ex", 52, true), | |
| 149 | + frame( | |
| 150 | + "Mix.Tasks.Vigil.Test", | |
| 151 | + "error_event/2", | |
| 152 | + "lib/mix/tasks/vigil.test.ex", | |
| 153 | + 120, | |
| 154 | + true | |
| 155 | + ) | |
| 156 | + ] | |
| 157 | + } | |
| 158 | + } | |
| 159 | + ] | |
| 160 | + }, | |
| 161 | + "extra" => %{"sequence" => i} | |
| 162 | + }) | |
| 163 | + end | |
| 164 | + | |
| 165 | + defp message_event(i, run) do | |
| 166 | + base_event("info", run) | |
| 167 | + |> Map.merge(%{ | |
| 168 | + "message" => %{"formatted" => "This is a test message from mix vigil.test"}, | |
| 169 | + "extra" => %{"sequence" => i} | |
| 170 | + }) | |
| 171 | + end | |
| 172 | + | |
| 173 | + defp frame(module, function, file, line, in_app) do | |
| 174 | + %{ | |
| 175 | + "module" => module, | |
| 176 | + "function" => "#{module}.#{function}", | |
| 177 | + "filename" => file, | |
| 178 | + "lineno" => line, | |
| 179 | + "in_app" => in_app | |
| 180 | + } | |
| 181 | + end | |
| 182 | + | |
| 183 | + ## Sending | |
| 184 | + | |
| 185 | + defp send_local(envelope, org, project, key) do | |
| 186 | + case Keys.lookup(project.dsn_id, key.public_key) do | |
| 187 | + {:ok, target} -> | |
| 188 | + kept = | |
| 189 | + Vigil.Ingest.accept(target, envelope, %{ | |
| 190 | + client_ip: "127.0.0.1", | |
| 191 | + user_agent: "mix vigil.test" | |
| 192 | + }) | |
| 193 | + | |
| 194 | + drain_all(project.id) | |
| 195 | + | |
| 196 | + Mix.shell().info( | |
| 197 | + "Sent #{kept} event(s) to #{org.slug}/#{project.slug}, and processed them." | |
| 198 | + ) | |
| 199 | + | |
| 200 | + Mix.shell().info(VigilWeb.base_url() <> "/orgs/#{org.slug}/projects/#{project.slug}") | |
| 201 | + | |
| 202 | + {:error, :not_accepting} -> | |
| 203 | + Mix.raise("#{org.slug} isn't accepting events (organization settings)") | |
| 204 | + | |
| 205 | + {:error, _} -> | |
| 206 | + Mix.raise("The project's key was refused") | |
| 207 | + end | |
| 208 | + end | |
| 209 | + | |
| 210 | + # Until nothing pending is left (a full batch leaves more). | |
| 211 | + defp drain_all(project_id) do | |
| 212 | + if Drain.drain(project_id) >= 100, do: drain_all(project_id) | |
| 213 | + end | |
| 214 | + | |
| 215 | + defp send_http(envelope, project, key, url) do | |
| 216 | + body = | |
| 217 | + Enum.join( | |
| 218 | + [Jason.encode!(envelope.headers)] ++ | |
| 219 | + Enum.flat_map(envelope.items, fn i -> | |
| 220 | + [Jason.encode!(Map.put(i.headers, "length", byte_size(i.payload))), i.payload] | |
| 221 | + end), | |
| 222 | + "\n" | |
| 223 | + ) <> "\n" | |
| 224 | + | |
| 225 | + hex = String.replace(key.public_key, "-", "") | |
| 226 | + endpoint = String.trim_trailing(url, "/") <> "/api/#{project.dsn_id}/envelope/" | |
| 227 | + | |
| 228 | + case Req.post(endpoint, | |
| 229 | + body: body, | |
| 230 | + headers: [ | |
| 231 | + {"content-type", "application/x-sentry-envelope"}, | |
| 232 | + {"x-sentry-auth", | |
| 233 | + "Sentry sentry_key=#{hex}, sentry_version=7, sentry_client=vigil.test/1"} | |
| 234 | + ], | |
| 235 | + retry: false | |
| 236 | + ) do | |
| 237 | + {:ok, %{status: 200, body: body}} -> | |
| 238 | + Mix.shell().info("#{endpoint} accepted it: #{inspect(body)}") | |
| 239 | + | |
| 240 | + {:ok, %{status: status, body: body}} -> | |
| 241 | + Mix.raise("#{endpoint} answered #{status}: #{inspect(body)}") | |
| 242 | + | |
| 243 | + {:error, error} -> | |
| 244 | + Mix.raise( | |
| 245 | + "Couldn't reach #{endpoint}: #{Exception.message(error)} (is the server running?)" | |
| 246 | + ) | |
| 247 | + end | |
| 248 | + end | |
| 249 | +end |
modified
lib/vigil/application.ex
+5
−2
| 58 | 58 | end |
| 59 | 59 | |
| 60 | 60 | defp maybe_maintain_partitions do |
| 61 | − if Application.get_env(:vigil, :maintain_partitions_on_boot, true), | |
| 62 | − do: Vigil.Partitions.maintain() | |
| 61 | + if Application.get_env(:vigil, :maintain_partitions_on_boot, true) do | |
| 62 | + Vigil.Partitions.maintain() | |
| 63 | + # Items that waited for a processor which exists now. | |
| 64 | + Vigil.Ingest.revive_deferred() | |
| 65 | + end | |
| 63 | 66 | end |
| 64 | 67 | end |
added
lib/vigil/events/feedback_processor.ex
+83
−0
Click to load diff…
added
lib/vigil/events/normalize.ex
+539
−0
Click to load diff…
added
lib/vigil/events/processor.ex
+419
−0
Click to load diff…
added
lib/vigil/events/user_agent.ex
+45
−0
Click to load diff…
added
lib/vigil/events/user_agent/native.ex
+9
−0
Click to load diff…
modified
lib/vigil/ingest.ex
+19
−0
Click to load diff…
modified
lib/vigil/ingest/outcomes.ex
+1
−1
Click to load diff…
modified
lib/vigil/ingest/processor.ex
+9
−1
Click to load diff…
added
lib/vigil/issues.ex
+177
−0
Click to load diff…
added
lib/vigil/issues/issue.ex
+35
−0
Click to load diff…
added
lib/vigil/issues/issue_event.ex
+30
−0
Click to load diff…
added
lib/vigil/issues/issue_hash.ex
+19
−0
Click to load diff…
added
lib/vigil/issues/user_report.ex
+20
−0
Click to load diff…
modified
lib/vigil/partitions.ex
+28
−8
Click to load diff…
modified
lib/vigil/projects/project.ex
+5
−0
Click to load diff…
added
lib/vigil/releases.ex
+98
−0
Click to load diff…
added
lib/vigil/releases/release.ex
+23
−0
Click to load diff…
added
native/vigil_ua/Cargo.lock
+187
−0
Click to load diff…
added
native/vigil_ua/Cargo.toml
+21
−0
Click to load diff…
added
native/vigil_ua/src/lib.rs
+38
−0
Click to load diff…
added
priv/repo/migrations/20261201215637_create_issues_and_events.exs
+237
−0
Click to load diff…
added
test/mix/vigil_test_task_test.exs
+58
−0
Click to load diff…
modified
test/support/sdk/sentry_elixir_send.exs
+5
−0
Click to load diff…
modified
test/test_helper.exs
+3
−0
Click to load diff…
added
test/vigil/events/normalize_test.exs
+203
−0
Click to load diff…
added
test/vigil/events/processor_test.exs
+242
−0
Click to load diff…
modified
test/vigil_web/ingest_plug_test.exs
+9
−3
Click to load diff…
modified
test/vigil_web/sdk_conformance_test.exs
+16
−0
Click to load diff…
Parents: ed9d1e1