neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Slice 12: vigil_symbolic NIF, debug file upload and native symbolication
334594a · gmorell · 2026-12-02 04:21
Files changed
modified
lib/vigil/artifacts.ex
+28
−8
| 57 | 57 | # sentry-cli only chunks source maps at all when release_files is |
| 58 | 58 | # offered; with projects named it then sends artifact bundles, and |
| 59 | 59 | # without, the release-bound assemble (`assemble_for_release/3`). |
| 60 | − accept: ["release_files", "artifact_bundles", "artifact_bundles_v2"] | |
| 60 | + # debug_files: `debug-files upload` through `difs/assemble/` | |
| 61 | + # (Vigil.DebugFiles); pdbs and portablepdbs let it send PE/PDB and | |
| 62 | + # Portable PDB files and their debug ids; proguard: `upload-proguard` | |
| 63 | + # with SENTRY_EXPERIMENTAL_PROGUARD_CHUNK_UPLOAD=1. Not offered: | |
| 64 | + # sources (source bundles), bcsymbolmaps, il2cpp, dartsymbolmap. | |
| 65 | + accept: [ | |
| 66 | + "release_files", | |
| 67 | + "artifact_bundles", | |
| 68 | + "artifact_bundles_v2", | |
| 69 | + "debug_files", | |
| 70 | + "pdbs", | |
| 71 | + "portablepdbs", | |
| 72 | + "proguard" | |
| 73 | + ] | |
| 61 | 74 | } |
| 62 | 75 | end |
| 63 | 76 |
| 209 | 222 | defp assemble_projects(_scope, _), |
| 210 | 223 | do: {:error, 400, "projects: at least one project is required"} |
| 211 | 224 | |
| 212 | − defp start_assemble(org, checksum, chunks, projects, params) do | |
| 213 | − held = | |
| 214 | − Repo.all(from b in blob_query(org.id, chunks), select: b.checksum) |> MapSet.new() | |
| 225 | + @doc "Which of `chunks` (SHA-1s) the organization holds no blob for." | |
| 226 | + def missing_chunks(org_id, chunks) do | |
| 227 | + held = Repo.all(from b in blob_query(org_id, chunks), select: b.checksum) |> MapSet.new() | |
| 228 | + Enum.reject(Enum.uniq(chunks), &MapSet.member?(held, &1)) | |
| 229 | + end | |
| 215 | 230 | |
| 216 | − case Enum.reject(Enum.uniq(chunks), &MapSet.member?(held, &1)) do | |
| 231 | + defp start_assemble(org, checksum, chunks, projects, params) do | |
| 232 | + case missing_chunks(org.id, chunks) do | |
| 217 | 233 | [] when chunks != [] -> |
| 218 | 234 | now = DateTime.utc_now(:second) |
| 219 | 235 |
| 294 | 310 | end |
| 295 | 311 | end |
| 296 | 312 | |
| 297 | − defp join_chunks(org_id, chunks) do | |
| 313 | + @doc """ | |
| 314 | + An uploaded file's bytes: the organization's blobs for `chunks`, in | |
| 315 | + order, at most `max` bytes in all; or `{:error, detail}`. | |
| 316 | + """ | |
| 317 | + def join_chunks(org_id, chunks, max \\ @max_file_size) do | |
| 298 | 318 | blobs = Repo.all(blob_query(org_id, chunks)) |> Map.new(&{&1.checksum, &1}) |
| 299 | 319 | |
| 300 | 320 | Enum.reduce_while(chunks, {:ok, [], 0}, fn checksum, {:ok, acc, size} -> |
| 301 | 321 | with %FileBlob{} = blob <- Map.get(blobs, checksum), |
| 302 | − true <- size + blob.size <= @max_file_size || :too_large, | |
| 322 | + true <- size + blob.size <= max || :too_large, | |
| 303 | 323 | {:ok, data} <- Storage.get(blob.storage_key) do |
| 304 | 324 | {:cont, {:ok, [acc, data], size + blob.size}} |
| 305 | 325 | else |
| 306 | − :too_large -> {:halt, {:error, "Bundle is larger than #{@max_file_size} bytes"}} | |
| 326 | + :too_large -> {:halt, {:error, "File is larger than #{max} bytes"}} | |
| 307 | 327 | _ -> {:halt, {:error, "Chunk #{checksum} is missing"}} |
| 308 | 328 | end |
| 309 | 329 | end) |
modified
lib/vigil/artifacts/cleanup_worker.ex
+9
−2
| 1 | 1 | defmodule Vigil.Artifacts.CleanupWorker do |
| 2 | − @moduledoc "Daily: drops chunk blobs and assemble statuses over a day old (`Vigil.Artifacts.cleanup/1`)." | |
| 2 | + @moduledoc """ | |
| 3 | + Daily: drops chunk blobs and assemble statuses over a day old | |
| 4 | + (`Vigil.Artifacts.cleanup/1`), and debug-file assemble states and old | |
| 5 | + event attachments (`Vigil.DebugFiles.cleanup/1`). | |
| 6 | + """ | |
| 3 | 7 | use Oban.Worker, queue: :maintenance, max_attempts: 3 |
| 4 | 8 | |
| 5 | 9 | @impl Oban.Worker |
| 6 | − def perform(_job), do: Vigil.Artifacts.cleanup() | |
| 10 | + def perform(_job) do | |
| 11 | + Vigil.Artifacts.cleanup() | |
| 12 | + Vigil.DebugFiles.cleanup() | |
| 13 | + end | |
| 7 | 14 | end |
added
lib/vigil/debug_files.ex
+518
−0
| 1 | +defmodule Vigil.DebugFiles do | |
| 2 | + @moduledoc """ | |
| 3 | + Debug information files (ROADMAP.md, slice 12): what `sentry-cli | |
| 4 | + debug-files upload` and `upload-proguard` send, kept per project for | |
| 5 | + native and JVM symbolication (`Vigil.Symbolication.Native`, | |
| 6 | + `Vigil.Symbolication.JVM`). | |
| 7 | + | |
| 8 | + ## Chunked upload (`debug-files upload`) | |
| 9 | + | |
| 10 | + 1. `GET /organizations/{org}/chunk-upload/` offers `debug_files` (and | |
| 11 | + `pdbs`, `portablepdbs`, `proguard`): `Vigil.Artifacts.chunk_options/1`. | |
| 12 | + 2. `POST /projects/{org}/{project}/files/difs/assemble/` with | |
| 13 | + `{sha1: {name, debug_id?, chunks}}` (`assemble/3`). Each file | |
| 14 | + answers `not_found` with the chunks to upload; `created` once they | |
| 15 | + are all here (an `AssembleWorker` job is queued); `assembling`; | |
| 16 | + `ok` with the stored file as Sentry's `DebugInfoFile` (`dif`); or | |
| 17 | + `error` with a `detail`, told once so a re-upload retries. | |
| 18 | + 3. Chunks go through the organization's `chunk-upload/` | |
| 19 | + (`Vigil.Artifacts.store_chunks/2`); sentry-cli asks again, and polls | |
| 20 | + with `--wait`. | |
| 21 | + | |
| 22 | + The job joins the chunks, checks the whole SHA-1, and reads the file | |
| 23 | + with symbolic (`Vigil.Symbolic.object_info/1`): one `DebugFile` per | |
| 24 | + object (a fat Mach-O has one per architecture), with its debug id, code | |
| 25 | + id, arch, kind, format and features. A file symbolic doesn't know is | |
| 26 | + tried as a Proguard mapping (`debug_id` its UUID: from a | |
| 27 | + `proguard/<uuid>.txt` name, as sentry-cli sends it, else computed as | |
| 28 | + sentry-cli does). The bytes go to `Vigil.Storage`; a SymCache is built | |
| 29 | + at the first lookup and kept there too (`symcache/1`). | |
| 30 | + | |
| 31 | + ## Legacy upload (`upload-proguard`) | |
| 32 | + | |
| 33 | + `POST /projects/{org}/{project}/files/dsyms/` with a zip of files | |
| 34 | + (`upload_archive/3`), which `upload-proguard` sends unless chunked | |
| 35 | + upload is switched on. Also `GET …/files/dsyms/` (`list/2`), | |
| 36 | + `GET …/files/dsyms/unknown/?checksums=` (`missing_checksums/2`) and | |
| 37 | + `DELETE …/files/dsyms/?id=` (`delete/2`). | |
| 38 | + """ | |
| 39 | + | |
| 40 | + import Ecto.Query | |
| 41 | + | |
| 42 | + alias Vigil.{Artifacts, Repo, Storage, Symbolic} | |
| 43 | + alias Vigil.DebugFiles.{Assembly, AssembleWorker, DebugFile} | |
| 44 | + alias Vigil.Projects.Project | |
| 45 | + | |
| 46 | + require Logger | |
| 47 | + | |
| 48 | + @max_file_size 256 * 1024 * 1024 | |
| 49 | + @max_files_per_request 512 | |
| 50 | + @sha1 ~r/\A[0-9a-f]{40}\z/ | |
| 51 | + @proguard_name ~r"(?:\A|/)proguard/([0-9a-fA-F-]{36})\.txt\z" | |
| 52 | + | |
| 53 | + @doc "The most a legacy `files/dsyms/` upload may be (the multipart parser's cap)." | |
| 54 | + def max_upload_size, do: @max_file_size | |
| 55 | + | |
| 56 | + ## Chunked assemble | |
| 57 | + | |
| 58 | + @doc """ | |
| 59 | + A `difs/assemble/` request for `project` (already checked visible): | |
| 60 | + `params` maps each file's SHA-1 to `name`, `debug_id` (optional) and | |
| 61 | + `chunks`. Answers `{:ok, %{sha1 => %{state:, missingChunks:, detail:, | |
| 62 | + dif:}}}` or `{:error, 400, detail}`. | |
| 63 | + """ | |
| 64 | + def assemble(%Project{} = project, params) when is_map(params) do | |
| 65 | + requests = params | |
| 66 | + | |
| 67 | + if map_size(requests) > @max_files_per_request do | |
| 68 | + {:error, 400, "Too many files"} | |
| 69 | + else | |
| 70 | + Enum.reduce_while(requests, {:ok, %{}}, fn {checksum, request}, {:ok, acc} -> | |
| 71 | + case assemble_one(project, String.downcase(checksum), request) do | |
| 72 | + {:ok, response} -> {:cont, {:ok, Map.put(acc, checksum, response)}} | |
| 73 | + {:error, _, _} = error -> {:halt, error} | |
| 74 | + end | |
| 75 | + end) | |
| 76 | + end | |
| 77 | + end | |
| 78 | + | |
| 79 | + defp assemble_one(project, checksum, %{} = request) do | |
| 80 | + chunks = | |
| 81 | + request |> Map.get("chunks", []) |> List.wrap() |> Enum.map(&String.downcase(to_string(&1))) | |
| 82 | + | |
| 83 | + name = request |> Map.get("name") |> string() || checksum | |
| 84 | + | |
| 85 | + if Regex.match?(@sha1, checksum) and Enum.all?(chunks, &Regex.match?(@sha1, &1)) do | |
| 86 | + {:ok, assemble_state(project, checksum, name, chunks)} | |
| 87 | + else | |
| 88 | + {:error, 400, "Invalid checksum"} | |
| 89 | + end | |
| 90 | + end | |
| 91 | + | |
| 92 | + defp assemble_one(_project, _checksum, _request), do: {:error, 400, "Invalid request"} | |
| 93 | + | |
| 94 | + defp assemble_state(project, checksum, name, chunks) do | |
| 95 | + case existing(project.id, checksum) do | |
| 96 | + %DebugFile{} = file -> | |
| 97 | + response(:ok, [], nil, file) | |
| 98 | + | |
| 99 | + nil -> | |
| 100 | + case Repo.get_by(Assembly, project_id: project.id, checksum: checksum) do | |
| 101 | + # An error is told once, then forgotten, so uploading again retries. | |
| 102 | + %Assembly{state: :error} = assembly -> | |
| 103 | + Repo.delete(assembly) | |
| 104 | + response(:error, [], assembly.detail, nil) | |
| 105 | + | |
| 106 | + # `ok` but no file: deleted since. Start over. | |
| 107 | + %Assembly{state: :ok} = assembly -> | |
| 108 | + Repo.delete(assembly) | |
| 109 | + start_assemble(project, checksum, name, chunks) | |
| 110 | + | |
| 111 | + %Assembly{state: state, detail: detail} -> | |
| 112 | + response(state, [], detail, nil) | |
| 113 | + | |
| 114 | + nil -> | |
| 115 | + start_assemble(project, checksum, name, chunks) | |
| 116 | + end | |
| 117 | + end | |
| 118 | + end | |
| 119 | + | |
| 120 | + defp existing(project_id, checksum) do | |
| 121 | + Repo.one( | |
| 122 | + from f in DebugFile, | |
| 123 | + where: f.project_id == ^project_id and f.checksum == ^checksum, | |
| 124 | + order_by: [asc: f.id], | |
| 125 | + limit: 1 | |
| 126 | + ) | |
| 127 | + end | |
| 128 | + | |
| 129 | + defp start_assemble(project, checksum, name, chunks) do | |
| 130 | + case Artifacts.missing_chunks(project.organization_id, chunks) do | |
| 131 | + [] when chunks != [] -> | |
| 132 | + now = DateTime.utc_now(:second) | |
| 133 | + | |
| 134 | + Repo.insert_all( | |
| 135 | + Assembly, | |
| 136 | + [ | |
| 137 | + %{ | |
| 138 | + id: Vigil.Schema.uuid7(), | |
| 139 | + project_id: project.id, | |
| 140 | + checksum: checksum, | |
| 141 | + state: :created, | |
| 142 | + inserted_at: now, | |
| 143 | + updated_at: now | |
| 144 | + } | |
| 145 | + ], | |
| 146 | + on_conflict: :nothing, | |
| 147 | + conflict_target: [:project_id, :checksum] | |
| 148 | + ) | |
| 149 | + | |
| 150 | + %{ | |
| 151 | + organization_id: project.organization_id, | |
| 152 | + project_id: project.id, | |
| 153 | + checksum: checksum, | |
| 154 | + name: name, | |
| 155 | + chunks: chunks | |
| 156 | + } | |
| 157 | + |> AssembleWorker.new() | |
| 158 | + |> Oban.insert!() | |
| 159 | + | |
| 160 | + response(:created, [], nil, nil) | |
| 161 | + | |
| 162 | + missing -> | |
| 163 | + response(:not_found, missing, nil, nil) | |
| 164 | + end | |
| 165 | + end | |
| 166 | + | |
| 167 | + defp response(state, missing, detail, file) do | |
| 168 | + %{ | |
| 169 | + state: to_string(state), | |
| 170 | + missingChunks: missing, | |
| 171 | + detail: detail, | |
| 172 | + dif: file && serialize(file) | |
| 173 | + } | |
| 174 | + end | |
| 175 | + | |
| 176 | + @doc """ | |
| 177 | + Assembles a debug file (the `AssembleWorker` job): joins its chunks, | |
| 178 | + checks the whole SHA-1, reads and stores it. Records `ok` or `error` on | |
| 179 | + the assembly either way. | |
| 180 | + """ | |
| 181 | + def run_assemble(%{"project_id" => project_id, "checksum" => checksum} = args) do | |
| 182 | + set_state(project_id, checksum, :assembling, nil) | |
| 183 | + | |
| 184 | + result = | |
| 185 | + with {:ok, data} <- | |
| 186 | + Artifacts.join_chunks(args["organization_id"], args["chunks"], @max_file_size), | |
| 187 | + :ok <- check_whole(data, checksum) do | |
| 188 | + create_files( | |
| 189 | + %{organization_id: args["organization_id"], project_id: project_id}, | |
| 190 | + args["name"], | |
| 191 | + checksum, | |
| 192 | + data | |
| 193 | + ) | |
| 194 | + end | |
| 195 | + | |
| 196 | + case result do | |
| 197 | + {:ok, files} -> | |
| 198 | + set_state(project_id, checksum, :ok, nil) | |
| 199 | + {:ok, files} | |
| 200 | + | |
| 201 | + {:error, detail} -> | |
| 202 | + Logger.warning("debug file #{checksum} failed: #{detail}") | |
| 203 | + set_state(project_id, checksum, :error, detail) | |
| 204 | + {:error, detail} | |
| 205 | + end | |
| 206 | + end | |
| 207 | + | |
| 208 | + defp check_whole(data, checksum) do | |
| 209 | + if sha1(data) == checksum, do: :ok, else: {:error, "File checksum mismatch"} | |
| 210 | + end | |
| 211 | + | |
| 212 | + defp set_state(project_id, checksum, state, detail) do | |
| 213 | + Repo.update_all( | |
| 214 | + from(a in Assembly, where: a.project_id == ^project_id and a.checksum == ^checksum), | |
| 215 | + set: [state: state, detail: detail, updated_at: DateTime.utc_now(:second)] | |
| 216 | + ) | |
| 217 | + end | |
| 218 | + | |
| 219 | + ## Reading and storing a file | |
| 220 | + | |
| 221 | + @doc """ | |
| 222 | + Reads `data` (named `name`, SHA-1 `checksum`) and stores it for the | |
| 223 | + project: `{:ok, [DebugFile]}`, one per object, or `{:error, detail}`. | |
| 224 | + A file already stored (same checksum and debug id) is kept as it is. | |
| 225 | + """ | |
| 226 | + def create_files(%{organization_id: org_id, project_id: project_id}, name, checksum, data) do | |
| 227 | + with {:ok, objects} <- identify(name, data) do | |
| 228 | + key = "difs/#{org_id}/#{project_id}/#{checksum}" | |
| 229 | + now = DateTime.utc_now(:second) | |
| 230 | + | |
| 231 | + rows = | |
| 232 | + for object <- objects do | |
| 233 | + %{ | |
| 234 | + id: Vigil.Schema.uuid7(), | |
| 235 | + organization_id: org_id, | |
| 236 | + project_id: project_id, | |
| 237 | + checksum: checksum, | |
| 238 | + debug_id: object.debug_id, | |
| 239 | + code_id: object.code_id, | |
| 240 | + object_name: String.slice(name, 0, 1000), | |
| 241 | + cpu_name: object.arch, | |
| 242 | + file_format: object.file_format, | |
| 243 | + kind: object.kind, | |
| 244 | + features: object.features, | |
| 245 | + size: byte_size(data), | |
| 246 | + storage_key: key, | |
| 247 | + inserted_at: now, | |
| 248 | + updated_at: now | |
| 249 | + } | |
| 250 | + end | |
| 251 | + | |
| 252 | + case Storage.put(key, data) do | |
| 253 | + :ok -> | |
| 254 | + Repo.insert_all(DebugFile, rows, | |
| 255 | + on_conflict: :nothing, | |
| 256 | + conflict_target: [:project_id, :checksum, :debug_id] | |
| 257 | + ) | |
| 258 | + | |
| 259 | + {:ok, | |
| 260 | + Repo.all( | |
| 261 | + from f in DebugFile, | |
| 262 | + where: f.project_id == ^project_id and f.checksum == ^checksum, | |
| 263 | + order_by: [asc: f.id] | |
| 264 | + )} | |
| 265 | + | |
| 266 | + {:error, reason} -> | |
| 267 | + {:error, "Could not store the file: #{inspect(reason)}"} | |
| 268 | + end | |
| 269 | + end | |
| 270 | + end | |
| 271 | + | |
| 272 | + defp identify(name, data) do | |
| 273 | + case Symbolic.object_info(data) do | |
| 274 | + {:ok, [_ | _] = objects} -> | |
| 275 | + {:ok, objects} | |
| 276 | + | |
| 277 | + {:ok, []} -> | |
| 278 | + {:error, "The file contains no objects"} | |
| 279 | + | |
| 280 | + {:error, :unsupported} -> | |
| 281 | + proguard(name, data) | |
| 282 | + | |
| 283 | + {:error, :too_many_objects} -> | |
| 284 | + {:error, "The file contains too many objects"} | |
| 285 | + | |
| 286 | + {:error, :too_large} -> | |
| 287 | + {:error, "The file is too large"} | |
| 288 | + | |
| 289 | + {:error, _} -> | |
| 290 | + {:error, "The debug information file could not be read"} | |
| 291 | + end | |
| 292 | + end | |
| 293 | + | |
| 294 | + defp proguard(name, data) do | |
| 295 | + case Symbolic.proguard_info(data) do | |
| 296 | + {:ok, %{line_info: false}} -> | |
| 297 | + {:error, "Proguard mapping does not contain line information"} | |
| 298 | + | |
| 299 | + {:ok, %{uuid: uuid}} -> | |
| 300 | + uuid = | |
| 301 | + case Regex.run(@proguard_name, name, capture: :all_but_first) do | |
| 302 | + [named] -> String.downcase(named) | |
| 303 | + nil -> uuid | |
| 304 | + end | |
| 305 | + | |
| 306 | + {:ok, | |
| 307 | + [ | |
| 308 | + %{ | |
| 309 | + debug_id: uuid, | |
| 310 | + code_id: nil, | |
| 311 | + arch: "any", | |
| 312 | + kind: nil, | |
| 313 | + file_format: "proguard", | |
| 314 | + features: ["mapping"] | |
| 315 | + } | |
| 316 | + ]} | |
| 317 | + | |
| 318 | + {:error, _} -> | |
| 319 | + {:error, "Unsupported debug information file"} | |
| 320 | + end | |
| 321 | + end | |
| 322 | + | |
| 323 | + ## Legacy upload, list, delete | |
| 324 | + | |
| 325 | + @doc """ | |
| 326 | + The legacy `POST …/files/dsyms/` upload: a zip of debug files (or one | |
| 327 | + bare file). Each file is read and stored; files that aren't debug files | |
| 328 | + are skipped. Answers the stored files. | |
| 329 | + """ | |
| 330 | + def upload_archive(%Project{} = project, filename, data) do | |
| 331 | + files = | |
| 332 | + case data do | |
| 333 | + <<"PK", 3, 4, _::binary>> -> | |
| 334 | + case Symbolic.read_zip(data, @max_file_size) do | |
| 335 | + {:ok, files} -> {:ok, files} | |
| 336 | + {:error, :too_large} -> {:error, "The archive is too large"} | |
| 337 | + {:error, :too_many_files} -> {:error, "The archive has too many files"} | |
| 338 | + {:error, _} -> {:error, "The archive is not a valid zip"} | |
| 339 | + end | |
| 340 | + | |
| 341 | + _ -> | |
| 342 | + {:ok, [{filename || "file", data}]} | |
| 343 | + end | |
| 344 | + | |
| 345 | + with {:ok, files} <- files do | |
| 346 | + target = %{organization_id: project.organization_id, project_id: project.id} | |
| 347 | + | |
| 348 | + stored = | |
| 349 | + Enum.flat_map(files, fn {name, bytes} -> | |
| 350 | + case create_files(target, name, sha1(bytes), bytes) do | |
| 351 | + {:ok, rows} -> rows | |
| 352 | + {:error, _} -> [] | |
| 353 | + end | |
| 354 | + end) | |
| 355 | + | |
| 356 | + {:ok, stored} | |
| 357 | + end | |
| 358 | + end | |
| 359 | + | |
| 360 | + @doc """ | |
| 361 | + A project's debug files, newest first. Options: `query` (a debug id, | |
| 362 | + code id or name fragment), `limit` (100). | |
| 363 | + """ | |
| 364 | + def list(%Project{id: project_id}, opts \\ []) do | |
| 365 | + limit = opts |> Keyword.get(:limit, 100) |> min(1000) | |
| 366 | + | |
| 367 | + query = | |
| 368 | + from f in DebugFile, where: f.project_id == ^project_id, order_by: [desc: f.id], limit: ^limit | |
| 369 | + | |
| 370 | + query = | |
| 371 | + case opts |> Keyword.get(:query) |> string() do | |
| 372 | + nil -> | |
| 373 | + query | |
| 374 | + | |
| 375 | + q -> | |
| 376 | + like = "%" <> String.replace(q, ["%", "_", "\\"], &("\\" <> &1)) <> "%" | |
| 377 | + | |
| 378 | + from f in query, | |
| 379 | + where: | |
| 380 | + f.debug_id == ^String.downcase(q) or f.code_id == ^String.downcase(q) or | |
| 381 | + ilike(f.object_name, ^like) | |
| 382 | + end | |
| 383 | + | |
| 384 | + Repo.all(query) | |
| 385 | + end | |
| 386 | + | |
| 387 | + @doc "Which of `checksums` the project has no debug file for." | |
| 388 | + def missing_checksums(%Project{id: project_id}, checksums) do | |
| 389 | + checksums = checksums |> Enum.map(&String.downcase/1) |> Enum.uniq() | |
| 390 | + | |
| 391 | + held = | |
| 392 | + Repo.all( | |
| 393 | + from f in DebugFile, | |
| 394 | + where: f.project_id == ^project_id and f.checksum in ^checksums, | |
| 395 | + select: f.checksum | |
| 396 | + ) | |
| 397 | + |> MapSet.new() | |
| 398 | + | |
| 399 | + Enum.reject(checksums, &MapSet.member?(held, &1)) | |
| 400 | + end | |
| 401 | + | |
| 402 | + @doc "Deletes one of the project's debug files (`:ok`, or `:error` when there is none)." | |
| 403 | + def delete(%Project{id: project_id}, id) do | |
| 404 | + with {:ok, id} <- Ecto.UUID.cast(id), | |
| 405 | + %DebugFile{} = file <- Repo.get_by(DebugFile, id: id, project_id: project_id) do | |
| 406 | + Repo.delete!(file) | |
| 407 | + | |
| 408 | + if file.symcache_key, do: Storage.delete(file.symcache_key) | |
| 409 | + | |
| 410 | + unless Repo.exists?(from f in DebugFile, where: f.storage_key == ^file.storage_key), | |
| 411 | + do: Storage.delete(file.storage_key) | |
| 412 | + | |
| 413 | + :ok | |
| 414 | + else | |
| 415 | + _ -> :error | |
| 416 | + end | |
| 417 | + end | |
| 418 | + | |
| 419 | + @doc "A debug file as Sentry's API shows it (`DebugInfoFile`)." | |
| 420 | + def serialize(%DebugFile{} = file) do | |
| 421 | + %{ | |
| 422 | + id: file.id, | |
| 423 | + uuid: file.debug_id, | |
| 424 | + debugId: file.debug_id, | |
| 425 | + codeId: file.code_id, | |
| 426 | + cpuName: file.cpu_name, | |
| 427 | + objectName: file.object_name, | |
| 428 | + symbolType: file.file_format, | |
| 429 | + headers: %{"Content-Type" => content_type(file)}, | |
| 430 | + size: file.size, | |
| 431 | + sha1: file.checksum, | |
| 432 | + dateCreated: file.inserted_at, | |
| 433 | + data: %{type: file.kind, features: file.features} | |
| 434 | + } | |
| 435 | + end | |
| 436 | + | |
| 437 | + defp content_type(%DebugFile{file_format: "proguard"}), do: "text/x-proguard+plain" | |
| 438 | + defp content_type(%DebugFile{file_format: format}), do: "application/x-#{format}" | |
| 439 | + | |
| 440 | + ## Symbolication | |
| 441 | + | |
| 442 | + @doc """ | |
| 443 | + The best debug file of the project for each of `debug_ids` (normalised): | |
| 444 | + one with DWARF/PDB line info over one with only a symbol table, newer | |
| 445 | + over older. Proguard mappings excluded. | |
| 446 | + """ | |
| 447 | + @spec for_debug_ids(Ecto.UUID.t(), [String.t()]) :: %{String.t() => DebugFile.t()} | |
| 448 | + def for_debug_ids(_project_id, []), do: %{} | |
| 449 | + | |
| 450 | + def for_debug_ids(project_id, debug_ids) do | |
| 451 | + from(f in DebugFile, | |
| 452 | + where: | |
| 453 | + f.project_id == ^project_id and f.debug_id in ^debug_ids and f.file_format != "proguard" | |
| 454 | + ) | |
| 455 | + |> Repo.all() | |
| 456 | + |> Enum.group_by(& &1.debug_id) | |
| 457 | + |> Map.new(fn {id, files} -> {id, Enum.max_by(files, &rank/1)} end) | |
| 458 | + end | |
| 459 | + | |
| 460 | + defp rank(file), do: {"debug" in file.features, "symtab" in file.features, file.id} | |
| 461 | + | |
| 462 | + @doc "The newest Proguard mapping of the project with this UUID, or `nil`." | |
| 463 | + def proguard_mapping(project_id, uuid) do | |
| 464 | + Repo.one( | |
| 465 | + from f in DebugFile, | |
| 466 | + where: | |
| 467 | + f.project_id == ^project_id and f.debug_id == ^String.downcase(uuid) and | |
| 468 | + f.file_format == "proguard", | |
| 469 | + order_by: [desc: f.id], | |
| 470 | + limit: 1 | |
| 471 | + ) | |
| 472 | + end | |
| 473 | + | |
| 474 | + @doc "A debug file's bytes." | |
| 475 | + def contents(%DebugFile{storage_key: key}), do: Storage.get(key) | |
| 476 | + | |
| 477 | + @doc """ | |
| 478 | + The SymCache of a debug file: the stored one, or built now from the file | |
| 479 | + and stored for next time. | |
| 480 | + """ | |
| 481 | + def symcache(%DebugFile{} = file) do | |
| 482 | + cached = if file.symcache_key, do: Storage.get(file.symcache_key), else: {:error, :none} | |
| 483 | + | |
| 484 | + case cached do | |
| 485 | + {:ok, symcache} -> {:ok, symcache} | |
| 486 | + _ -> build_symcache(file) | |
| 487 | + end | |
| 488 | + end | |
| 489 | + | |
| 490 | + defp build_symcache(file) do | |
| 491 | + key = "symcaches/#{file.organization_id}/#{file.project_id}/#{file.checksum}/#{file.debug_id}" | |
| 492 | + | |
| 493 | + with {:ok, data} <- contents(file), | |
| 494 | + {:ok, symcache} <- Symbolic.build_symcache(data, file.debug_id), | |
| 495 | + :ok <- Storage.put(key, symcache) do | |
| 496 | + Repo.update_all(from(f in DebugFile, where: f.id == ^file.id), set: [symcache_key: key]) | |
| 497 | + {:ok, symcache} | |
| 498 | + end | |
| 499 | + end | |
| 500 | + | |
| 501 | + ## Housekeeping | |
| 502 | + | |
| 503 | + @doc """ | |
| 504 | + Drops assemble states older than `max_age_seconds` (a day), and event | |
| 505 | + attachments (raw minidumps) past the instance's event retention. | |
| 506 | + """ | |
| 507 | + def cleanup(max_age_seconds \\ 86_400) do | |
| 508 | + cutoff = DateTime.add(DateTime.utc_now(), -max_age_seconds) | |
| 509 | + Repo.delete_all(from a in Assembly, where: a.updated_at < ^cutoff) | |
| 510 | + # MINIDUMPS_CLEANUP | |
| 511 | + :ok | |
| 512 | + end | |
| 513 | + | |
| 514 | + defp sha1(data), do: :crypto.hash(:sha, data) |> Base.encode16(case: :lower) | |
| 515 | + | |
| 516 | + defp string(v) when is_binary(v) and v != "", do: v | |
| 517 | + defp string(_), do: nil | |
| 518 | +end |
added
lib/vigil/debug_files/assemble_worker.ex
+20
−0
| 1 | +defmodule Vigil.DebugFiles.AssembleWorker do | |
| 2 | + @moduledoc """ | |
| 3 | + Joins an uploaded debug file's chunks, reads it with symbolic, and | |
| 4 | + stores it (`Vigil.DebugFiles.run_assemble/1`). One job per (project, | |
| 5 | + checksum) at a time. A failure is recorded on the assembly for | |
| 6 | + sentry-cli to read, so the job doesn't retry it. | |
| 7 | + """ | |
| 8 | + use Oban.Worker, | |
| 9 | + queue: :processing, | |
| 10 | + max_attempts: 1, | |
| 11 | + unique: [keys: [:project_id, :checksum], states: [:available, :scheduled, :executing]] | |
| 12 | + | |
| 13 | + @impl Oban.Worker | |
| 14 | + def perform(%Oban.Job{args: args}) do | |
| 15 | + case Vigil.DebugFiles.run_assemble(args) do | |
| 16 | + {:ok, _files} -> :ok | |
| 17 | + {:error, detail} -> {:cancel, detail} | |
| 18 | + end | |
| 19 | + end | |
| 20 | +end |
added
lib/vigil/debug_files/assembly.ex
+17
−0
| 1 | +defmodule Vigil.DebugFiles.Assembly do | |
| 2 | + @moduledoc """ | |
| 3 | + Where assembling the debug file `checksum` stands for a project: | |
| 4 | + `created` (queued), `assembling`, then `ok` or `error` (with `detail`). | |
| 5 | + sentry-cli polls this through `difs/assemble/` for `--wait`. | |
| 6 | + """ | |
| 7 | + use Vigil.Schema | |
| 8 | + | |
| 9 | + schema "debug_file_assemblies" do | |
| 10 | + field :project_id, Ecto.UUID | |
| 11 | + field :checksum, :string | |
| 12 | + field :state, Ecto.Enum, values: [:created, :assembling, :ok, :error] | |
| 13 | + field :detail, :string | |
| 14 | + | |
| 15 | + timestamps(type: :utc_datetime) | |
| 16 | + end | |
| 17 | +end |
added
lib/vigil/debug_files/debug_file.ex
+29
−0
Click to load diff…
modified
lib/vigil/events/processor.ex
+12
−7
Click to load diff…
added
lib/vigil/symbolic.ex
+121
−0
Click to load diff…
added
lib/vigil/symbolic/native.ex
+49
−0
Click to load diff…
added
lib/vigil/symbolication/jvm.ex
+272
−0
Click to load diff…
added
lib/vigil/symbolication/native.ex
+440
−0
Click to load diff…
added
lib/vigil_web/api/v0/debug_file_controller.ex
+92
−0
Click to load diff…
modified
lib/vigil_web/chunk_multipart.ex
+27
−4
Click to load diff…
modified
lib/vigil_web/router.ex
+10
−0
Click to load diff…
added
native/vigil_symbolic/Cargo.lock
+1686
−0
Click to load diff…
added
native/vigil_symbolic/Cargo.toml
+62
−0
Click to load diff…
added
native/vigil_symbolic/src/lib.rs
+549
−0
Click to load diff…
added
priv/repo/migrations/20261202041259_create_debug_files.exs
+86
−0
Click to load diff…
modified
?
+0
−0
Click to load diff…
added
test/support/fixtures/native/crash.c
+33
−0
Click to load diff…
added
test/support/fixtures/native_fixtures.ex
+62
−0
Click to load diff…
added
test/support/fixtures/proguard/mapping.txt
+16
−0
Click to load diff…
added
test/vigil/symbolication/native_test.exs
+162
−0
Click to load diff…
added
test/vigil_web/api/debug_file_api_test.exs
+220
−0
Click to load diff…
added
test/vigil_web/sentry_cli_debug_files_test.exs
+137
−0
Click to load diff…
Parents: a7fc868