neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Slice 10: release health
0f6e20c · gmorell · 2026-12-02 03:34
Files changed
modified
ROADMAP.md
+10
−1
| 448 | 448 | - The trace page lists the trace's logs. |
| 449 | 449 | - Tested with the real sentry-elixir 14.1 logger handler (`config :sentry, logs: [level: :info]`) and sentry-python 3.0.2, which sends logs by default; `enable_logs` was removed in 3.x. |
| 450 | 450 | |
| 451 | −### 10. Release health · small-medium | |
| 451 | +### 10. Release health · small-medium · **done** | |
| 452 | 452 | - `session` and `sessions` items roll up into per-release, per-environment, per-hour crash-free counts. |
| 453 | 453 | - Release pages show crash-free users and sessions and adoption. |
| 454 | +- **As built:** | |
| 455 | + - Only sentry-python sends sessions; sentry-elixir has none. | |
| 456 | + - `session` items (application mode): `init` counts a start, and a final status counts once as `exited` (or `errored` with errors), `abnormal` or `crashed`. | |
| 457 | + - `sessions` items (request mode): each aggregate bucket's total counts as started. | |
| 458 | + - Counts go into `session_counts` per (project, release, environment, hour), partitioned weekly and kept 400 days. Users go into `session_users` per day, as a SHA-256 of the SDK's `did` with a crashed flag, kept 90 days. | |
| 459 | + - Sessions without a release, or over 5 days old, are dropped; old ones count as a `:server` outcome. One that started before this week's partition counts in its arrival hour. | |
| 460 | + - Crash-free sessions = 1 − crashed/started. Crash-free users counts per-day users. Adoption = the release's share of the project's sessions over 24h. | |
| 461 | + - The releases list has a crash-free · adoption column. A release's page has health tiles and an hourly sessions chart, with crashes in the error colour. | |
| 462 | + - Tested with the real sentry-python 3.0.2: request-mode aggregates plus an application session that crashes. | |
| 454 | 463 | |
| 455 | 464 | ### 11. Retention and operations · medium |
| 456 | 465 | - **Retention** per type, settable per org: events and logs 90 days, releases 365 days. Old partitions are dropped. Orphaned issues, blobs and bundles are swept, and soft-deleted orgs and projects are removed by Oban jobs. |
modified
config/config.exs
+3
−1
| 73 | 73 | %{table: "spans", kind: :uuid7_daily, ahead_days: 3, keep_days: 14}, |
| 74 | 74 | %{table: "transaction_counts", kind: :weekly, ahead_days: 21, keep_days: 90}, |
| 75 | 75 | %{table: "uptime_checks", kind: :uuid7_daily, ahead_days: 3, keep_days: 30}, |
| 76 | − %{table: "log_events", kind: :uuid7_daily, ahead_days: 3, keep_days: 30} | |
| 76 | + %{table: "log_events", kind: :uuid7_daily, ahead_days: 3, keep_days: 30}, | |
| 77 | + %{table: "session_counts", kind: :weekly, ahead_days: 21, keep_days: 400}, | |
| 78 | + %{table: "session_users", kind: :weekly, ahead_days: 21, keep_days: 90} | |
| 77 | 79 | ] |
| 78 | 80 | |
| 79 | 81 | # Configure the endpoint |
modified
lib/vigil/ingest/processor.ex
+2
−0
| 20 | 20 | "span" => Vigil.Performance.Processor, |
| 21 | 21 | "check_in" => Vigil.Monitors.Processor, |
| 22 | 22 | "log" => Vigil.Logs.Processor, |
| 23 | + "session" => Vigil.ReleaseHealth.Processor, | |
| 24 | + "sessions" => Vigil.ReleaseHealth.Processor, | |
| 23 | 25 | "user_report" => Vigil.Events.FeedbackProcessor, |
| 24 | 26 | "feedback" => Vigil.Events.FeedbackProcessor |
| 25 | 27 | } |
added
lib/vigil/release_health.ex
+150
−0
| 1 | +defmodule Vigil.ReleaseHealth do | |
| 2 | + @moduledoc """ | |
| 3 | + Release health from sessions (`Vigil.ReleaseHealth.Processor` writes | |
| 4 | + them): crash-free sessions and users, and adoption, per release of a | |
| 5 | + project. | |
| 6 | + | |
| 7 | + * **Crash-free sessions**: 1 − crashed ÷ started. | |
| 8 | + * **Crash-free users**: 1 − users with a crashed session ÷ users (by | |
| 9 | + the SDK's distinct id, counted per day and summed over the window, | |
| 10 | + so a user on two days counts twice, as Sentry's daily buckets do). | |
| 11 | + * **Adoption**: the release's share of the project's sessions over | |
| 12 | + the last 24 hours (in the same environment, when one is given). | |
| 13 | + | |
| 14 | + Rates are nil when there are no sessions. | |
| 15 | + """ | |
| 16 | + | |
| 17 | + import Ecto.Query | |
| 18 | + | |
| 19 | + alias Vigil.Repo | |
| 20 | + | |
| 21 | + @doc """ | |
| 22 | + Health of `release` in a project over the last `days` (90, within | |
| 23 | + retention), optionally one `environment`: `%{sessions:, crashed:, | |
| 24 | + errored:, abnormal:, crash_free_sessions:, users:, crashed_users:, | |
| 25 | + crash_free_users:, adoption:}`. | |
| 26 | + """ | |
| 27 | + def summary(project_id, release, opts \\ []) do | |
| 28 | + summaries(project_id, [release], opts) |> Map.fetch!(release) | |
| 29 | + end | |
| 30 | + | |
| 31 | + @doc "`summary/3` for many releases of one project at once: `%{version => summary}`." | |
| 32 | + def summaries(project_id, releases, opts \\ []) do | |
| 33 | + days = Keyword.get(opts, :days, 90) | |
| 34 | + env = opts[:environment] | |
| 35 | + since = NaiveDateTime.add(NaiveDateTime.utc_now(), -days * 86_400) | |
| 36 | + | |
| 37 | + counts = | |
| 38 | + from(s in "session_counts", | |
| 39 | + where: | |
| 40 | + s.project_id == type(^project_id, :binary_id) and s.release in ^releases and | |
| 41 | + s.hour >= ^since, | |
| 42 | + group_by: s.release, | |
| 43 | + select: | |
| 44 | + {s.release, | |
| 45 | + %{ | |
| 46 | + sessions: type(sum(s.started), :integer), | |
| 47 | + crashed: type(sum(s.crashed), :integer), | |
| 48 | + errored: type(sum(s.errored), :integer), | |
| 49 | + abnormal: type(sum(s.abnormal), :integer) | |
| 50 | + }} | |
| 51 | + ) | |
| 52 | + |> env_filter(env) | |
| 53 | + |> Repo.all() | |
| 54 | + |> Map.new() | |
| 55 | + | |
| 56 | + users = | |
| 57 | + from(u in "session_users", | |
| 58 | + where: | |
| 59 | + u.project_id == type(^project_id, :binary_id) and u.release in ^releases and | |
| 60 | + u.date >= ^NaiveDateTime.to_date(since), | |
| 61 | + group_by: u.release, | |
| 62 | + select: | |
| 63 | + {u.release, | |
| 64 | + %{ | |
| 65 | + users: count(), | |
| 66 | + crashed_users: | |
| 67 | + type(sum(fragment("CASE WHEN ? THEN 1 ELSE 0 END", u.crashed)), :integer) | |
| 68 | + }} | |
| 69 | + ) | |
| 70 | + |> env_filter(env) | |
| 71 | + |> Repo.all() | |
| 72 | + |> Map.new() | |
| 73 | + | |
| 74 | + adoption = adoption(project_id, releases, env) | |
| 75 | + | |
| 76 | + Map.new(releases, fn r -> | |
| 77 | + c = Map.get(counts, r, %{sessions: 0, crashed: 0, errored: 0, abnormal: 0}) | |
| 78 | + u = Map.get(users, r, %{users: 0, crashed_users: 0}) | |
| 79 | + | |
| 80 | + {r, | |
| 81 | + c | |
| 82 | + |> Map.merge(u) | |
| 83 | + |> Map.merge(%{ | |
| 84 | + crash_free_sessions: rate(c.sessions, c.crashed), | |
| 85 | + crash_free_users: rate(u.users, u.crashed_users), | |
| 86 | + adoption: Map.get(adoption, r) | |
| 87 | + })} | |
| 88 | + end) | |
| 89 | + end | |
| 90 | + | |
| 91 | + defp env_filter(query, nil), do: query | |
| 92 | + defp env_filter(query, env), do: where(query, [s], s.environment == ^env) | |
| 93 | + | |
| 94 | + defp rate(0, _), do: nil | |
| 95 | + defp rate(total, bad), do: 1 - bad / total | |
| 96 | + | |
| 97 | + # Each release's share of the project's sessions over the last day. | |
| 98 | + defp adoption(project_id, releases, env) do | |
| 99 | + since = NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400) | |
| 100 | + | |
| 101 | + rows = | |
| 102 | + from(s in "session_counts", | |
| 103 | + where: s.project_id == type(^project_id, :binary_id) and s.hour >= ^since, | |
| 104 | + group_by: s.release, | |
| 105 | + select: {s.release, type(sum(s.started), :integer)} | |
| 106 | + ) | |
| 107 | + |> env_filter(env) | |
| 108 | + |> Repo.all() | |
| 109 | + | |
| 110 | + total = rows |> Enum.map(&elem(&1, 1)) |> Enum.sum() | |
| 111 | + | |
| 112 | + if total == 0, | |
| 113 | + do: %{}, | |
| 114 | + else: | |
| 115 | + rows | |
| 116 | + |> Enum.filter(&(elem(&1, 0) in releases)) | |
| 117 | + |> Map.new(fn {r, n} -> {r, n / total} end) | |
| 118 | + end | |
| 119 | + | |
| 120 | + @doc """ | |
| 121 | + A release's sessions hour by hour over the last `hours` (24), oldest | |
| 122 | + first, empty hours included: `[%{hour:, sessions:, crashed:}]`. | |
| 123 | + """ | |
| 124 | + def hourly(project_id, release, hours \\ 24) do | |
| 125 | + now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) | |
| 126 | + since = NaiveDateTime.add(%{now | minute: 0, second: 0}, -(hours - 1) * 3600) | |
| 127 | + | |
| 128 | + rows = | |
| 129 | + Repo.all( | |
| 130 | + from s in "session_counts", | |
| 131 | + where: | |
| 132 | + s.project_id == type(^project_id, :binary_id) and s.release == ^release and | |
| 133 | + s.hour >= ^since, | |
| 134 | + group_by: s.hour, | |
| 135 | + select: {s.hour, type(sum(s.started), :integer), type(sum(s.crashed), :integer)} | |
| 136 | + ) | |
| 137 | + |> Map.new(fn {h, n, c} -> {NaiveDateTime.truncate(h, :second), {n, c}} end) | |
| 138 | + | |
| 139 | + for i <- 0..(hours - 1) do | |
| 140 | + hour = NaiveDateTime.add(since, i * 3600) | |
| 141 | + {n, c} = Map.get(rows, hour, {0, 0}) | |
| 142 | + %{hour: DateTime.from_naive!(hour, "Etc/UTC"), sessions: n, crashed: c} | |
| 143 | + end | |
| 144 | + end | |
| 145 | + | |
| 146 | + @doc "Whether a project has any sessions (so pages can hide empty health)." | |
| 147 | + def any_sessions?(project_id) do | |
| 148 | + Repo.exists?(from s in "session_counts", where: s.project_id == type(^project_id, :binary_id)) | |
| 149 | + end | |
| 150 | +end |
added
lib/vigil/release_health/processor.ex
+188
−0
| 1 | +defmodule Vigil.ReleaseHealth.Processor do | |
| 2 | + @moduledoc """ | |
| 3 | + `session` and `sessions` items (sentry-python; sentry-elixir sends none) | |
| 4 | + into hourly counts per project, release and environment, and distinct | |
| 5 | + users per day: | |
| 6 | + | |
| 7 | + * `session`: one session's update. `init: true` starts it (counted in | |
| 8 | + `started`, its user noted); a final status counts it once in | |
| 9 | + `exited` (or `errored` when it had errors), `abnormal` or `crashed`. | |
| 10 | + A crashed session marks its user crashed for the day. | |
| 11 | + * `sessions`: an SDK's own aggregate (request mode): | |
| 12 | + `{"attrs": {release, environment}, "aggregates": [{started, did?, | |
| 13 | + exited, errored, abnormal, crashed}]}`; each bucket's total is | |
| 14 | + started. | |
| 15 | + | |
| 16 | + Sessions without a release are dropped, as Sentry does; so are ones | |
| 17 | + that started more than 5 days ago (a `:server` outcome, `session`, | |
| 18 | + `timestamp`). A session that started before this week's partition | |
| 19 | + counts in the hour it arrived. | |
| 20 | + """ | |
| 21 | + @behaviour Vigil.Ingest.Processor | |
| 22 | + | |
| 23 | + alias Vigil.Ingest.{BufferedItem, Outcomes} | |
| 24 | + alias Vigil.Repo | |
| 25 | + | |
| 26 | + @max_age_seconds 5 * 86_400 | |
| 27 | + | |
| 28 | + @impl true | |
| 29 | + def process(%BufferedItem{type: "session"} = item) do | |
| 30 | + with {:ok, %{} = s} <- Jason.decode(item.payload), | |
| 31 | + release when is_binary(release) <- release(s["attrs"]), | |
| 32 | + {:ok, hour, date} <- bucket(item, s["started"]) do | |
| 33 | + env = environment(s["attrs"]) | |
| 34 | + status = s["status"] | |
| 35 | + | |
| 36 | + counts = | |
| 37 | + %{started: if(s["init"] == true, do: 1, else: 0)} | |
| 38 | + |> Map.merge(final(status, s["errors"])) | |
| 39 | + | |
| 40 | + if Enum.any?(counts, fn {_, n} -> n > 0 end) do | |
| 41 | + add_counts(item.project_id, release, env, hour, counts) | |
| 42 | + end | |
| 43 | + | |
| 44 | + did = s["did"] | |
| 45 | + | |
| 46 | + if is_binary(did) and did != "" and (s["init"] == true or status == "crashed") do | |
| 47 | + add_user(item.project_id, release, env, date, did, status == "crashed") | |
| 48 | + end | |
| 49 | + | |
| 50 | + :ok | |
| 51 | + else | |
| 52 | + {:drop, :too_old} -> | |
| 53 | + Outcomes.record(item, :server, "session", "timestamp", 1, DateTime.utc_now()) | |
| 54 | + :ok | |
| 55 | + | |
| 56 | + _ -> | |
| 57 | + :ok | |
| 58 | + end | |
| 59 | + end | |
| 60 | + | |
| 61 | + def process(%BufferedItem{type: "sessions"} = item) do | |
| 62 | + with {:ok, %{"aggregates" => aggs} = s} when is_list(aggs) <- Jason.decode(item.payload), | |
| 63 | + release when is_binary(release) <- release(s["attrs"]) do | |
| 64 | + env = environment(s["attrs"]) | |
| 65 | + | |
| 66 | + for %{} = agg <- Enum.take(aggs, 1000) do | |
| 67 | + case bucket(item, agg["started"]) do | |
| 68 | + {:ok, hour, date} -> | |
| 69 | + counts = | |
| 70 | + for k <- ~w(exited errored abnormal crashed), into: %{} do | |
| 71 | + {String.to_existing_atom(k), count(agg[k])} | |
| 72 | + end | |
| 73 | + | |
| 74 | + started = counts |> Map.values() |> Enum.sum() | |
| 75 | + add_counts(item.project_id, release, env, hour, Map.put(counts, :started, started)) | |
| 76 | + | |
| 77 | + if is_binary(agg["did"]) and agg["did"] != "" do | |
| 78 | + add_user(item.project_id, release, env, date, agg["did"], counts.crashed > 0) | |
| 79 | + end | |
| 80 | + | |
| 81 | + {:drop, :too_old} -> | |
| 82 | + Outcomes.record(item, :server, "session", "timestamp", 1, DateTime.utc_now()) | |
| 83 | + | |
| 84 | + :error -> | |
| 85 | + :ok | |
| 86 | + end | |
| 87 | + end | |
| 88 | + | |
| 89 | + :ok | |
| 90 | + else | |
| 91 | + _ -> :ok | |
| 92 | + end | |
| 93 | + end | |
| 94 | + | |
| 95 | + # What a final status adds; nothing for "ok" (an update mid-session). | |
| 96 | + defp final("crashed", _), do: %{crashed: 1} | |
| 97 | + defp final("abnormal", _), do: %{abnormal: 1} | |
| 98 | + defp final("errored", _), do: %{errored: 1} | |
| 99 | + defp final("exited", errors) when is_integer(errors) and errors > 0, do: %{errored: 1} | |
| 100 | + defp final("exited", _), do: %{exited: 1} | |
| 101 | + defp final(_, _), do: %{} | |
| 102 | + | |
| 103 | + defp count(n) when is_integer(n) and n > 0, do: n | |
| 104 | + defp count(_), do: 0 | |
| 105 | + | |
| 106 | + defp release(%{"release" => r}) when is_binary(r) and r != "", do: String.slice(r, 0, 250) | |
| 107 | + defp release(_), do: nil | |
| 108 | + | |
| 109 | + defp environment(%{"environment" => e}) when is_binary(e), do: String.slice(e, 0, 255) | |
| 110 | + defp environment(_), do: "" | |
| 111 | + | |
| 112 | + defp bucket(item, started) do | |
| 113 | + now = DateTime.utc_now() | |
| 114 | + | |
| 115 | + case Vigil.Performance.Normalize.time(started) do | |
| 116 | + nil -> | |
| 117 | + :error | |
| 118 | + | |
| 119 | + at -> | |
| 120 | + cond do | |
| 121 | + DateTime.diff(now, at) > @max_age_seconds -> | |
| 122 | + {:drop, :too_old} | |
| 123 | + | |
| 124 | + DateTime.compare(at, week_start(now)) == :lt or DateTime.diff(at, now) > 3600 -> | |
| 125 | + arrived = item.received_at || now | |
| 126 | + {:ok, hour(arrived), DateTime.to_date(arrived)} | |
| 127 | + | |
| 128 | + true -> | |
| 129 | + {:ok, hour(at), DateTime.to_date(at)} | |
| 130 | + end | |
| 131 | + end | |
| 132 | + end | |
| 133 | + | |
| 134 | + defp week_start(now) do | |
| 135 | + date = DateTime.to_date(now) | |
| 136 | + monday = Date.add(date, 1 - Date.day_of_week(date)) | |
| 137 | + DateTime.new!(monday, ~T[00:00:00], "Etc/UTC") | |
| 138 | + end | |
| 139 | + | |
| 140 | + defp hour(dt), | |
| 141 | + do: | |
| 142 | + dt | |
| 143 | + |> DateTime.truncate(:second) | |
| 144 | + |> Map.merge(%{minute: 0, second: 0}) | |
| 145 | + |> DateTime.to_naive() | |
| 146 | + | |
| 147 | + defp add_counts(project_id, release, env, hour, counts) do | |
| 148 | + c = fn k -> Map.get(counts, k, 0) end | |
| 149 | + | |
| 150 | + Repo.query!( | |
| 151 | + """ | |
| 152 | + INSERT INTO session_counts AS s | |
| 153 | + (project_id, release, environment, hour, started, exited, errored, abnormal, crashed) | |
| 154 | + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) | |
| 155 | + ON CONFLICT (project_id, release, environment, hour) DO UPDATE SET | |
| 156 | + started = s.started + EXCLUDED.started, | |
| 157 | + exited = s.exited + EXCLUDED.exited, | |
| 158 | + errored = s.errored + EXCLUDED.errored, | |
| 159 | + abnormal = s.abnormal + EXCLUDED.abnormal, | |
| 160 | + crashed = s.crashed + EXCLUDED.crashed | |
| 161 | + """, | |
| 162 | + [ | |
| 163 | + Ecto.UUID.dump!(project_id), | |
| 164 | + release, | |
| 165 | + env, | |
| 166 | + hour, | |
| 167 | + c.(:started), | |
| 168 | + c.(:exited), | |
| 169 | + c.(:errored), | |
| 170 | + c.(:abnormal), | |
| 171 | + c.(:crashed) | |
| 172 | + ] | |
| 173 | + ) | |
| 174 | + end | |
| 175 | + | |
| 176 | + # Users are kept as a hash of the SDK's distinct id: counted, never shown. | |
| 177 | + defp add_user(project_id, release, env, date, did, crashed?) do | |
| 178 | + Repo.query!( | |
| 179 | + """ | |
| 180 | + INSERT INTO session_users AS u (project_id, release, environment, date, did_hash, crashed) | |
| 181 | + VALUES ($1, $2, $3, $4, $5, $6) | |
| 182 | + ON CONFLICT (project_id, release, environment, date, did_hash) DO UPDATE SET | |
| 183 | + crashed = u.crashed OR EXCLUDED.crashed | |
| 184 | + """, | |
| 185 | + [Ecto.UUID.dump!(project_id), release, env, date, :crypto.hash(:sha256, did), crashed?] | |
| 186 | + ) | |
| 187 | + end | |
| 188 | +end |
modified
lib/vigil_web/live/release_live/index.ex
+29
−4
Click to load diff…
modified
lib/vigil_web/live/release_live/show.ex
+66
−0
Click to load diff…
added
priv/repo/migrations/20261202033054_create_release_health.exs
+40
−0
Click to load diff…
added
test/vigil/release_health_test.exs
+110
−0
Click to load diff…
modified
test/vigil_web/live/release_live_test.exs
+56
−0
Click to load diff…
added
test/vigil_web/sdk_sessions_conformance_test.exs
+83
−0
Click to load diff…
Parents: 08898fc