neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions running · Badges · Packages
Log in to mark this repository.
Performance: spans from transactions and span v2, transaction groups, traces
0750f91 · gmorell · 2026-12-02 02:35
Files changed
modified
config/config.exs
+4
−1
| 64 | 64 | %{table: "issue_events", kind: :uuid7_daily, ahead_days: 3, keep_days: 90}, |
| 65 | 65 | %{table: "issue_tags", kind: :weekly, ahead_days: 21, keep_days: 90}, |
| 66 | 66 | %{table: "issue_counts", kind: :weekly, ahead_days: 21, keep_days: 90}, |
| 67 | − %{table: "project_counts", kind: :weekly, ahead_days: 21, keep_days: 400} | |
| 67 | + %{table: "project_counts", kind: :weekly, ahead_days: 21, keep_days: 400}, | |
| 68 | + # Spans are many and short-lived; their rollups last longer. | |
| 69 | + %{table: "spans", kind: :uuid7_daily, ahead_days: 3, keep_days: 14}, | |
| 70 | + %{table: "transaction_counts", kind: :weekly, ahead_days: 21, keep_days: 90} | |
| 68 | 71 | ] |
| 69 | 72 | |
| 70 | 73 | # Configure the endpoint |
modified
lib/vigil/events/processor.ex
+9
−1
| 322 | 322 | grouping_config: config, |
| 323 | 323 | hashes: hashes |> Enum.filter(&(elem(&1, 0) == config)) |> Enum.map(&elem(&1, 1)), |
| 324 | 324 | tags: tags, |
| 325 | − data: data | |
| 325 | + data: data, | |
| 326 | + trace_id: trace_id(event) | |
| 326 | 327 | }) |
| 327 | 328 | end |
| 328 | 329 | |
| 330 | + defp trace_id(%{"contexts" => %{"trace" => %{"trace_id" => id}}}) when is_binary(id) do | |
| 331 | + id = String.downcase(id) | |
| 332 | + if Regex.match?(~r/\A[0-9a-f]{32}\z/, id), do: id | |
| 333 | + end | |
| 334 | + | |
| 335 | + defp trace_id(_), do: nil | |
| 336 | + | |
| 329 | 337 | @doc """ |
| 330 | 338 | The tags an event is counted under: its own, plus the ones GlitchTip |
| 331 | 339 | derives (level, environment, release, server name, browser, OS, device, |
modified
lib/vigil/ingest/processor.ex
+2
−0
| 16 | 16 | "client_report" => Vigil.Ingest.Processors.ClientReport, |
| 17 | 17 | "event" => Vigil.Events.Processor, |
| 18 | 18 | "security" => Vigil.Events.Processor, |
| 19 | + "transaction" => Vigil.Performance.Processor, | |
| 20 | + "span" => Vigil.Performance.Processor, | |
| 19 | 21 | "user_report" => Vigil.Events.FeedbackProcessor, |
| 20 | 22 | "feedback" => Vigil.Events.FeedbackProcessor |
| 21 | 23 | } |
modified
lib/vigil/issues/issue_event.ex
+2
−0
| 23 | 23 | field :hashes, {:array, :string}, default: [] |
| 24 | 24 | field :tags, :map, default: %{} |
| 25 | 25 | field :data, :map, default: %{} |
| 26 | + # contexts.trace.trace_id: links the error to its trace's spans. | |
| 27 | + field :trace_id, :string | |
| 26 | 28 | |
| 27 | 29 | belongs_to :project, Vigil.Projects.Project |
| 28 | 30 | belongs_to :issue, Vigil.Issues.Issue |
added
lib/vigil/performance.ex
+280
−0
| 1 | +defmodule Vigil.Performance do | |
| 2 | + @moduledoc """ | |
| 3 | + Reading performance data: transaction groups with their throughput, | |
| 4 | + latency percentiles and error rate, a group's hourly series and sample | |
| 5 | + transactions, and a whole trace (its spans as a tree, plus the errors | |
| 6 | + that happened in it). Writing is `Vigil.Performance.Processor`. | |
| 7 | + | |
| 8 | + Callers pass project ids they have already checked visibility for | |
| 9 | + (`Vigil.Projects.get_visible/2`, `list_visible/1`). | |
| 10 | + """ | |
| 11 | + | |
| 12 | + import Ecto.Query | |
| 13 | + | |
| 14 | + alias Vigil.Issues.{Issue, IssueEvent} | |
| 15 | + alias Vigil.Performance.{Histogram, Span, TransactionGroup} | |
| 16 | + alias Vigil.Repo | |
| 17 | + | |
| 18 | + @doc """ | |
| 19 | + A project's transaction groups seen in the last `hours` (24), each with | |
| 20 | + `:stats`: `count`/`error_count`/`avg_ms` over that window, `tpm` | |
| 21 | + (transactions a minute over it), and `p50`/`p95`/`p99` from the | |
| 22 | + lifetime histogram. Options: `sort` (`:count` (default), `:p95`, | |
| 23 | + `:avg`, `:errors`, `:recent`), `query` (substring of the name), `hours`, | |
| 24 | + `limit` (100). | |
| 25 | + """ | |
| 26 | + def list_groups(project_id, opts \\ []) do | |
| 27 | + hours = Keyword.get(opts, :hours, 24) | |
| 28 | + since = hours_ago(hours) | |
| 29 | + | |
| 30 | + window = | |
| 31 | + from c in "transaction_counts", | |
| 32 | + where: c.hour >= ^since, | |
| 33 | + group_by: c.transaction_group_id, | |
| 34 | + select: %{ | |
| 35 | + group_id: c.transaction_group_id, | |
| 36 | + count: type(sum(c.count), :integer), | |
| 37 | + error_count: type(sum(c.error_count), :integer), | |
| 38 | + duration_sum: type(sum(c.duration_sum), :float) | |
| 39 | + } | |
| 40 | + | |
| 41 | + query = | |
| 42 | + from g in TransactionGroup, | |
| 43 | + join: w in subquery(window), | |
| 44 | + on: w.group_id == g.id, | |
| 45 | + where: g.project_id == ^project_id, | |
| 46 | + select: {g, w}, | |
| 47 | + limit: ^Keyword.get(opts, :limit, 100) | |
| 48 | + | |
| 49 | + query = | |
| 50 | + case opts[:query] do | |
| 51 | + q when is_binary(q) and q != "" -> where(query, [g], ilike(g.name, ^"%#{like(q)}%")) | |
| 52 | + _ -> query | |
| 53 | + end | |
| 54 | + | |
| 55 | + query | |
| 56 | + |> Repo.all() | |
| 57 | + |> Enum.map(fn {g, w} -> with_stats(g, w, hours) end) | |
| 58 | + |> sort(Keyword.get(opts, :sort, :count)) | |
| 59 | + end | |
| 60 | + | |
| 61 | + defp sort(groups, :p95), do: Enum.sort_by(groups, &(&1.stats.p95 || 0), :desc) | |
| 62 | + defp sort(groups, :avg), do: Enum.sort_by(groups, & &1.stats.avg_ms, :desc) | |
| 63 | + defp sort(groups, :errors), do: Enum.sort_by(groups, & &1.stats.error_rate, :desc) | |
| 64 | + defp sort(groups, :recent), do: Enum.sort_by(groups, & &1.last_seen, {:desc, DateTime}) | |
| 65 | + defp sort(groups, _), do: Enum.sort_by(groups, & &1.stats.count, :desc) | |
| 66 | + | |
| 67 | + defp with_stats(group, window, hours) do | |
| 68 | + count = window.count || 0 | |
| 69 | + | |
| 70 | + Map.put(group, :stats, %{ | |
| 71 | + count: count, | |
| 72 | + error_count: window.error_count || 0, | |
| 73 | + error_rate: if(count > 0, do: (window.error_count || 0) / count, else: 0.0), | |
| 74 | + avg_ms: if(count > 0, do: (window.duration_sum || 0.0) / count, else: 0.0), | |
| 75 | + tpm: count / (hours * 60), | |
| 76 | + p50: Histogram.percentile(group.histogram, 50), | |
| 77 | + p95: Histogram.percentile(group.histogram, 95), | |
| 78 | + p99: Histogram.percentile(group.histogram, 99) | |
| 79 | + }) | |
| 80 | + end | |
| 81 | + | |
| 82 | + @doc "A transaction group of a project, with lifetime `:stats` (as in `list_groups/2`), or nil." | |
| 83 | + def get_group(project_id, id) do | |
| 84 | + with {:ok, id} <- Ecto.UUID.cast(id), | |
| 85 | + %TransactionGroup{} = g <- Repo.get_by(TransactionGroup, id: id, project_id: project_id) do | |
| 86 | + hours = max(DateTime.diff(DateTime.utc_now(), g.first_seen, :hour), 1) | |
| 87 | + | |
| 88 | + with_stats( | |
| 89 | + g, | |
| 90 | + %{count: g.count, error_count: g.error_count, duration_sum: g.duration_sum}, | |
| 91 | + hours | |
| 92 | + ) | |
| 93 | + else | |
| 94 | + _ -> nil | |
| 95 | + end | |
| 96 | + end | |
| 97 | + | |
| 98 | + @doc """ | |
| 99 | + A group's last `hours` (24) hour by hour, oldest first, empty hours | |
| 100 | + included: `[%{hour:, count:, error_count:, avg_ms:}]`. | |
| 101 | + """ | |
| 102 | + def group_hourly(group_id, hours \\ 24) do | |
| 103 | + since = hours_ago(hours - 1) | |
| 104 | + | |
| 105 | + rows = | |
| 106 | + Repo.all( | |
| 107 | + from c in "transaction_counts", | |
| 108 | + where: c.transaction_group_id == type(^group_id, :binary_id) and c.hour >= ^since, | |
| 109 | + select: {c.hour, c.count, c.error_count, c.duration_sum} | |
| 110 | + ) | |
| 111 | + |> Map.new(fn {hour, n, e, d} -> {NaiveDateTime.truncate(hour, :second), {n, e, d}} end) | |
| 112 | + | |
| 113 | + for i <- 0..(hours - 1) do | |
| 114 | + hour = NaiveDateTime.add(since, i * 3600) | |
| 115 | + {n, e, d} = Map.get(rows, hour, {0, 0, 0.0}) | |
| 116 | + | |
| 117 | + %{ | |
| 118 | + hour: DateTime.from_naive!(hour, "Etc/UTC"), | |
| 119 | + count: n, | |
| 120 | + error_count: e, | |
| 121 | + avg_ms: if(n > 0, do: d / n, else: 0.0) | |
| 122 | + } | |
| 123 | + end | |
| 124 | + end | |
| 125 | + | |
| 126 | + @doc "The histogram's buckets as `[%{from_ms:, to_ms:, count:}]`, for a chart." | |
| 127 | + def histogram_buckets(%{histogram: h}) do | |
| 128 | + bounds = Histogram.boundaries() | |
| 129 | + | |
| 130 | + h | |
| 131 | + |> Enum.with_index() | |
| 132 | + |> Enum.map(fn {n, i} -> | |
| 133 | + %{from_ms: Enum.at(bounds, i), to_ms: Enum.at(bounds, i + 1), count: n} | |
| 134 | + end) | |
| 135 | + end | |
| 136 | + | |
| 137 | + @doc """ | |
| 138 | + Sample transactions (segment spans) of a group: `order` `:recent` | |
| 139 | + (default) or `:slowest`, from the spans still kept. | |
| 140 | + """ | |
| 141 | + def samples(group_id, opts \\ []) do | |
| 142 | + query = | |
| 143 | + from s in Span, | |
| 144 | + where: s.transaction_group_id == ^group_id and s.is_segment, | |
| 145 | + limit: ^Keyword.get(opts, :limit, 20) | |
| 146 | + | |
| 147 | + query = | |
| 148 | + case Keyword.get(opts, :order, :recent) do | |
| 149 | + :slowest -> order_by(query, [s], desc: s.duration_ms) | |
| 150 | + _ -> order_by(query, [s], desc: s.id) | |
| 151 | + end | |
| 152 | + | |
| 153 | + Repo.all(query) | |
| 154 | + end | |
| 155 | + | |
| 156 | + @doc """ | |
| 157 | + A trace across the given (visible) projects: `nil` if none of its spans | |
| 158 | + are there, else `%{trace_id:, spans:, roots:, start_at:, duration_ms:, | |
| 159 | + errors:}`. `spans` are ordered by start, each with `:depth` and | |
| 160 | + `:offset_ms` (from the trace's start) for a waterfall; `roots` are the | |
| 161 | + span ids with no parent among them. `errors` are the trace's issue | |
| 162 | + events (`IssueEvent`s, with `issue.project` preloaded). | |
| 163 | + """ | |
| 164 | + def get_trace(project_ids, trace_id) do | |
| 165 | + trace_id = String.downcase(trace_id || "") | |
| 166 | + | |
| 167 | + spans = | |
| 168 | + Repo.all( | |
| 169 | + from s in Span, | |
| 170 | + where: s.project_id in ^project_ids and s.trace_id == ^trace_id, | |
| 171 | + order_by: [asc: s.start_at], | |
| 172 | + limit: 5000 | |
| 173 | + ) | |
| 174 | + | |
| 175 | + errors = trace_errors(project_ids, trace_id) | |
| 176 | + | |
| 177 | + case spans do | |
| 178 | + [] when errors == [] -> | |
| 179 | + nil | |
| 180 | + | |
| 181 | + [] -> | |
| 182 | + %{ | |
| 183 | + trace_id: trace_id, | |
| 184 | + spans: [], | |
| 185 | + roots: [], | |
| 186 | + start_at: nil, | |
| 187 | + duration_ms: 0.0, | |
| 188 | + errors: errors | |
| 189 | + } | |
| 190 | + | |
| 191 | + spans -> | |
| 192 | + start = Enum.min_by(spans, & &1.start_at, DateTime).start_at | |
| 193 | + | |
| 194 | + finish = | |
| 195 | + spans | |
| 196 | + |> Enum.map(&DateTime.add(&1.start_at, round(&1.duration_ms * 1000), :microsecond)) | |
| 197 | + |> Enum.max(DateTime) | |
| 198 | + | |
| 199 | + ordered = tree_order(spans) | |
| 200 | + | |
| 201 | + %{ | |
| 202 | + trace_id: trace_id, | |
| 203 | + spans: | |
| 204 | + Enum.map(ordered, fn {span, depth} -> | |
| 205 | + span | |
| 206 | + |> Map.put(:depth, depth) | |
| 207 | + |> Map.put(:offset_ms, DateTime.diff(span.start_at, start, :microsecond) / 1000) | |
| 208 | + end), | |
| 209 | + roots: ordered |> Enum.filter(&(elem(&1, 1) == 0)) |> Enum.map(&elem(&1, 0).span_id), | |
| 210 | + start_at: start, | |
| 211 | + duration_ms: DateTime.diff(finish, start, :microsecond) / 1000, | |
| 212 | + errors: errors | |
| 213 | + } | |
| 214 | + end | |
| 215 | + end | |
| 216 | + | |
| 217 | + # Depth-first, children by start time; spans whose parent isn't here | |
| 218 | + # are roots. Cycles can't loop: each span is placed once. | |
| 219 | + defp tree_order(spans) do | |
| 220 | + ids = MapSet.new(spans, & &1.span_id) | |
| 221 | + | |
| 222 | + children = | |
| 223 | + Enum.group_by(spans, fn s -> | |
| 224 | + if s.parent_span_id && MapSet.member?(ids, s.parent_span_id), do: s.parent_span_id | |
| 225 | + end) | |
| 226 | + | |
| 227 | + roots = Map.get(children, nil, []) | |
| 228 | + | |
| 229 | + {acc, _seen} = | |
| 230 | + Enum.reduce(roots, {[], MapSet.new()}, fn root, {acc, seen} -> | |
| 231 | + walk(root, 0, children, acc, seen) | |
| 232 | + end) | |
| 233 | + | |
| 234 | + Enum.reverse(acc) | |
| 235 | + end | |
| 236 | + | |
| 237 | + defp walk(span, depth, children, acc, seen) do | |
| 238 | + if MapSet.member?(seen, span.id) do | |
| 239 | + {acc, seen} | |
| 240 | + else | |
| 241 | + acc = [{span, depth} | acc] | |
| 242 | + seen = MapSet.put(seen, span.id) | |
| 243 | + | |
| 244 | + children | |
| 245 | + |> Map.get(span.span_id, []) | |
| 246 | + |> Enum.reduce({acc, seen}, fn child, {acc, seen} -> | |
| 247 | + walk(child, depth + 1, children, acc, seen) | |
| 248 | + end) | |
| 249 | + end | |
| 250 | + end | |
| 251 | + | |
| 252 | + defp trace_errors(project_ids, trace_id) do | |
| 253 | + Repo.all( | |
| 254 | + from e in IssueEvent, | |
| 255 | + join: i in Issue, | |
| 256 | + on: i.id == e.issue_id, | |
| 257 | + where: e.project_id in ^project_ids and e.trace_id == ^trace_id and is_nil(i.deleted_at), | |
| 258 | + order_by: [asc: e.id], | |
| 259 | + limit: 100, | |
| 260 | + preload: [issue: {i, :project}] | |
| 261 | + ) | |
| 262 | + end | |
| 263 | + | |
| 264 | + @doc "The trace id an issue event belongs to, or nil." | |
| 265 | + def trace_id_of(%IssueEvent{trace_id: id}), do: id | |
| 266 | + | |
| 267 | + @doc "Whether any spans of the trace are kept in these projects." | |
| 268 | + def trace_exists?(project_ids, trace_id) when is_binary(trace_id) do | |
| 269 | + Repo.exists?(from s in Span, where: s.project_id in ^project_ids and s.trace_id == ^trace_id) | |
| 270 | + end | |
| 271 | + | |
| 272 | + def trace_exists?(_, _), do: false | |
| 273 | + | |
| 274 | + defp hours_ago(hours) do | |
| 275 | + now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) | |
| 276 | + NaiveDateTime.add(%{now | minute: 0, second: 0}, -hours * 3600) | |
| 277 | + end | |
| 278 | + | |
| 279 | + defp like(q), do: String.replace(q, ~w(\\ % _), &"\\#{&1}") | |
| 280 | +end |
added
lib/vigil/performance/histogram.ex
+55
−0
Click to load diff…
added
lib/vigil/performance/normalize.ex
+216
−0
Click to load diff…
added
lib/vigil/performance/processor.ex
+146
−0
Click to load diff…
added
lib/vigil/performance/span.ex
+35
−0
Click to load diff…
added
lib/vigil/performance/transaction_group.ex
+23
−0
Click to load diff…
added
priv/repo/migrations/20261202023246_create_performance.exs
+90
−0
Click to load diff…
added
test/vigil/performance_test.exs
+228
−0
Click to load diff…
Parents: 145dcb0