neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Cron monitors: check-ins, missed and timeout, incidents, monitor alerts
28e6625 · gmorell · 2026-12-02 02:55
Files changed
modified
config/config.exs
+2
−0
| 52 | 52 | {"17 3 * * *", Vigil.Partitions.MaintainWorker}, |
| 53 | 53 | # Alert rules against the issues events touched (Vigil.Alerts). |
| 54 | 54 | {"* * * * *", Vigil.Alerts.EvaluateWorker}, |
| 55 | + # Cron monitors: missed check-ins and timed-out runs (Vigil.Monitors). | |
| 56 | + {"* * * * *", Vigil.Monitors.TickWorker}, | |
| 55 | 57 | # Chunk blobs and assemble statuses past a day (Vigil.Artifacts). |
| 56 | 58 | {"41 3 * * *", Vigil.Artifacts.CleanupWorker} |
| 57 | 59 | ]} |
modified
lib/vigil/alerts.ex
+55
−17
| 216 | 216 | rules = |
| 217 | 217 | Repo.all( |
| 218 | 218 | from r in Rule, |
| 219 | − where: r.project_id == ^project_id and r.enabled, | |
| 219 | + where: r.project_id == ^project_id and r.enabled and r.kind != :monitor, | |
| 220 | 220 | preload: :recipients |
| 221 | 221 | ) |
| 222 | 222 |
| 287 | 287 | end) |
| 288 | 288 | end |
| 289 | 289 | |
| 290 | + @doc """ | |
| 291 | + Tells a project's enabled rules of `kind` (`:monitor`) a `message` | |
| 292 | + (`%{title:, text:, url:, level:}`): a notification per rule, delivered | |
| 293 | + to its recipients as issue alerts are. Returns the notifications. | |
| 294 | + """ | |
| 295 | + def notify(project_id, kind, %{title: _, text: _} = message) do | |
| 296 | + message = Map.new(message, fn {k, v} -> {to_string(k), v} end) | |
| 297 | + | |
| 298 | + Repo.all( | |
| 299 | + from r in Rule, | |
| 300 | + where: r.project_id == ^project_id and r.enabled and r.kind == ^kind, | |
| 301 | + preload: :recipients | |
| 302 | + ) | |
| 303 | + |> Enum.map(fn rule -> | |
| 304 | + {:ok, notification} = | |
| 305 | + Repo.transact(fn -> | |
| 306 | + notification = | |
| 307 | + Repo.insert!(%Notification{ | |
| 308 | + alert_rule_id: rule.id, | |
| 309 | + project_id: project_id, | |
| 310 | + message: message | |
| 311 | + }) | |
| 312 | + | |
| 313 | + deliver_to_recipients(rule, notification) | |
| 314 | + {:ok, notification} | |
| 315 | + end) | |
| 316 | + | |
| 317 | + notification | |
| 318 | + end) | |
| 319 | + end | |
| 320 | + | |
| 321 | + defp deliver_to_recipients(rule, notification) do | |
| 322 | + recipients = if rule.recipients == [], do: [%Recipient{type: :email}], else: rule.recipients | |
| 323 | + | |
| 324 | + for recipient <- recipients do | |
| 325 | + delivery = | |
| 326 | + Repo.insert!(%Delivery{ | |
| 327 | + notification_id: notification.id, | |
| 328 | + recipient_id: recipient.id, | |
| 329 | + project_id: rule.project_id, | |
| 330 | + type: Atom.to_string(recipient.type), | |
| 331 | + target: recipient.url | |
| 332 | + }) | |
| 333 | + | |
| 334 | + {:ok, _} = DeliverWorker.enqueue(delivery) | |
| 335 | + end | |
| 336 | + end | |
| 337 | + | |
| 338 | + @doc "A message notification's message, with atom keys, as webhooks and email take it." | |
| 339 | + def message_for(%Notification{message: %{} = m}) do | |
| 340 | + %{title: m["title"], text: m["text"], url: m["url"], level: m["level"] || "error"} | |
| 341 | + end | |
| 342 | + | |
| 290 | 343 | defp fire(rule, issue_ids, now) do |
| 291 | 344 | {:ok, notification} = |
| 292 | 345 | Repo.transact(fn -> |
| 312 | 365 | conflict_target: [:alert_rule_id, :issue_id] |
| 313 | 366 | ) |
| 314 | 367 | |
| 315 | − recipients = | |
| 316 | − if rule.recipients == [], do: [%Recipient{type: :email}], else: rule.recipients | |
| 317 | − | |
| 318 | − for recipient <- recipients do | |
| 319 | − delivery = | |
| 320 | − Repo.insert!(%Delivery{ | |
| 321 | − notification_id: notification.id, | |
| 322 | − recipient_id: recipient.id, | |
| 323 | − project_id: rule.project_id, | |
| 324 | − type: Atom.to_string(recipient.type), | |
| 325 | − target: recipient.url | |
| 326 | − }) | |
| 327 | − | |
| 328 | − {:ok, _} = DeliverWorker.enqueue(delivery) | |
| 329 | − end | |
| 330 | − | |
| 368 | + deliver_to_recipients(rule, notification) | |
| 331 | 369 | {:ok, notification} |
| 332 | 370 | end) |
| 333 | 371 |
modified
lib/vigil/alerts/deliver_worker.ex
+24
−0
| 41 | 41 | end |
| 42 | 42 | end |
| 43 | 43 | |
| 44 | + # A message (a monitor incident): the same text to every member, or to | |
| 45 | + # the webhook. | |
| 46 | + defp send_it(%Delivery{type: "email", notification: %{message: %{}} = n}) do | |
| 47 | + message = Alerts.message_for(n) | |
| 48 | + project = Repo.get!(Project, n.project_id) | |
| 49 | + | |
| 50 | + project | |
| 51 | + |> Alerts.email_recipients() | |
| 52 | + |> Enum.reduce_while(:ok, fn user, :ok -> | |
| 53 | + case user |> Vigil.Alerts.Email.message_alert(project, message) |> Vigil.Mailer.deliver() do | |
| 54 | + {:ok, _} -> {:cont, :ok} | |
| 55 | + {:error, reason} -> {:halt, {:error, {:mail, reason}}} | |
| 56 | + end | |
| 57 | + end) | |
| 58 | + end | |
| 59 | + | |
| 60 | + defp send_it(%Delivery{recipient: %Recipient{} = r, notification: %{message: %{}} = n}) do | |
| 61 | + Vigil.Alerts.Webhooks.deliver_message( | |
| 62 | + Alerts.webhook_recipient(r), | |
| 63 | + Alerts.message_for(n), | |
| 64 | + Alerts.webhook_opts() | |
| 65 | + ) | |
| 66 | + end | |
| 67 | + | |
| 44 | 68 | defp send_it(%Delivery{type: "email"} = d) do |
| 45 | 69 | alert = Alerts.alert_for(d.notification) |
| 46 | 70 | project = Repo.get!(Project, d.notification.project_id) |
modified
lib/vigil/alerts/email.ex
+9
−0
| 18 | 18 | |> html_body(html(alert)) |
| 19 | 19 | end |
| 20 | 20 | |
| 21 | + @doc "A plain alert (a monitor incident) for `user`: `message` from `Vigil.Alerts.message_for/1`." | |
| 22 | + def message_alert(user, project, message) do | |
| 23 | + new() | |
| 24 | + |> to(user.email) | |
| 25 | + |> from(sender()) | |
| 26 | + |> subject("[#{project.name}] #{message.title}") | |
| 27 | + |> text_body(message.text <> if(message.url, do: "\n\n#{message.url}\n", else: "\n")) | |
| 28 | + end | |
| 29 | + | |
| 21 | 30 | @doc "A test message, from the alerts page." |
| 22 | 31 | def test(user, project) do |
| 23 | 32 | new() |
modified
lib/vigil/alerts/notification.ex
+6
−1
| 1 | 1 | defmodule Vigil.Alerts.Notification do |
| 2 | − @moduledoc "One firing of a rule: the issues it was about. Its deliveries say where it went." | |
| 2 | + @moduledoc """ | |
| 3 | + One firing of a rule: the issues it was about, or a `message` (`title`, | |
| 4 | + `text`, `url`, `level`) for alerts that aren't about issues, such as a | |
| 5 | + monitor incident. Its deliveries say where it went. | |
| 6 | + """ | |
| 3 | 7 | use Vigil.Schema |
| 4 | 8 | |
| 5 | 9 | schema "alert_notifications" do |
| 6 | 10 | field :project_id, Ecto.UUID |
| 7 | 11 | field :issue_ids, {:array, Ecto.UUID}, default: [] |
| 12 | + field :message, :map | |
| 8 | 13 | |
| 9 | 14 | belongs_to :alert_rule, Vigil.Alerts.Rule |
| 10 | 15 | has_many :deliveries, Vigil.Alerts.Delivery |
modified
lib/vigil/alerts/rule.ex
+5
−2
Click to load diff…
modified
lib/vigil/ingest/processor.ex
+1
−0
Click to load diff…
added
lib/vigil/monitors.ex
+552
−0
Click to load diff…
added
lib/vigil/monitors/check_in.ex
+25
−0
Click to load diff…
added
lib/vigil/monitors/incident.ex
+17
−0
Click to load diff…
added
lib/vigil/monitors/monitor.ex
+108
−0
Click to load diff…
added
lib/vigil/monitors/processor.ex
+19
−0
Click to load diff…
added
lib/vigil/monitors/schedule.ex
+82
−0
Click to load diff…
added
lib/vigil/monitors/tick_worker.ex
+10
−0
Click to load diff…
modified
lib/vigil_web/live/project_live/alerts.ex
+4
−1
Click to load diff…
added
priv/repo/migrations/20261202025121_create_monitors.exs
+93
−0
Click to load diff…
added
test/vigil/monitors_test.exs
+271
−0
Click to load diff…
Parents: 5944a1f