Dogfooding: Vigil reports its errors and Oban crons through sentry-elixir, with a loop guard for ingest

98ad9a4 · gmorell · 2026-12-02 03:46

15 files +192 -1

Files changed

modified config/config.exs
+8 −0
@@ -80,6 +80,14 @@ config :vigil, Vigil.Partitions,
80 80 %{table: "session_users", kind: :weekly, ahead_days: 21, keep_days: 90}
81 81 ]
82 82
83 +# Self-reporting (Vigil.SelfReport): inert until VIGIL_SELF_DSN sets a DSN
84 +# (runtime.exs). Oban job errors and every cron job, as cron monitors.
85 +config :sentry,
86 + dsn: nil,
87 + enable_source_code_context: false,
88 + integrations: [oban: [capture_errors: true, cron: [enabled: true]]],
89 + before_send: {Vigil.SelfReport, :before_send}
90 +
83 91 # Configure the endpoint
84 92 config :vigil, VigilWeb.Endpoint,
85 93 url: [host: "localhost"],
modified config/runtime.exs
+9 −0
@@ -177,3 +177,12 @@ end
177 177 if System.get_env("METRICS_PUBLIC") in ~w(true 1) do
178 178 config :vigil, :metrics_public, true
179 179 end
180 +
181 +# Self-reporting: Vigil's own errors and cron jobs go to this DSN (a
182 +# project on this instance, or on another). Unset: nothing is sent.
183 +if dsn = System.get_env("VIGIL_SELF_DSN") do
184 + config :sentry,
185 + dsn: dsn,
186 + environment_name: System.get_env("VIGIL_SELF_ENVIRONMENT", "production"),
187 + release: "vigil@" <> to_string(Application.spec(:vigil, :vsn) || "dev")
188 +end
modified lib/vigil/application.ex
+3 −0
@@ -7,6 +7,9 @@ defmodule Vigil.Application do
7 7
8 8 @impl true
9 9 def start(_type, _args) do
10 + # Vigil's own errors to VIGIL_SELF_DSN, if set (Vigil.SelfReport).
11 + Vigil.SelfReport.attach()
12 +
10 13 children = [
11 14 VigilWeb.Telemetry,
12 15 Vigil.Repo,
added lib/vigil/self_report.ex
+90 −0
@@ -0,0 +1,90 @@
1 +defmodule Vigil.SelfReport do
2 + @moduledoc """
3 + Dogfooding: Vigil reports its own errors and Oban cron check-ins to a
4 + Vigil (itself, or another instance) through sentry-elixir, when
5 + `VIGIL_SELF_DSN` is set; without it the SDK sends nothing.
6 +
7 + * crashes and error logs, through `Sentry.LoggerHandler` (`attach/0`,
8 + at boot);
9 + * errors raised in the plug pipeline (`Sentry.PlugCapture` in the
10 + Endpoint);
11 + * Oban job errors, and every cron job as a cron monitor (the SDK's
12 + Oban integration).
13 +
14 + **The loop guard** (`before_send/1`): an event about ingest itself (the
15 + envelope edge, the buffer, the drain and its processors) is dropped.
16 + Reporting it would send an envelope through the very path that failed,
17 + which could fail again and report again, forever.
18 + """
19 +
20 + @ingest_modules ~w(
21 + Vigil.Ingest
22 + VigilWeb.IngestPlug
23 + Vigil.Events.Processor
24 + Vigil.Events.Normalize
25 + Vigil.Events.FeedbackProcessor
26 + Vigil.Performance.Processor
27 + Vigil.Logs.Processor
28 + Vigil.Monitors.Processor
29 + Vigil.ReleaseHealth.Processor
30 + )
31 +
32 + @ingest_path ~r{^/api/\d+/(envelope|store|security|minidump)/?$}
33 +
34 + @doc "Whether self-reporting is on (a DSN is configured)."
35 + def enabled?, do: not is_nil(Sentry.Config.dsn())
36 +
37 + @doc "Attaches sentry-elixir's logger handler, when self-reporting is on."
38 + def attach do
39 + if enabled?() do
40 + :logger.add_handler(:vigil_self_report, Sentry.LoggerHandler, %{
41 + config: %{metadata: [:request_id], capture_log_messages: false, level: :error}
42 + })
43 + end
44 +
45 + :ok
46 + end
47 +
48 + @doc "sentry-elixir's `before_send`: the event, or nil when it is about ingest."
49 + def before_send(%Sentry.Event{} = event) do
50 + if about_ingest?(event), do: nil, else: event
51 + end
52 +
53 + def before_send(event), do: event
54 +
55 + @doc false
56 + def about_ingest?(%Sentry.Event{} = event) do
57 + ingest_request?(event) or ingest_worker?(event) or ingest_frames?(event)
58 + end
59 +
60 + defp ingest_request?(%{request: %{url: url}}) when is_binary(url) do
61 + Regex.match?(@ingest_path, URI.parse(url).path || "")
62 + end
63 +
64 + defp ingest_request?(_), do: false
65 +
66 + defp ingest_worker?(%{tags: tags}) do
67 + worker = to_string(Map.get(tags, :oban_worker) || Map.get(tags, "oban_worker") || "")
68 + ingest_module?(worker)
69 + end
70 +
71 + defp ingest_frames?(%{exception: exceptions}) do
72 + Enum.any?(List.wrap(exceptions), fn
73 + %{stacktrace: %{frames: frames}} when is_list(frames) ->
74 + Enum.any?(frames, &ingest_module?(inspect_module(&1.module)))
75 +
76 + _ ->
77 + false
78 + end)
79 + end
80 +
81 + defp inspect_module(nil), do: ""
82 + defp inspect_module(mod) when is_atom(mod), do: inspect(mod)
83 + defp inspect_module(mod), do: to_string(mod)
84 +
85 + defp ingest_module?(""), do: false
86 +
87 + defp ingest_module?(name) do
88 + Enum.any?(@ingest_modules, &(name == &1 or String.starts_with?(name, &1 <> ".")))
89 + end
90 +end
modified lib/vigil_web/endpoint.ex
+6 −0
@@ -1,4 +1,7 @@
1 1 defmodule VigilWeb.Endpoint do
2 + # Errors raised anywhere below are reported (Vigil.SelfReport), when a
3 + # self-report DSN is set; before Phoenix.Endpoint, as sentry-elixir asks.
4 + use Sentry.PlugCapture
2 5 use Phoenix.Endpoint, otp_app: :vigil
3 6
4 7 # The session will be stored in the cookie and signed,
@@ -58,6 +61,9 @@ defmodule VigilWeb.Endpoint do
58 61 pass: ["*/*"],
59 62 json_decoder: Phoenix.json_library()
60 63
64 + # The request's details on any error reported from here on.
65 + plug Sentry.PlugContext
66 +
61 67 plug Plug.MethodOverride
62 68 plug Plug.Head
63 69 plug Plug.Session, @session_options
modified lib/vigil_web/health_plug.ex
+3 −1
@@ -91,7 +91,9 @@ defmodule VigilWeb.HealthPlug do
91 91 pending =
92 92 Vigil.Repo.aggregate(
93 93 from(i in Vigil.Ingest.BufferedItem, where: i.state == :pending),
94 − :count, timeout: 2_000)
94 + :count,
95 + timeout: 2_000
96 + )
95 97
96 98 if pending < @backlog_warn,
97 99 do: :ok,
modified mix.exs
+3 −0
@@ -81,6 +81,9 @@ defmodule Vigil.MixProject do
81 81 {:telemetry_metrics, "~> 1.0"},
82 82 # /metrics in Prometheus format (VigilWeb.Metrics).
83 83 {:telemetry_metrics_prometheus_core, "~> 1.2"},
84 + # Vigil reports its own errors and crons to a Vigil (VIGIL_SELF_DSN;
85 + # Vigil.SelfReport).
86 + {:sentry, "~> 14.1"},
84 87 {:telemetry_poller, "~> 1.0"},
85 88 {:gettext, "~> 1.0"},
86 89 {:jason, "~> 1.2"},
modified mix.lock
+2 −0
@@ -26,6 +26,7 @@
26 26 "mint": {:hex, :mint, "1.11.0", "a713551624815c0435237b93d90ea8b9b14254690c66f732d0ef8930f76ff1d9", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "c6279ba2d6aa3a383a1d4cfbe7b59f42e6efd400f58d8e2acfeac48a438693ab"},
27 27 "neiam_auth": {:git, "https://gg.neiam.co/neiam/neiam_auth.git", "f58a6a74e89f26f91f51dc5bc8977c7a4d9bc47e", [tag: "v0.4.0"]},
28 28 "nimble_options": {:hex, :nimble_options, "1.2.0", "73e5c5fecb08e59f390e0f59c2320b585d72470dbdd9230a469e06881b9dd4a8", [:mix], [], "hexpm", "3a784a75ef9e407619c8c9e98239b1acd29216deb737044a8c1063e2671ce6d9"},
29 + "nimble_ownership": {:hex, :nimble_ownership, "1.0.2", "fa8a6f2d8c592ad4d79b2ca617473c6aefd5869abfa02563a77682038bf916cf", [:mix], [], "hexpm", "098af64e1f6f8609c6672127cfe9e9590a5d3fcdd82bc17a377b8692fd81a879"},
29 30 "nimble_pool": {:hex, :nimble_pool, "1.2.0", "7f7e19c4fc7e960ffd405b0bcaec73e29a96e249a806b386f57e18192f24fa4f", [:mix], [], "hexpm", "bc442e72578c21842589fa8e5886b6175fa5245fdbbad35fc08db277ac2b2c0d"},
30 31 "oban": {:hex, :oban, "2.24.2", "2309cbe71f096cfb00ec8102a0beaaaaf54b2f449c63ce6cc9ce1fa46ea8fc4b", [:mix], [{:ecto_sql, "~> 3.10", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:ecto_sqlite3, "~> 0.9", [hex: :ecto_sqlite3, repo: "hexpm", optional: true]}, {:igniter, "~> 0.5", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.20", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 1.3", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "605a3b61d554ab416e0cf462b5ea060f7d19ae3dbf1516ec6b5ee30aeb1a5d0b"},
31 32 "oidcc": {:hex, :oidcc, "3.9.2", "6f3e6e574e8d01dcc63a27b87e0790bbe6700bea82b4cf74dbe52ba26378da15", [:mix, :rebar3], [{:igniter, ">= 0.6.3 and < 0.9.0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jose, "~> 1.11", [hex: :jose, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.2", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_registry, "~> 0.3.1", [hex: :telemetry_registry, repo: "hexpm", optional: false]}], "hexpm", "e469213a2518904e805ff4f8325a405ac740a74a381e711968d32636c10d9c9b"},
@@ -42,6 +43,7 @@
42 43 "postgrex": {:hex, :postgrex, "0.23.0", "f106e1429559f0db8799c250279ee5fa16e364a39380cd9523e725df3625b235", [:mix], [{:db_connection, "~> 2.9", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "f001ab3fd56f5ef2b8b3cd4a512005614fcaeccca068ddb29e57d338a7f104ec"},
43 44 "req": {:hex, :req, "0.7.5", "0c3293cbf7717aba20b1b1b3ea13fe8d3fdf6f1557490f390c3f1ef6dd9db955", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:finch, "~> 0.21", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "576853c4bf403faa3c1fa5d093e0759227083b78f8a6864c67d62c0a9c45514d"},
44 45 "rustler": {:hex, :rustler, "0.38.0", "7a8906998ff0d28e3021c0a73264abcda719bda344b2e58307c6805b0f87c9b4", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "704c03c1bf66be12b031c5a389347b91c81c5cb819a24b068b0de36fe4a5652a"},
46 + "sentry": {:hex, :sentry, "14.1.0", "7299ad220029c7d972ef7fea571fbe0a910655fe3f0b30aef8681b1730581359", [:mix], [{:finch, "~> 0.21", [hex: :finch, repo: "hexpm", optional: true]}, {:hackney, ">= 1.8.0 and < 5.0.0", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_ownership, "~> 1.0", [hex: :nimble_ownership, repo: "hexpm", optional: false]}, {:opentelemetry, ">= 0.0.0", [hex: :opentelemetry, repo: "hexpm", optional: true]}, {:opentelemetry_api, ">= 0.0.0", [hex: :opentelemetry_api, repo: "hexpm", optional: true]}, {:opentelemetry_exporter, ">= 0.0.0", [hex: :opentelemetry_exporter, repo: "hexpm", optional: true]}, {:opentelemetry_semantic_conventions, ">= 0.0.0", [hex: :opentelemetry_semantic_conventions, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6", [hex: :phoenix, repo: "hexpm", optional: true]}, {:phoenix_live_view, "~> 0.20 or ~> 1.0", [hex: :phoenix_live_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}, {:telemetry_poller, "~> 0.5 or ~> 1.0", [hex: :telemetry_poller, repo: "hexpm", optional: true]}], "hexpm", "f2ebc73fac06d0595347a4bb474dcb9230783dcd6cfcfe62ac10fe243d33743d"},
45 47 "swoosh": {:hex, :swoosh, "1.30.0", "a0904169dce7777d4decb80fb01b51eb317e6d140e9f6f00a8e2562426ae06e8", [:mix], [{:bandit, ">= 1.0.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, ">= 1.9.0 and < 5.0.0", [hex: :hackney, repo: "hexpm", optional: true]}, {:idna, ">= 6.0.0 and < 9.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mua, "~> 0.2.3", [hex: :mua, repo: "hexpm", optional: true]}, {:multipart, "~> 0.4", [hex: :multipart, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:req, "~> 0.5.10 or ~> 0.6 or ~> 1.0", [hex: :req, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d1b553f5264ae39fb299e0da12e01e33aaba49c2509757065ca3aef06e659b0c"},
46 48 "tailwind": {:hex, :tailwind, "0.5.2", "d8bc230361fd3e14b495043c364aefddfac2ad4bf2eada380f458e9a15cb2e20", [:mix], [], "hexpm", "e0e5fd9799fb9231b97e52ce074d9ff509043d1946713a71fbc608e81eff7e66"},
47 49 "telemetry": {:hex, :telemetry, "1.4.3", "d0333fa869361e6833d6ff8e806ff4d8553fe1332fcce0af86c7381951a1dc2f", [:rebar3], [], "hexpm", "17a63581cf2e832a238287f5fe6811e8ca1f333249f1376ad653164a3c4770d4"},
added test/vigil/self_report_test.exs
+68 −0
@@ -0,0 +1,68 @@
1 +defmodule Vigil.SelfReportTest do
2 + @moduledoc """
3 + Vigil reporting to itself through sentry-elixir (the real SDK, as a
4 + dependency): an error lands as an issue in a Vigil project; one about
5 + the ingest path is dropped by the loop guard.
6 + """
7 + use VigilWeb.ConnCase, async: false
8 +
9 + import Vigil.AccountsFixtures
10 + import Vigil.OrganizationsFixtures
11 +
12 + alias Vigil.{Projects, SelfReport}
13 +
14 + setup do
15 + owner = user_fixture()
16 + org = organization_fixture(owner)
17 + project = project_fixture(org_scope(owner, org), %{"name" => "vigil"})
18 + [key] = Projects.list_keys(project)
19 +
20 + server = start_supervised!({Bandit, plug: VigilWeb.Endpoint, port: 0, ip: :loopback})
21 + {:ok, {_ip, port}} = ThousandIsland.listener_info(server)
22 + hex = String.replace(key.public_key, "-", "")
23 +
24 + Sentry.put_config(:dsn, "http://#{hex}@127.0.0.1:#{port}/#{project.dsn_id}")
25 + on_exit(fn -> Sentry.put_config(:dsn, nil) end)
26 + %{project: project}
27 + end
28 +
29 + test "Vigil's own error becomes an issue in its project", %{project: project} do
30 + assert SelfReport.enabled?()
31 +
32 + assert {:ok, _id} =
33 + Sentry.capture_exception(%RuntimeError{message: "self-report works"},
34 + stacktrace: [
35 + {Vigil.Alerts, :evaluate, 1, [file: ~c"lib/vigil/alerts.ex", line: 1]}
36 + ],
37 + result: :sync
38 + )
39 +
40 + Vigil.Ingest.Drain.drain(project.id)
41 + assert [%{title: "RuntimeError: self-report works"}] = Vigil.Issues.list_issues(project.id)
42 + end
43 +
44 + test "errors about ingest are dropped before they are sent", %{project: project} do
45 + assert :excluded =
46 + Sentry.capture_exception(%RuntimeError{message: "drain broke"},
47 + stacktrace: [
48 + {Vigil.Ingest.Drain, :drain, 1, [file: ~c"lib/vigil/ingest/drain.ex", line: 1]}
49 + ],
50 + result: :sync
51 + )
52 +
53 + assert :excluded =
54 + Sentry.capture_message("envelope edge failed",
55 + request: %{url: "https://vigil.example/api/12/envelope/"},
56 + result: :sync
57 + )
58 +
59 + assert :excluded =
60 + Sentry.capture_message("drain job failed",
61 + tags: %{oban_worker: "Vigil.Ingest.DrainWorker"},
62 + result: :sync
63 + )
64 +
65 + Vigil.Ingest.Drain.drain(project.id)
66 + assert Vigil.Issues.list_issues(project.id) == []
67 + end
68 +end

Parents: e8c687c